45 lines
718 B
C
45 lines
718 B
C
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
void cat(const int fd) {
|
|
char buf[8096];
|
|
ssize_t len;
|
|
|
|
while ((len = read(fd, buf, sizeof(buf))) > 0)
|
|
if (write(1, buf, len) != len)
|
|
fprintf(stderr, "cat: write error copying\n");
|
|
}
|
|
|
|
int main(const int argc, const char **argv) {
|
|
if (argc == 1)
|
|
cat(STDIN_FILENO);
|
|
|
|
else {
|
|
for (int i = 1; i < argc; i++) {
|
|
if (argv[i][0] == '-') {
|
|
if (argv[i][1])
|
|
continue;
|
|
|
|
else
|
|
cat(STDIN_FILENO);
|
|
|
|
continue;
|
|
}
|
|
|
|
int fd = open(argv[i], O_RDONLY);
|
|
if (fd < 0) {
|
|
fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
cat(fd);
|
|
close(fd);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|