- 论坛徽章:
- 0
|
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <error.h>
const char MAXLINE = 80;
//extern char ** environ;
char * environ[] = {"PATH=/usr/bin",(char * )0};
int main(int argc , char * argv[])
{
int status;
char buf[MAXLINE];
pid_t pid;
printf("myshell%%");
while (fgets(buf,MAXLINE,stdin)!=NULL)
{
if (strlen(buf)==1)
continue;
if ( (pid = fork()) < 0 )
{
printf("%s",argv[0]);// 注意这一行,没有\n
perror(":fork error! ");
exit(1) ;
}
else if (pid == 0)
{
printf("to exec %s\n",buf);
execlp(buf,buf,(char *)0);
printf("%s",argv[0]);
perror(":execlp error1! ");
printf("%s\n",argv[0]);
perror(":execlp error2! ");
printf("%s\n",argv[0]);
perror(":execlp error3! ");
exit(1) ;
}
if ( (waitpid(pid,&status,0)) < 0 )
{
printf("%s",argv[0]);
perror(":waitpid error! ");
exit(1) ;
}
printf("myshell%%");
}
return 0;
} |
执行结果如下:
myshell% ls
to exec ls
:execlp error1! : No such file or directory
./a.out./a.out
:execlp error2! : Illegal seek
./a.out
:execlp error3! : Illegal seek
两个./a.out打印到了一起,而且第一个a.out在perror之后
这是为什么呢? |
|