-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirecting-std-output.c
72 lines (67 loc) · 1.74 KB
/
redirecting-std-output.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
/*
File descriptors:
They are unique across a file.
They are things we can read from or write to.
fd 0 is automatically linked to stdin
fd 1 is automatically linked to stdout
fd 2 is automatically linked to stderr
lets take fd 3 linked to pingResults
this has to be then directed to stdout
*/
int main(int *argc, char *argv)
{
int pid = fork();
if (pid == -1)
{
printf("Failed fork()\n");
return 1;
}
if (pid == 0)
{
// child
// open the file to write into
int file = open("pingResults.txt", O_WRONLY | O_CREAT /*creates file if doesnt exist*/, 0777 /*permissions*/);
if (file == -1)
{
return 2;
}
// dup duplicates by adding another fd linked to specified file
// dup2 links the fd specified to the specified file
// in this case, the stdout (fd - 1)
printf("fd to pingResults: %d\n", file);
dup2(file, STDOUT_FILENO);
close(file);
int err = execlp("ping", "ping", "-c", "3", "google.com", NULL);
if (err == -1)
{
printf("Could not find program to execute\n");
}
printf("This will not show up on theterminal\n");
}
else
{
int wstatus;
wait(&wstatus);
if (WIFEXITED(wstatus))
{
int statusCode = WEXITSTATUS(wstatus);
if (statusCode == 0)
{
printf("Success\n");
}
else
{
printf("Failure with status code %d\n", statusCode);
}
}
}
return 0;
}