gpt4 book ai didi

c - UNIX shell I/O 重定向

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:08 25 4
gpt4 key购买 nike

<分区>

我正尝试在 C 中实现 LINUX shell,但是当我尝试运行这样的命令时:sort -u < in.txt > out.txt它说:sort: cannot read: '<': No such file or directory .我还尝试了其他命令,如 ls -l > out.txt等等,但它一直告诉我同样的事情。这是我的代码:

#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>


#define LENGTH 1024

int main(){
char line[LENGTH];
char* argv[100];
char* path= "/bin/";
char fullPath[30];
int option;
char* inputFile;
char* outputFile;

while(1){

printf("mysh3>");

if(!fgets(line,LENGTH,stdin)){
break;
}

size_t length = strlen(line);
if(line[length-1]=='\n')
line[length-1] = '\0';

}

char* token;
token = strtok(line," ");
int i = 0;
while(token!=NULL){
argv[i]=token;
token = strtok(NULL," ");
i++;
}

if(argv[1]==">"){
option=1;
outputFile=argv[2];
}
else if(argv[2]==">"){
option = 1;
outputFile=argv[3];
}
else if(argv[1]=="<" && argv[3]==">"){
option=2;
inputFile=argv[2];
outputFile=argv[4];
}
else if(argv[2]=="<" && argv[4]==">"){
option=2;
inputFile=argv[3];
outputFile=argv[5];
}
else if(argv[1]=="<" && argv[3]==">>"){
option=3;
inputFile=argv[2];
outputFile=argv[4];
}
else if(argv[2]=="<" && argv[4]==">>"){
option=3;
inputFile=argv[3];
outputFile=argv[5];
}


argv[i]=NULL;

strcpy(fullPath, path);
strcat(fullPath, argv[0]);

for(int i = 0; i < strlen(fullPath); i++){
if(fullPath[i]=='\n'){
fullPath[i]='\0';
}
}

int ret=forkEx2(argv,inputFile,outputFile,fullPath,option);
if(ret==-1) {
return -1;
}

int forkEx2(char* argv[],char* inputFile,char* outputFile,char
fullPath[],int option){
int fd;
pid_t pid,waitPid;
pid = fork();
if(pid<0){
perror("ERROR: Fork failed.\n");
return -1;
}
else if(pid==0){
if(option==1){
fd=open(outputFile,O_CREAT | O_TRUNC | O_WRONLY, 0600); //if the file does not exist the system create it. Clear all data from the file. Open the file for write only.Store its file descriptor in fd

dup2(fd,STDOUT_FILENO); //replace the standar out with the file
close(fd);
}
else if(option==2){
fd=open(inputFile,O_RDONLY,0600); //open the file for read only

dup2(fd,STDIN_FILENO); //replace the standar in with the file
close(fd);

fd=open(outputFile,O_CREAT | O_TRUNC | O_WRONLY, 0600); //if the file does not exist the system create it. Clear all data from the file. Open the file for write only

dup2(fd,STDOUT_FILENO); //replace the standar out with the file
close(fd);
}
else if(option==3){
fd=open(inputFile,O_RDONLY,0600); //open the file for read only

dup2(fd,STDIN_FILENO); //replace the standar in with the file
close(fd);

fd=open(outputFile, O_APPEND | O_WRONLY, 0600); //append at the end of the file. Open the file for write only

dup2(fd,STDOUT_FILENO); //replace the standar out with the file
close(fd);
}

execvp(fullPath,argv);
perror("ERROR: Child should never arrive here.\n");

}

else{
waitPid = wait(&waitPid);
if (waitPid == -1) {
perror("ERROR: Waitpid failed.\n");
return -1;
}

printf("Parent: Finished pid %d.\n", waitPid);
}
}

}

感谢任何帮助。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com