问题描述
试图用 | 模仿 linux 的管道命令钥匙。例如 ls | sort
以及处理像 -r -a -lh
这样的参数,这是我的完整源代码。请记住,看起来有点痛苦,但重要的是我在开始时包含我的完整源代码,所以不用多说:
//################ #-for include
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <dirent.h>
//################
int main(int b,char ** locations) {
int ok = 0;
int dots = 0;
char argA[1000];
char argB[1000];
char argAP[1000];
char argBP[1000];
while (locations[ok] != NULL) {
//printf("%s \n",locations[ok]);
if (strcmp(locations[ok],":") == 0) {
dots = 1;
}
ok++;
} // this code block checks if a second arg was given.
strcpy(argA,"");
strcpy(argB,"");
strcpy(argAP,"");
strcpy(argBP,"");
if (dots == 0) {
int x = 1;
strcat(argA,locations[x]); //strcat(argA,"");
x++;
while (locations[x] != NULL) {
strcat(argAP,locations[x]); //strcat(argAP,"");
x++;
} //printf("%s%s \n",argA,argAP);
} // this code block gets the single command if two arent given and
// so argA is the command and argAP is the arguments parameters (arguments).
if (dots == 1) {
int x = 1;
int compare = strcmp(locations[x],":");
if (strcmp(locations[1],":") == 0) {
x++;
//printf("one arg\n");
strcat(argA,"");
x++;
while (locations[x] != NULL) {
strcat(argAP,"");
x++;
} //printf("%s%s \n",argAP);
} else {
//printf("two args\n");
strcat(argA,"");
compare = strcmp(locations[x],":");
x++;
compare = strcmp(locations[x],":");
while (compare != 0) {
strcat(argAP,"");
x++;
compare = strcmp(locations[x],":");
} //printf("argA: %s%s \n",argAP);
x++;
strcat(argB,locations[x]); //strcat(argB,"");
x++;
while (locations[x] != NULL) {
strcat(argBP,locations[x]); //strcat(argBP,"");
x++;
} //printf("argB: %s%s \n",argB,argBP);
}
} // this code block gets the 2 args and their command and arguments
// ########### fork and piping ############
// so this is inspired by our class example
//strcpy(argA,"ls"); strcpy(argAP,"-a");
printf("%s+%s+ \n",argAP); // this code block is for simply visual
printf("%s+%s+ \n",argBP); // verification of correct input.
//execlp(argA,(char *) NULL);
pid_t pid;
int fd[2];
pipe(fd);
int rd = fd[0]; // rd points to 0 (read) in pipe
int wt = fd[1]; // wt points to 1 (write) in pipe
pid = fork();
if (pid == 0) {
if (strcmp(argAP,"") == 0) {
dup(rd);
close(rd);
close(wt);
execlp(argA,(char * ) NULL);
fprintf(stderr,"Failed to execute \n");
} else {
dup(rd);
close(rd);
close(wt);
execlp(argA,argAP,"Failed to execute \n");
}
exit(0);
} else {
pid = fork();
if (pid == 0) {
if (strcmp(argBP,"") == 0) {
dup(rd);
close(wt);
close(rd);
execlp(argB,(char * ) NULL);
fprintf(stderr,"Failed to execute \n");
} else {
dup(rd);
close(wt);
close(rd);
execlp(argB,argBP,"Failed to execute \n");
}
exit(1);
} else {
int status;
close(rd);
close(wt);
waitpid(pid,& status,0);
}
exit(1);
}
// ########### fork and piping ############
return 0;
}
这是为了以防您需要直接复制我的程序,所以我有这四个变量 argA 和 argAP,同样我有 argB 和 argBP,其中 arg(CHAR) 是命令,arg(CHAR P) 是参数命令,例如 ls -a argA = ls,and argAP = -a
。我无法开始工作的部分是我代码的以下特定部分:
// ########### fork and piping ############
// so this is inspired by our class example
//strcpy(argA,"-a");
printf("%s+%s+ \n",argAP); // this code block is for simply visual
printf("%s+%s+ \n",argBP); // verification of correct input.
//execlp(argA,(char *) NULL);
pid_t pid;
int fd[2];
pipe(fd);
int rd = fd[0]; // rd points to 0 (read) in pipe
int wt = fd[1]; // wt points to 1 (write) in pipe
pid = fork();
if (pid == 0) {
if (strcmp(argAP,"") == 0) {
dup(rd);
close(rd);
close(wt);
execlp(argA,(char * ) NULL);
fprintf(stderr,"Failed to execute \n");
} else {
dup(rd);
close(rd);
close(wt);
execlp(argA,"Failed to execute \n");
}
exit(0);
} else {
pid = fork();
if (pid == 0) {
if (strcmp(argBP,"") == 0) {
dup(rd);
close(wt);
close(rd);
execlp(argB,"Failed to execute \n");
} else {
dup(rd);
close(wt);
close(rd);
execlp(argB,"Failed to execute \n");
}
exit(1);
} else {
int status;
close(rd);
close(wt);
waitpid(pid,0);
}
exit(1);
}
// ########### fork and piping ############
所以运行程序而不是使用 |你必须使用:所以运行 ls: sort == ls | sort;
我为大量的代码道歉,我只是将它设置为它可以处理的地方,如果用户只需输入:ls 或只是简单的 ls,那么程序就设置好了处理一些用户输入错误。现在运行实际命令 ./p ls: sort
只在控制台上运行 ls
而不是第二个命令。在这一点上,我已经为此工作了大约 3 个小时,我只是通过研究知道答案就在眼前,这很简单,因为它与关闭 fd 或重新安排它们有关order 因为程序执行第一个命令没有任何问题,它是挂断的第二部分。请记住在我的代码中我有 fd[0] setup as rd and fd[1] setup as wt (for reading and write for the sake of simplicity).
我再次知道该问题有一个简单的解决方案,因为我已将错误范围缩小到此代码位置:
if (strcmp(argAP,"Failed to execute \n");
}
exit(1);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)