p1.c |
wcat.c |
#include < stdio.h > #include < fcntl.h > main(int argc, char **argv) { int k, p[2]; char *a1, *a2; a1 = NULL; a2 = NULL; open("f1", O_RDONLY); open("f2", O_RDONLY); open("fout-1", O_WRONLY | O_CREAT | O_TRUNC, 0666); open("fout-2", O_WRONLY | O_CREAT | O_TRUNC, 0666); pipe(p); for (k = 1; k < argc; k++) { if (strcmp(argv[k], "d1") == 0) { dup(atoi(argv[k+1])); k++; } else if (strcmp(argv[k], "d2") == 0) { dup2(atoi(argv[k+1]), atoi(argv[k+2])); k += 2; } else if (strcmp(argv[k], "f") == 0) { if (fork() == 0) { while(strcmp(argv[k], "e") != 0) k++; } } else if (strcmp(argv[k], "e") == 0) { k = argc; } else if (strcmp(argv[k], "c") == 0) { close(atoi(argv[k+1])); k++; } else { a1 = argv[k]; a2 = argv[k+1]; k++; } } execlp("wcat", "wcat", a1, a2, NULL); printf("Issues\n"); } |
#include < stdio.h > main(int argc, char **argv) { int fd1; int fd2; char c; int j; fd1 = atoi(argv[1]); fd2 = atoi(argv[2]); while (1) { j = read(fd1, &c, 1); if (j == 0) exit(0); if (j != 1) { fprintf(stderr, "bad read from %d\n", fd1); exit(1); } j = write(fd2, &c, 1); if (j != 1) { fprintf(stderr, "bad write to %d\n", fd2); exit(1); } } } |
|
Now, wcat.c and p1.c have been compiled into wcat and p1 respectively. Below are 17 commands typed in from the shell. For each command, circle the appropriate output for files fout-1, fout-2, fout-3, and the screen. Use the list of possible outputs below. |
Possible Outputs |
Commands | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Assumptionsint dup2(int orig, int dest) |
typedef struct flist { int size; int checksum; struct flist *flink; struct flist *blink; } Flist;
Write a version of free() that works with the above version of malloc(). Your free() should: