C语言 使用 C-if 条件实现 cd 系统调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16094814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
implementing cd system call using C- if condition
提问by Urvah Shabbir
Here is code that implements the cd system call using C. The problem with this code is that it's not entering the ifcondition if(strcmp(buffer,"cd") == 0)and I can't understand why.
这是使用 C 实现 cd 系统调用的代码。这段代码的问题是它没有进入if条件 if(strcmp(buffer,"cd") == 0),我不明白为什么。
#include<sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include<dirent.h>
#include<error.h>
#define BUFFERSIZE 20
int main(){
char *args[80];
char buffer[BUFFERSIZE];
char *prompt = "OS";
char *a = ">";
printf("%s%s",prompt,a);
fgets(buffer, BUFFERSIZE, stdin);
char *tok;
tok = strtok (buffer," ");
while(buffer != NULL){
buffer[strlen(buffer)-1] = '#include <stdio.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
//#include <error.h>
int hasPrefix(char const *, char const *);
int cd(char *pth);
#define BUFFERSIZE 200
int main(){
char buffer[BUFFERSIZE];
char *prompt = "OS";
char *a = ">";
char *tok;
tok = strtok (buffer," ");
while(buffer != NULL){
bzero(buffer, BUFFERSIZE);
printf("%s%s",prompt,a);
fgets(buffer, BUFFERSIZE, stdin);
if(hasPrefix(buffer,"cd") == 0){
tok = strchr(buffer,' '); //use something more powerful
if(tok) {
char *tempTok = tok + 1;
tok = tempTok;
char *locationOfNewLine = strchr(tok, '\n');
if(locationOfNewLine) {
*locationOfNewLine = '...
if(strncmp(buffer,"cd",2) == 0){
...
';
}
cd(tok);
}
}else{
system("ls"); //for testing the CWD/PWD
}
}
return 0;
}
int hasPrefix(char const *p, char const *q)
{
int i = 0;
for(i = 0;q[i];i++)
{
if(p[i] != q[i])
return -1;
}
return 0;
}
int cd(char *pth){
char path[BUFFERSIZE];
strcpy(path,pth);
char cwd[BUFFERSIZE];
if(pth[0] != '/')
{// true for the dir in cwd
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/");
strcat(cwd,path);
chdir(cwd);
}else{//true for dir w.r.t. /
chdir(pth);
}
return 0;
}
';
pid_t pid;
pid = fork();
if(pid < 0){
fprintf(stderr, "Fork failed");
return 1;
}
else if(pid == 0){
if(strcmp(buffer,"cd") == 0){
tok = strtok(NULL,"\n");
cd(tok);
}
printf("%s%s",prompt,a);
fgets(buffer, BUFFERSIZE, stdin);
}
else{
wait(NULL);
}
}
return 0;
}
int cd(char *pth){
char path[1000];
strcpy(path,pth);
static char *prompt = "OS";
static char *a = ">";
char *token;
char cwd[256];
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/");
strcat(cwd,path);
chdir(cwd);
printf("%s-%s%s",prompt,path,a);
return 0;
}
采纳答案by bikram990
Have updated the logic after suggestions from others.
根据其他人的建议更新了逻辑。
There is no need for a child processhere. If you want multitasking then use threads. Child process may be required for process running in background.
这里不需要子进程。如果您想要多任务处理,请使用线程。Child process may be required for process running in background.
The following program is working for me:
以下程序对我有用:
buffer[strlen(buffer)-1] = '##代码##';
回答by Jay Stenmark
Use
用
##代码##instead. It is good for comparing prefixes of arbitrary length. It's also puts a limit on string size. No need to build your own comparison routine.
反而。它适用于比较任意长度的前缀。它也限制了字符串的大小。无需构建自己的比较例程。
You have other problems elsewhere in the code, but those can be addressed separately.
您在代码的其他地方还有其他问题,但可以单独解决这些问题。
回答by Barmar
I think the problem is because of this line:
我认为问题是因为这一行:
##代码##This replaces the last character of bufferwith a null character. So if buffercontained "cd", it now contains just "c"(since the null character is the string terminator in C).
这将buffer用空字符替换最后一个字符。所以,如果buffer包含"cd",它现在只包含"c"(因为空字符是在C中的字符串结束)。
There doesn't seem to be any need for this statement, just remove it.
这个语句似乎没有任何必要,只需将其删除即可。

