0
유닉스/리눅스 쉘의 간접 입력 기능을 구현하기위한 간단한 코드를 작성했습니다. 문제가 무엇인지 제발, 그냥 내가 답변을주지 않고이 붙어간접 입력이있는 단순한 쉘
sort -r <fileList
는 쉘
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
extern void error(char* message);
void
cisshRedirectedInput(char* command[], char* inputFile)
{
//Try to implement the RedirectInput from here
pid_t pid;
int status;
int fd;
//For the child process
if ((pid=fork())==0)
{
//Try to input files, failing on an error
fd=open(inputFile,O_RDONLY);//To read input file
if(fd < 0)
{
error("sampleSh: error opening standard input file");
exit(1);
}
//use dup() to copy file
close(1);
if(dup(fd) < 0)
{
error("sampleSh: error duplicating standard input");
perror("dup()");
exit(1);
}
//Close file and exec()
close(fd);
execvp(command[0], command);
//If failure in any case
error("sampleSh: failure to execute command");
exit(1);
}
else
{
/* This is the parent process.
* Wait for the child to terminate.
*/
if(wait(&status) < 0)
{
error("sampleSh: error waiting for child.");
perror("wait");
}
if(status != 0)
error("sampleSh: command exited with nonzero error status.");
}
}
그러나, 컴파일 후 (오류가보고되지),하지만 난 때 (파일 목록이 이미 생성)?
고마워, 그것을 확인합니다. –
안녕하세요 조나단, 답장을 보내 주셔서 대단히 감사합니다. 나는 당신이 지적한대로 오류를 수정했다. 그러나 이러한 오류는 여전히 나옵니다. "sort : fflush failed : 표준 출력 : 잘못된 파일 설명자" "sort : write error", 이유는 무엇입니까? –
고마워요. 그것은 모든 것을 해결합니다. 방금 오자가 있었어. –