tholm@uvicctr.UUCP (Terrence W. Holm) (08/26/88)
EFTH MINIX report #28 - August 1988 - execlp(3) & execvp(3) There follows an implementation of execlp(3) and execvp(3) for Minix. Please consider this public domain software. A "man" page is included. ---------------------------------------------------------- echo x - execl.3 gres '^X' '' > execl.3 << '/' XSUBROUTINES X execl(3) - load a process image from a file X XINVOCATION X execl( path, arg0, arg1, ..., argn, (char *) 0 ) X char *path, *arg0, *arg1, ..., *argn; X X execle( path, arg0, arg1, ..., argn, (char *) 0, envp ) X char *path, *arg0, *arg1, ..., *argn, *envp[]; X X execlp( file, arg0, arg1, ..., argn, (char *) 0 ) X char *file, *arg0, *arg1, ..., *argn; X X execv( path, argv ) X char *path, *argv[]; X X execvp( file, argv ) X char *file, *argv[]; X X execn( name ) X char *name; X XEXPLANATION X These calls are simpler calls to execve(2). The subroutines X execl(3), execle(3) and execlp(3) allow the "argv" vector to X be given as a list of strings, terminated by a (char *) 0. X This differentiates them from execv(3), execve(2) and execvp(3). X X Execle(3) and execve(2) allow an environment parameter, all X of the other calls pass the current environment. X X Execlp(3) and execvp(3) are like execl(3) and execv(3), except X that they use the environment variable $PATH as a search list X of possible locations for the executable file, but only if <file> X does not start with a '/'. X X The path search list is a list of directory names separated by X ':'s. If a colon appears at the beginning or end of the list, X or two appear together, then an empty prefix is tried. If $PATH X is not in the environment, it defaults to ":/bin:/usr/bin". X X For example, with the default $PATH and the file "sh", the X attempts will be: "sh", "/bin/sh" and "/usr/bin/sh". X X For the special case of no argument list and no environment X the execn(3) call is available, though this should not be used X by applications. X XRESULTS X o/w : Will not return. X -1 : Error. If <file> is not an executable file in the search X path, then errno is set to ENOENT. X XREFERENCES X execve(2), fork(2), environ(4) / echo x - execlp.c gres '^X' '' > execlp.c << '/' X/* execlp(3) and execvp(3) X * X * Author: Terrence W. Holm July 1988 X * X * X * Execlp(3) and execvp(3) are like execl(3) and execv(3), X * except that they use the environment variable $PATH as X * a search list of possible locations for the executable X * file, if <file> does not start with a '/'. X * X * The path search list is a list of directory names separated X * by ':'s. If a colon appears at the beginning or end of the X * list, or two appear together, then an empty prefix is X * tried. If $PATH is not in the environment, it defaults to X * ":/bin:/usr/bin". X * X * For example, if <file> is "sh", and the $PATH is X * ":/bin:/usr/local:/usr/bin", then the attempts will be: X * "sh", "/bin/sh", "/usr/local/sh" and "/usr/bin/sh". X * X * If the <file> is not an executable file in one of the X * directories, then -1 is returned. X */ X X#include <errno.h> X#include <string.h> X#include <unistd.h> X X#ifndef X_OK X#define X_OK 1 X#endif X X#define NULL (char *) 0 X Xextern char *getenv(); X Xextern char **environ; Xextern int errno; X X Xexeclp( file, arg0 ) X char *file; X char *arg0; X X { X return( execvp( file, &arg0 ) ); X } X X Xexecvp( file, argv ) X char *file; X char *argv[]; X X { X char path_name[100]; X char *next; X char *path = getenv( "PATH" ); X X if ( path == NULL ) X path = ":/bin:/usr/bin"; X X if ( file[0] == '/' ) X path = ""; X X do { X next = strchr( path, ':' ); X X if ( next == NULL ) X strcpy( path_name, path ); X else X { X *path_name = '\0'; X strncat( path_name, path, next - path ); X path = next + 1; X } X X if ( *path_name != '\0' ) X strcat( path_name, "/" ); X X strcat( path_name, file ); X X if ( access( path_name, X_OK ) == 0 ) X execve( path_name, argv, environ ); X } while ( next != NULL ); X X errno = ENOENT; X return( -1 ); X } / ---------------------------------------------------------- Edwin L. Froese uw-beaver!ubc-cs!mprg!handel!froese Terrence W. Holm uw-beaver!uvicctr!tholm