次のリダイレクト オプション ">、>>、<、|" を実行する独自のシェルを作成しています。パイプを使用。しかし、この奇妙なエラーが発生し続けます。コマンド ps -ax > processes.txt を試すと、次のいずれかが表示されます。
Error: Must set personality to get -x option
また
Error: Garbage option
これまでに書いたコードは次のとおりです。
for (i=0; i<nargs; i++)
{
//> redirect output to output file
if((strcmp(args[i], ">")==0))
{ //printf("Forking.\n");
pid = fork();
//printf("Forked\n");
if(pid == 0)
{ /* child process */
const char* file = args[i+1];
printf("entered child process\n");
int fd;
if((fd=creat(file,O_WRONLY| O_TRUNC | O_CREAT ))==-1)
{
fprintf(stderr,"Couldn't open %s\n", args[i+1]);
exit(-1);
}//this works
else
{fd=creat(file,O_WRONLY | O_TRUNC | O_CREAT);
dup2(fd,1);
close(fd);
args[i]= 0;
execvp(args[0], args);
perror("exec failed\n");
exit(-1);}
}
else if(pid > 0)
{ /* parent process */
if(!async) waitpid(pid, NULL, 0);
else printf("this is an async call\n");
//execvp(args[i+1], args);
}
else { /* error occurred */
perror("fork failed\n");
exit(1);
}
printf("Child complete\n");
}//>> redirect output to output file does not overwrite just writes to the end of the file
else if((strcmp(args[i], ">>")==0))
{
printf("Forking.\n");
pid = fork();
printf("Forked\n");
if(pid == 0)
{ /* child process */
const char* file = args[i+1];
printf("entered child process\n");
int fd;
if((fd=open(file,O_WRONLY | O_CREAT | O_APPEND))==-1)
{
fprintf(stderr,"Couldn't open %s\n", args[i+1]);
exit(-1);
}
fd=open(file,O_WRONLY | O_CREAT | O_APPEND);
dup2(fd,1);
close(fd);
args[i]=NULL;
//printf("%d", fd);
execvp(args[0], args);
/* return only when exec fails */
perror("exec failed\n");
exit(-1);
}
else if(pid > 0)
{ /* parent process */
if(!async) waitpid(pid, NULL, 0);
else printf("this is an async call\n");
}
else { /* error occurred */
perror("fork failed\n");
exit(1);
}
printf("Child complete\n");
}//input comes from a file
else if((strcmp(args[i], "<")==0))
{
printf("Forking.\n");
pid = fork();
printf("Forked\n");
if(pid == 0)
{ /* child process */
const char* file = args[i+1];
printf("entered child process\n");
int fd;
if((fd=open(file,O_RDONLY)==-1))
{
fprintf(stderr,"File does not exist %s\n", args[i+1]);
exit(-1);
}
fd=open(file,O_RDONLY);
dup2(fd,0);
close(fd);
args[i]=NULL;
execvp(args[0], args);
/* return only when exec fails */
perror("exec failed\n");
exit(-1);
}
else if(pid > 0)
{ /* parent process */
if(!async) waitpid(pid, NULL, 0);
else printf("this is an async call\n");
}
else { /* error occurred */
perror("fork failed\n");
exit(1);
}
printf("Child complete\n");
}//| output is input for another command
else if((strcmp(args[i], "|")==0))
{//the mypipe code edited
printf("Forking.\n");
pid = fork();
printf("Forked\n");
if(pid == 0)
{ const char* file = args[i+1];
printf("entered child process\n");
int fd;
if((fd=open(file,O_WRONLY | O_CREAT | O_TRUNC))==-1)
{
fprintf(stderr,"Couldn't open %s\n", args[i+1]);
exit(-1);
}//this works
fd=open(file,O_WRONLY | O_CREAT | O_TRUNC);
dup2(fd,1);
close(fd);
//close(fd);
args[i]=NULL;
execvp(args[0], args);
/* return only when exec fails */
perror("exec failed\n");
exit(-1);
}
else if(pid > 0)
{ /* parent process */
if(!async) waitpid(pid, NULL, 0);
else printf("this is an async call\n");
}
else { /* error occurred */
perror("fork failed\n");
exit(1);
}
printf("Child complete\n");
}//