0
我有一个任务来编写一个c程序,该程序可以使用fork和exec在$ PATH中列出的所有目录中搜索目录。我的问题是我如何得到的方式,然后我就可以在我的代码中使用它与EXECL
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char* argv[]) {
int pid = 0;
for(int i = 0; i < argc; i++) {
if (pid = fork() != 0){
printf("Arg%d: %c\n", i, *argv[i]); // replace with exec ls -l <dir>/<arg>
return 0;
}
}
return 0;
}
With ['getenv()'](http://en.cppreference.com/w/c/program/getenv)? –
请记住,'argv [0]'是* your *程序的名称。 –
@JOACHIMPILEBORG你是对的,我错过了。 –