COHERENT manpages

This page displays the COHERENT manpage for execve() [Execute a load module].

List of available manpages
Index


execve() -- System Call (libc)

Execute a load module
#include <unistd.h>
execve(file, argv, env)
char *file, *argv[], *env[];

The  function execve()  executes a  program.  It  specifies arguments  as a
single, NULL-terminated array  of parameters, called argv. The argument env
is  the address  of an  array  of pointers  to strings  that define  file's
environment.  This allows execve() to pass a new environment to the program
being executed.  For more information on program execution, see execution.

Example

The following example demonstrates execve(), as well as tmpnam(), getenv(),
and path().  It finds all lines  with more than LIMIT  characters and calls
MicroEMACS to edit them.

#include <stdio.h>
#include <path.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

#define LIMIT 70

extern **environ, *tempnam();

main(argc, argv)
int argc; char *argv[];
{
    /*                me     -e   tmp   file */
    char *cmda[5] = { NULL, "-e", NULL, NULL, NULL };
    FILE *ifp, *tmp;
    char line[256];
    int  ct, len;

    if ((NULL == (cmda[3] = argv[1])) ||
        (NULL == (ifp = fopen(argv[1], "r")))) {
        fprintf(stderr, "Cannot open %s\n", argv[1]);
        exit(EXIT_FAILURE);
    }

    if ((cmda[0] = path(getenv("PATH"), "me", X_OK)) == NULL) {
        fprintf(stderr, "Cannot locate me\n");
        exit(EXIT_FAILURE);
    }

    if (NULL == (tmp = fopen((cmda[2] = tempnam(NULL, "lng")), "w"))) {
        fprintf(stderr, "Cannot open tmpfile\n");
        exit(EXIT_FAILURE);
    }

    for (ct = 1; NULL != fgets(line, sizeof(line), ifp); ct++)
        if (((len = strlen(line)) > LIMIT) ||
            ('\n' != line[len -1]))
            fprintf(tmp, "%d: %d characters long\n", ct, len);

    fclose(tmp);
    fclose(ifp);

    if (execve(cmda[0], cmda, environ) < 0) {
        fprintf(stderr, "cannot execute me\n");
        exit(EXIT_FAILURE);
    }
}

See Also

environ,
execution,
libc,
unistd.h
POSIX Standard, §3.1.2

Diagnostics

execve() does not return if successful.   It returns -1 for errors, such as
file being  nonexistent, not accessible  with execute permission,  having a
bad format, or too large to fit in memory.