|
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdio.h>
- #define MAXLINE 4096 /* max file length */
- #define DEF_PAGER "/bin/more" /* default pager program */
- int
- main(int argc,char **argv)
- {
- int n,fd[2];
- pid_t pid;
- char line[MAXLINE],*pager,*argv0;
- FILE *fp;
- if(argc != 2) {
- printf(" usage: a.out <PATHNAME> \n");
- exit(0);
- }
- if((fp=fopen(argv[1],"r")) == NULL ) {
- printf("fopen %s error\n",argv[1]);
- exit(0);
- }
- if(pipe(fd)<0) {
- printf("pipe error\n");
- exit(0);
- }
- if((pid=fork())<0) {
- printf("fork error\n");
- exit(0);
- }else if (pid>0) { /* parent */
- close(fd[0]); /* close read end */
- /* parent copies argv[1] to pipe */
- while(fgets(line,MAXLINE,fp) != NULL ) {
- n=strlen(line);
- if(write(fd[1],line,n) != n) {
- printf("write error\n");
- exit(0);
- }
- }
- if(ferror(fp)) {
- printf("fgets error\n");
- exit(0);
- }
- close(fd[1]); /* close write end of piep for reader */
- if(waitpid(pid,NULL,0)<0) {
- printf("waitpid error\n");
- exit(0);
- }
- exit(0);
- }else{ /* child */
- close(fd[1]); /* close write end */
- if(fd[0] != STDIN_FILENO) {
- if(dup2(fd[0],STDIN_FILENO) != STDIN_FILENO) {
- printf("dup2 error to stdin\n");
- exit(0);
- }
- close(fd[0]);
- }
- /* get arguments for execl() */
- if( ( pager = getenv ( "PAGER" )) == NULL )
- pager=DEF_PAGER;
- if((argv0=strrchr(pager,'/')) != NULL )
- argv0++; /* step past rightmost slash */
- else
- argv0=pager; /* no slash in pager */
- if(execl(pager,argv0,(char *) 0) <0) {
- printf("execl() error for %s\n",pager);
- }
- }
- exit(0);
- }
- 14.2.c: In function `main':
- 14.2.c:40: warning: passing arg 1 of `ferror' from incompatible pointer type
- 14.2.c:60: warning: assignment makes pointer from integer without a cast
复制代码
上面的程序错误已改正。程序下面的WARNING将不存在。
现在懂了在40行错了是fp 不是fd ferror参数的类型是FILE * ,不是fd, |
|