-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_1.c
98 lines (88 loc) · 2.33 KB
/
4_1.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
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
#include <tlpi_hdr.h>
#include <fcntl.h>
#define MAXLINE 4096
int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
int main(int argc, char **argv)
{
int opt = 0;
int is_append = 0;
int tee_fd[MAXLINE] = {0};
int fd_no = 0;
char temp[MAXLINE] = {0};
ssize_t n = 0;
// read the command line arguments
// If there are no more option characters, getopt() returns -1.
while ((opt = getopt(argc, argv, "a-:")) != -1)
{
switch (opt)
{
case 'a':
is_append = 1;
break;
case '-':
if (strcmp(optarg, "help") == 0)
{
printf(
"Usage: tee [Option]... [File]...\n"
"Copy standard input to each file, and also to standard output.\n"
"-a append to the given FILEs, do not overwrite.\n");
}
exit(0);
case '?':
default:
exit(0);
}
}
// The variable optind is the index of the next element to be
// processed in argv. The system initializes this value to 1.
for (int i = optind; i < argc; i++, fd_no++)
{
if (is_append)
{
// open all the files, which is non-option arguments.
tee_fd[fd_no] = open(argv[i], O_CREAT | O_WRONLY | O_APPEND, 0666);
if (tee_fd[fd_no] < 0)
{
exit(1);
}
}
else
{
// open all the files, which is non-option arguments.
tee_fd[fd_no] = open(argv[i], O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (tee_fd[fd_no] < 0)
{
exit(1);
}
}
}
// read and write to multiple outputs
while ((n = read(STDIN_FILENO, temp, MAXLINE)) > 0)
{
// write to stdout
if (write(STDOUT_FILENO, temp, n) != n)
{
exit(1);
}
// write to other output files
for (int i = 0; i < fd_no; i++)
{
if ((n = write(tee_fd[i], temp, n)) != n)
{
exit(1);
}
}
}
// failed to read
if (n < 0)
{
exit(1);
}
// close all the files
for (int i = 0; i < fd_no; i++)
{
close(tee_fd[i]);
}
}