-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforkcat2.c
More file actions
41 lines (32 loc) · 823 Bytes
/
forkcat2.c
File metadata and controls
41 lines (32 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Forkcat2.c */
#include <stdio.h>
#include <fcntl.h>
/* This program fork off a process that calls
cat /home/plank/cs360/lab9/forkcat1.c
and redirects the output to the file "catoutput". This is just like
forkcat1.c except that it first opens the output file as file descriptor 1.
*/
main(int argc, char **argv)
{
int status;
char **newargv;
int fd;
newargv = (char **) malloc(sizeof(char *)*3);
newargv[0] = "cat";
newargv[1] = "/home/plank/cs360/notes/Dup/dupex2.c";
newargv[2] = NULL;
if (fork() == 0) {
fd = open("catoutput", O_WRONLY | O_TRUNC | O_SYNC | O_CREAT, 0666);
if (fd < 0) {
perror("forkcat1: catoutput");
exit(1);
}
dup2(fd, 1);
close(fd);
execvp("cat", newargv);
perror("forkcat1");
exit(1);
} else {
wait(&status);
}
}