[英]cd command not working with execvp
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
int main()
{
char *ip;
char *temp[10];
pid_t pid;
pid = fork();
if (pid == 0) {
int i = 0;
do {
gets(ip);
temp[0] = strtok(ip, " ");
while (temp[++i] != NULL) {
temp[i] = strtok(NULL," ");
}
pid_t pid2;
pid2 = fork();
if (pid2 == 0) {
execvp(temp[0], temp);
}
} while(strcmp(temp[0], "quit"));
if (!strcmp(temp[0],"quit")) {
return;
}
} else if (pid < 0) {
fprintf(stderr,"error in creating child");
} else if (pid > 0) {
wait(NULL);
}
}
這段代碼似乎不適用於cd命令。我是否要修復它?我對操作系統的概念相當新,任何幫助將不勝感激! :)
cd
不作為可執行命令存在。 (它不能,因為一個進程只能改變自己的工作目錄,而不是它的父進程。)你需要使用chdir()
系統調用自己實現cd
作為內置,類似於你已經的方式實施quit
。
如果你計划實現它們,你還需要實現內置的其他命令包括(例如,我不是要徹底):
pushd
和popd
exit
, logout
, bye
等 fg
, bg
, jobs
和&
suffix history
set
, unset,
export
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.