#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]);// 注意這一行,沒有\(zhòng)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; } |
原帖由 皇家救星 于 2009-4-30 12:33 發(fā)表
一個(gè)是標(biāo)準(zhǔn)輸出,一個(gè)是標(biāo)準(zhǔn)錯(cuò)誤輸出
標(biāo)準(zhǔn)輸出一般有緩沖,標(biāo)準(zhǔn)錯(cuò)誤輸出一般沒有
printf碰上\n后會刷新緩沖
以上均是經(jīng)驗(yàn),沒找到標(biāo)準(zhǔn)支持
歡迎光臨 Chinaunix (http://72891.cn/) | Powered by Discuz! X3.2 |