How to pass command line arguments to C program using execlp

18,663

Solution 1

Arguments to programs are always strings.

int exec_arg_1, exec_arg_2;

if (pid == 0){
    printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
    char arg1[20], arg2[20];
    snprintf(arg1, sizeof(arg1), "%d", exec_arg_1);
    snprintf(arg2, sizeof(arg2), "%d", exec_arg_2);
    execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o", 
       "philosopher.o", arg_1, arg_2, NULL );
    fprintf(stderr, "Exec didn't work...\n");
    exit(1);
}

Note that execlp() is really only useful with a fixed number of arguments (or, a least, when there is a small fixed upper bound on the number of arguments). Most often, execvp() is a better choice.

Solution 2

This page includes plenty of usage examples....

EDIT : Added code snippet from the link A code snippet from the link above

static void show_info_page(const char *git_cmd)
{
    const char *page = cmd_to_page(git_cmd);
    setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
    execlp("info", "info", "gitman", page, (char *)NULL);
    die(_("no info viewer handled the request"));
}

The best practice would be having a look at the execlp(3) man page in the first place I reckon.

EDIT : Added explanation of execlp(3) fro mthe man page FreeBSD man page explains the usage of execlp() as follows

 int
 execlp(const char *file, const char *arg, ... /*, (char *)0 */);

 The initial argument for these functions is the pathname of a file which
 is to be executed.

 The const char *arg and subsequent ellipses in the execl(), execlp(), and
 execle() functions can be thought of as arg0, arg1, ..., argn.  Together
 they describe a list of one or more pointers to null-terminated strings
 that represent the argument list available to the executed program.  The
 first argument, by convention, should point to the file name associated
 with the file being executed.  The list of arguments must be terminated
 by a NULL pointer.

 The functions execlp(), execvp(), and execvP() will duplicate the actions
 of the shell in searching for an executable file if the specified file
 name does not contain a slash ``/'' character.  For execlp() and
 execvp(), search path is the path specified in the environment by
 ``PATH'' variable.  If this variable is not specified, the default path
 is set according to the _PATH_DEFPATH definition in <paths.h>, which is
 set to ``/usr/bin:/bin''

PS : some information, such as default search path, mat vary based on your system

Share:
18,663
Spence123
Author by

Spence123

Updated on June 04, 2022

Comments

  • Spence123
    Spence123 about 2 years

    I am trying to use execlp in a c program to run another c program. The exec function does call the program, but it does not pass the integer arguments correctly. My exec call is:

    int exec_arg_1, exec_arg_2;
    
    if(pid == 0){
        printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
        execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o", 
           "philosopher.o", &exec_arg_1, &exec_arg_2, NULL );
               printf("Exec didn't work...\n");
        }
    

    I assign values to the exec_arg ints, and print them right before to make sure they're correct, but the philosopher.o function just reads 0's from the location. If I run philosopher.o from the command line, it reads the arguments normally.

  • kaylum
    kaylum over 8 years
    Please note that link only answers are consider low quality on SO and may be flagged for deletion. The official SO blurb: “While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.”