本文介绍了使用带有execve的一个新的路径来运行ls命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用execve的运行ls命令。目前我使用以下参数运行它:

I am trying to use execve to run the ls command. Currently I'm running it with the following arguments:

execve(args[0], args, env_args)
//args looks like {"ls", "-l", "-a", NULL}
//env_args looks like {"PATH=/bin", "USER=me", NULL}

我预计今年要做的就是运行使用我的新env_args这意味着它会查找LS在我的道路ls命令。然而,这code实际上没有做任何事情,当我运行code它只返回到我的命令提示符下,没有输出。

What I expected this to do was run the ls command using my new env_args meaning that it would look up ls in my PATH. However, this code actually doesn't do anything and when I run the code it just returns to my command prompt without output.

使用相同的ARGS []我用execvp和LS工作,并搜查了我的电流路径。

Using the same args[] I was using execvp and ls worked and searched my current path.

你能告诉我什么,我做错了什么?

Can you tell me what I am doing wrong?

我所试图做的是我自己写的shell程序,我可以创建并导出我自己的环境,并有EXEC利用我在一个char定义的环境**。从本质上讲,我写我自己的函数在env_args操作添加和删除增值经销商,当我调用exec我希望能够呼吁EXEC {LS,-l,NULL}并让它往下看我的新环境一个有效的程序路径变量称为LS。我希望这说明我在做什么好一点。我不认为EXTERN ENVIRON变种会为我在这种情况下工作。

What I am trying to do is write my own shell program where I can create and export my own environment and have exec use the environment I have defined in a char**. Essentially I am writing my own functions to operate on env_args to add and remove vars and when I call exec i want to be able to call exec on {"ls", "-l", NULL} and have it look down my new environments path variable for a valid program called ls. I hope this explains what I am doing a little better. I don't think the extern environ var will work for me in this case.

推荐答案

的execve()不看PATH;对于这一点,你需要 execvp()。你的程序没有执行 LS ,显然你不报故障的execve()。请注意, EXEC *的成员()系列函数只在返回错误。

execve() does not look at PATH; for that, you need execvp(). Your program was failing to execute ls, and apparently you don't report failures to execute a program after the execve(). Note that members of the exec*() family of functions only return on error.

您会得到您所期望,如果你跑了 / bin中作为当前目录(因为方案(或多或少)的结果。 / LS - 又名 LS - 然后将存在)

You'd get the result you expected (more or less) if you ran the program with /bin as your current directory (because ./ls - aka ls - would then exist).

您需要提供的第一个参数的可执行文件的路径名的execve(),使用适当的设置PATH找到它了。

You need to provide the pathname of the executable in the first argument to execve(), after finding it using an appropriate PATH setting.

,或者继续使用 execvp(),但设置变量 ENVIRON 到新环境。需要注意的是 ENVIRON 现在(POSIX 2008)&LT宣布; unistd.h中> ,但previously是没有在任何地方声明。

Or continue to use execvp(), but set the variable environ to your new environment. Note that environ is now (POSIX 2008) declared in <unistd.h>, but previously was not declared anywhere.

extern char **environ;

environ = env_args;
execvp(args[0], &args[0]);

您不需要保存旧值,并恢复它;你在子进程和切换的环境不会影响主程序(壳)。

You don't need to save the old value and restore it; you're in the child process and switching its environment won't affect the main program (shell).

这似乎工作,我期望 - 并表明原来的code行为与我期望

This seems to work as I'd expect - and demonstrates that the original code behaves as I'd expect.

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

extern char **environ;

int main(void)
{
    char *args[]     = { "ls", "-l", "-a", NULL };
    char *env_args[] = { "PATH=/bin", "USER=me", NULL };

    execve(args[0], args, env_args);
    fprintf(stderr, "Oops!\n");

    environ = env_args;
    execvp(args[0], &args[0]);
    fprintf(stderr, "Oops again!\n");

    return -1;
}

我得到一个糟糕!其次是我的目录清单。当我创建一个可执行 LS 在我的当前目录:

#!/bin/sh
echo "Haha!"

然后,我没有得到糟糕!并做得到'哈哈!

then I don't get the 'Oops!' and do get the 'Haha!'.

这篇关于使用带有execve的一个新的路径来运行ls命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:45