-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls_output.cc
106 lines (83 loc) · 2.06 KB
/
ls_output.cc
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
const char * usage = ""
"Usage:\n"
" ls_output outfile\n"
"\n"
" It does something similar to the shell command:\n"
" csh> ls -l > outfile\n"
"\n"
"Example:\n"
" ls_output outfile\n"
" cat outputfile\n\n";
const char *ls = "ls";
int
main(int argc, char **argv, char **envp)
{
if (argc < 2) {
fprintf(stderr, "%s", usage );
exit(1);
}
// Save default input, output, and error because we will
// change them during redirection and we will need to restore them
// at the end.
// The dup() system call creates a copy of a file descriptor.
int defaultin = dup( 0 );
int defaultout = dup( 1 );
int defaulterr = dup( 2 );
////////////////// ls //////////////////////////
// Input: defaultin
// Output: file
// Error: defaulterr
// Create file descriptor
int outfd = creat( argv[ 1 ], 0666 );
if ( outfd < 0 ) {
perror( "ls : create outfile" );
exit( 2 );
}
// Redirect output to the created utfile instead off printing to stdout
dup2( outfd, 1 );
close( outfd );
// Redirect input
dup2( defaultin, 0 );
// Redirect output to file
dup2( outfd, 1 );
// Redirect err
dup2( defaulterr, 2 );
// Create new process for "ls"
int pid = fork();
if ( pid == -1 ) {
perror( "ls: fork\n");
exit( 2 );
}
if (pid == 0) {
//Child
// close file descriptors that are not needed
close(outfd);
close( defaultin );
close( defaultout );
close( defaulterr );
// You can use execvp() instead if the arguments are stored in an array
execlp(ls, ls, "-l", (char *) 0);
// exec() is not suppose to return, something went wrong
perror( "ls: exec ls");
exit( 2 );
}
// Restore input, output, and error
dup2( defaultin, 0 );
dup2( defaultout, 1 );
dup2( defaulterr, 2 );
// Close file descriptors that are not needed
close( outfd );
close( defaultin );
close( defaultout );
close( defaulterr );
// Wait for last process in the pipe line
waitpid( pid, 0, 0 );
exit( 2 );
}