-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtee.c
More file actions
119 lines (107 loc) · 2.75 KB
/
Copy pathtee.c
File metadata and controls
119 lines (107 loc) · 2.75 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
* tee [ -a ] [ -i ] [ File ... ]
*
* A little implementation of GNU coreutils tee.
*
* Copy standard input to each FILE, and also to standard output.
*
* -a to append to files
* -i to ignore interrupt signals
*
* TODO: more options for handling errors (see --output-error in GNU tee).
* TODO: testing
* TODO: manual buffering
* TODO: use const
* TODO: use errors.h functions
*/
/**
* Prints a formatted message to stderr, prints a friendly version of errno, and then exits with error code 1.
*/
void errorf(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
perror(NULL);
exit(1);
}
/**
* Opens given filenames with provided mode and store the file pointer in the provided array.
*/
void open_files(char *filenames[], char *mode, FILE *file_pointers[], int n) {
int i;
for (i = 0; i < n; i++) {
file_pointers[i] = fopen(filenames[i], mode);
if (file_pointers[i] == NULL) {
errorf("%s could not be opened with mode %s\n", filenames[i], mode);
}
}
}
/**
* Streams stdin to n file pointers.
*/
void stream_files(FILE *file_pointers[], int n) {
int b, i;
// This should be buffered... is there a generally "good" constant?
while ((b = getchar()) != EOF) {
for (i = 0; i < n; i++) {
if (fputc(b, file_pointers[i]) == EOF) {
errorf("Failed writing to %s\n", file_pointers[i]);
}
}
}
}
/**
* Closes n file pointers, ignoring any errors.
*/
void close_files(FILE *file_pointers[], int n) {
int i;
for (i = 0; i < n; i++) {
fclose(file_pointers[i]);
}
}
/**
* Registers a handler for SIGINT that ignores the signal.
*/
void ignore_sigint() {
struct sigaction action;
action.sa_handler = SIG_IGN; // Handler to ignore signal.
sigaction(SIGINT, &action, NULL);
}
/**
* TODO: testing
* Some techniques that seem promising if I wanted to spend more time on testing:
* - Redirecting stdin and stdout with `freopen` for the core of the program
* - Using the same `freopen` technique to test appending
* - Using `raise` to test ignoring interrupts
*/
void test() {}
int main(int argc, char *argv[]) {
char opt;
char *mode = "w";
FILE *file_pointers[argc + 1];
while ((opt = getopt(argc, argv, "ai")) != EOF) {
switch(opt) {
case 'a':
mode = "a";
break;
case 'i':
ignore_sigint();
break;
default:
errorf("Unknown option %c\n", opt);
}
}
argc -= optind;
argv += optind;
file_pointers[0] = stdout; // Always include stdout.
open_files(argv, mode, file_pointers + 1, argc);
stream_files(file_pointers, argc + 1);
close_files(file_pointers + 1, argc); // Don't close stdout.
return 0;
}