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你是對的,我錯過了。 –