[英]C program to execute shell piping and redirection
somewhat new to this. 对此有些新意。 I am writing a C program that allows the user to input UNIX commands such as
我正在编写一个C程序,允许用户输入UNIX命令,例如
ls –la | wc -l and ls | head > file.txt
I have the piping implemented, and a small amount of the redirection code finished. 我已经实现了管道,并且完成了少量的重定向代码。 currently, I can only execute commands like cat file1.txt > file2.txt , or other commands that END with redirection.
目前,我只能执行cat cat1.txt> file2.txt之类的命令,或者其他以重定向结束的命令。
If I comment out this segment - all chains of piping works fine. 如果我对此部分进行注释-所有管道链条都可以正常工作。
char * srch;
srch = strchr(cmd, '>');
*srch++ = '\0';
while(*srch == ' ')
{
++srch;
}
if(srch)
{
dup2(open(srch, O_RDWR|O_CREAT), STDOUT_FILENO);
}
my problem arises when I try to combine both piping and redirection into the same command such as cmd1 p1 p2 p3 < file1 | 当我尝试将管道传输和重定向合并到同一命令(例如cmd1 p1 p2 p3 <file1 | cmd2 > file2.
cmd2> file2。 Or even simpler commands such as ls |
甚至更简单的命令,例如ls | head > file.txt
头> file.txt
can anyone help me to implement the ability to add redirection AND piping in the same command. 谁能帮助我实现在同一命令中添加重定向和管道的功能。 I've been trying but I can't figure it out.
我一直在尝试,但无法解决。
here's what I have so far: 这是我到目前为止的内容:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
/*get args function*/
#define MAXARGS 256
char ** getargs(char * cmd) {
// assumes that cmd ends with NULL
char** argsarray;
int nargs = 0;
int nlen = strlen(cmd);
int i = 0;
argsarray = (char**) malloc(sizeof(char*) * MAXARGS);
argsarray[0] = strtok(cmd," ");
i = 0;
while (argsarray[i] != NULL){
i++;
argsarray[i] = strtok(NULL," ");
}
return argsarray;
}
int main(void){
pid_t childpid;
int fd[256][2];
char cmd[256];
char * sepCmd[256];
char * pch;
printf("Please enter a command sequence: \n");
gets(cmd);
//scanf("%s", cmd);
printf("You have entered: %s \n", cmd);
printf("Attempting to split up command: \n");
pch = strtok (cmd, "|");
//problems here...
char * srch;
srch = strchr(cmd, '>');
*srch++ = '\0';
while(*srch == ' ')
{
++srch;
}
if(srch)
{
dup2(open(srch, O_RDWR|O_CREAT), STDOUT_FILENO);
}
int count = 0;
while (pch != NULL && count < 256) {
printf("%s\n", pch);
sepCmd[count] = pch;
printf("The value in this array value is: %s\n", sepCmd[count]);
pch = strtok (NULL, "|");
count++;
}
char ** argue;
int k;
/* Block that deals with the first command given by the user */
k = 0;
pipe(fd[k]);
if(!fork()) {
dup2(fd[k][1], STDOUT_FILENO);
close(fd[k][0]);
argue = getargs(sepCmd[k]);
execvp(argue[0], argue);
perror(argue[0]);
exit(0);
}
/*Loop that will control all other comands except the last*/
for(k = 1; k <= count - 2; k++) {
close(fd[k-1][1]);
pipe(fd[k]);
if(!fork()) {
close(fd[k][0]);
dup2(fd[k-1][0], STDIN_FILENO);
dup2(fd[k][1], STDOUT_FILENO);
argue = getargs(sepCmd[k]);
execvp(argue[0], argue);
perror(argue[0]);
exit(0);
}
}
/*Block that will take care of the last command in the sequence*/
k = count - 1;
// if(reDir){
//argue = getargs(sepCmd[k]);
//open(argue[count], O_RDWR);
//if(!fork()){
// close(fd[0]);
// close(fd[1]);
// dup2(fd[k-1][0], STDOUT_FILENO);
// execl("/bin/>", argue[count]);}
//}
//else{
close(fd[k-1][1]);
if(!fork()) {
dup2(fd[k-1][0], STDIN_FILENO);
argue = getargs(sepCmd[k]);
execvp(argue[0], argue);
perror(argue[0]);
exit(0);
}
// }
while(waitpid(-1, NULL, 0) != -1);
}
Commented code is wrong. 注释的代码是错误的。 You are looking for '>' (
srch = strchr(cmd, '>')
) and without checking result, terminating expected result with '\\0'. 您正在寻找'>'(
srch = strchr(cmd, '>')
),而没有检查结果,以'\\ 0'终止期望的结果。 So when there is no redirection user gets Segmentation fault momentarily, and when there is also nothing good happens. 因此,在没有重定向的情况下,用户会暂时出现细分错误,在没有任何好处的情况下,也会发生此情况。 Btw I would suggest getting rid of
gets
as it's very deprecated. 顺便说一句,我建议摆脱
gets
因为它已经过时了。 You will get implicit declaration of function 'gets'
when compiling with --std=gnu11
. 使用
--std=gnu11
编译时,将implicit declaration of function 'gets'
。 As for tips about implementation, looking in google shows many nice results like: https://github.com/dan-f/my_shell or maybe https://github.com/jmreyes/simple-c-shell so you can check how they have done it. 至于实现的技巧,在google中查看会显示许多不错的结果,例如: https : //github.com/dan-f/my_shell或https://github.com/jmreyes/simple-c-shell,以便您可以查看如何他们做到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.