Spence123
Spence123

Reputation: 107

How to pass command line arguments to C program using execlp

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.

Upvotes: 2

Views: 22213

Answers (3)

James Peters
James Peters

Reputation: 161

Your issue is that execlp takes string pointers not integers for its arg parameters. From the manpage

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

You will have to convert these to strings before passing them to execlp.

#include<stdio.h>
#include<unistd.h>

#define MAXDIGITS 22

main()
{
    int exec_arg_1, exec_arg_2;

    char execStr1[MAXDIGITS + 1];
    char execStr2[MAXDIGITS + 1];

    exec_arg_1 = 750;
    exec_arg_2 = 25;

    snprintf(execStr1, MAXDIGITS + 1, "%d", exec_arg_1);
    snprintf(execStr2, MAXDIGITS + 1, "%d", exec_arg_2);

    printf("Our Strings: %s, %s\n", execStr1, execStr2);
    execlp("/home/drlight/Desktop/asp/Assignment_3/philosopher.o", "philosopher.o", execStr1, execStr2, NULL);
}

You need to make sure MAXDIGITS is large enough to hold all the decimal digits of your number, but 25 should be sufficient for even longs on most current platforms. However keep in mind that in future versions of gcc and/or with different compilers this may be different. Don't forget to leave room for the negative either. You can check these maximums by importing limits.h and printing the values of INT_MAX and LONG_MAX.

#include<stdio.h>
#include<limits.h>

main(int argc, char * argv[])
{
    printf("Int max: %d\n", INT_MAX);
    printf("Long max: %ld\n", LONG_MAX);
}

Upvotes: 0

fnisi
fnisi

Reputation: 1223

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

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

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.

Upvotes: 3

Related Questions