如何使用 execlp 将命令行参数传递给 C 程序

How to pass command line arguments to C program using execlp

我正在尝试在一个 c 程序中使用 execlp 运行 另一个 c 程序。 exec 函数确实调用了程序,但它没有正确传递整数参数。我的执行电话是:

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");
    }

我给 exec_arg 整数赋值,并在之前打印它们以确保它们是正确的,但是 philosopher.o 函数只是从该位置读取 0。如果我从命令行 运行 philosopher.o ,它会正常读取参数。

This page 包含大量使用示例....

编辑:添加了 link 中的代码片段 上面 link 的代码片段

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 execlp(3) man page

编辑:从手册页添加了对 execlp(3) 的解释 FreeBSD man page对execlp()的用法解释如下

 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 : 一些信息,例如默认搜索路径,可能因您的系统而异

程序的参数始终是字符串。

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);
}

请注意,execlp() 实际上只对固定数量的参数有用(或者,至少,当参数数量有一个小的固定上限时)。大多数情况下,execvp() 是更好的选择。

您的问题是 execlp 的 arg 参数采用字符串指针而不是整数。来自联机帮助页

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

在将它们传递给 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);
}

您需要确保 MAXDIGITS 足够大以容纳您号码的所有小数位,但在大多数当前平台上,25 应该足以容纳偶数。但是请记住,在具有不同编译器的 gcc and/or 的未来版本中,这可能会有所不同。也不要忘记为负面留出空间。您可以通过导入 limits.h 并打印 INT_MAX 和 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);
}