1 #include <stdio.h> 2 #include <unixio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 int trace = 1; 7 8 extern int piped_child(); 9 extern int piped_child_shutdown(); 10 11 main (argc, argv) 12 int argc; 13 char ** argv; 14 { 15 static char line[512]; 16 char *linep[] = {line, NULL}; 17 int pid; 18 int tofd, fromfd; 19 FILE *in, *out; 20 21 while (1) 22 { 23 printf("\nEnter a command to run: "); 24 line[0] = '\0'; 25 fgets(line, 511, stdin); 26 if (!strlen(line)) 27 exit(2); 28 29 line[strlen(line)-1] = '\0'; 30 pid = piped_child(linep, &tofd, &fromfd); 31 32 in = fdopen(fromfd, "r"); 33 out = fdopen(tofd, "w"); 34 35 #if 0 36 out = fdopen(tofd, "w"); 37 fclose(out); 38 #endif 39 40 do 41 { 42 if(!feof(stdin)) 43 { 44 fprintf(stdout, "> "); 45 fgets(line, 511, stdin); 46 fputs(line, out); 47 } 48 else 49 { 50 fclose(out); 51 close(tofd); 52 } 53 54 fgets(line, 511, in); 55 fputs(line, stdout); 56 line[0] = '\0'; 57 } while (!feof(in)); 58 59 fprintf(stderr, "waiting for child to stop\n"); 60 piped_child_shutdown(pid); 61 } 62 } 63