-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtee.c
43 lines (35 loc) · 893 Bytes
/
tee.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
#include <sys/stat.h>
#include <fcntl.h>
#include "hdr.h"
#ifndef BUF_SIZE /* override -DBUF_SIZE */
#define BUF_SIZE 500
#endif
int main(int argc, char *argv[]) {
int out_fd, open_flags;
mode_t file_params;
ssize_t num_read;
char buf[BUF_SIZE];
if (argc != 2 || strcmp(argv[1],"--help") == 0) {
fprintf(stderr, "%s tee file\n", argv[0]);
return -1;
}
open_flags = O_CREAT | O_WRONLY | O_TRUNC;
file_params = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
out_fd = open(argv[1], open_flags, file_params);
if(out_fd == -1) {
fprintf(stderr, "openning file %s\n", argv[1]);
return -1;
}
for(;;){
memset(buf, '\0', BUF_SIZE);
char* s = fgets(buf, BUF_SIZE, stdin);
if(s == NULL) { /* EOF */
break;
}
if(write(out_fd, buf, sizeof(buf)) == -1) {
fprintf(stderr, "could not write whole buffer\n");
return -1;
}
printf("%s", buf);
}
}