#include #include #include #define MAXLINE 10240 static void sig_pipe(int ); int main(int argc, char *argv[]) { int n, counter,fd1[2], fd2[2]; pid_t pid; char line[MAXLINE]; FILE *fp; char temp[MAXLINE]; char tmpfile[MAXLINE]; char tmpfile2[MAXLINE]; if (argc != 3) { printf("Too few arguments:%d\n",argc); printf("usage SUSAN file1 file 2\n"); exit(1); } for (counter=1; counter <3; counter++) { sprintf(tmpfile, "%s.out", argv[counter]); sprintf(tmpfile2, "%s.final", argv[counter]); sprintf(temp, "./hw3.sh < %s > %s", argv[counter], tmpfile); printf("file 1 = %s, file 2 = %s,command = %s\n", tmpfile, tmpfile2, temp); if (signal(SIGPIPE, sig_pipe) == SIG_ERR) printf("signal error\n"); if (pipe(fd1) < 0 || pipe(fd2) < 0) printf("pipe error\n"); if ((pid = fork()) < 0) printf("fork error\n"); else if (pid > 0) { close (fd1[0]); close (fd2[1]); /*system("./hw3.sh < 2.txt > out.txt");*/ system(temp); /*execl("./","test.sh", "<","2.txt",">","out2.txt",(char *) 0);*/ /*printf("file being read is %s\n",argv[counter+1]);*/ /*fp = fopen("out.txt", "r");*/ fp = fopen(tmpfile,"r"); while (fgets(line, MAXLINE, fp) != NULL) { n = strlen(line); /*printf("line from file = %s", line); */ if (write(fd1[1], line, n) != n) printf("write error to pipe\n"); /* if ((n = read(fd2[0], line, MAXLINE)) < 0) printf("read error from pipe\n"); if (n == 0) { printf("child closed pipe\n"); break; } line[n] = 0; if (fputs(line, stdout) == EOF) printf("fputs error\n"); */ } if (ferror(stdin)) printf("fgets error on stdin\n"); /* exit(0); */ } else { close(fd1[1]); close(fd2[0]); if (fd1[0] != STDIN_FILENO) { if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO) printf("dup2 error to stdin\n"); close(fd1[0]); } if (fd2[1] != STDOUT_FILENO) { if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO) printf("dup2 error to stdout\n"); close(fd2[1]); } /* if (execl("/usr/bin/cat", "cat", (char *)0) < 0) if (execl("./hw4","hw4",tmpfile2, (char *) 0) < 0) */ if (execl("./hw4", "hw4", (char *)0) < 0) printf("execl error\n"); } } } static void sig_pipe(int signo) { printf("SIGPIPE caught\n"); exit(1); }