-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcat.c
40 lines (29 loc) · 761 Bytes
/
cat.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
// cat - read and output files
#include <stdio.h>
#include "constants.h"
void output_file(FILE *in, FILE *out);
int main(int argc, char **argv) {
int arg_count = argc;
char **arguments = argv;
if (arg_count == 1) {
output_file(stdin, stdout);
return 0;
}
FILE *read_file;
while (--arg_count > 0) {
read_file = fopen(*++arguments, "r");
if (read_file == NULL) {
perror(*arguments);
return 1;
}
output_file(read_file, stdout);
fclose(read_file);
} // end while
}
void output_file(FILE *in, FILE *out) {
static char buffer[MAX_STRING_SIZE];
size_t size; // Number of bytes read by fread()
while ( (size = fread(buffer, 1, MAX_STRING_SIZE, in) ) != 0) {
fwrite(buffer, 1, size, out);
}
}