-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.c
More file actions
32 lines (24 loc) · 654 Bytes
/
cat.c
File metadata and controls
32 lines (24 loc) · 654 Bytes
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
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 65536
static char buffer[BUFFER_SIZE];
int main(int argc, char **argv) {
int fd, i;
ssize_t bytes_read;
if (argc < 2) {
while ((bytes_read = read(0, buffer, BUFFER_SIZE)) > 0) {
write(1, buffer, bytes_read);
}
return 0;
}
for (i = 1; i < argc; i++) {
fd = open(argv[i], O_RDONLY);
if (fd == -1) return 1;
while ((bytes_read = read(fd, buffer, BUFFER_SIZE)) > 0) {
write(1, buffer, bytes_read);
}
close(fd);
}
return 0;
}