2023-10-03 23:09:17 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-10-04 12:21:13 +03:00
|
|
|
void cat(const int fd) {
|
2023-11-04 17:55:02 +03:00
|
|
|
char buf[8096];
|
2023-10-03 23:09:17 +03:00
|
|
|
ssize_t len;
|
|
|
|
|
2023-10-30 18:09:33 +03:00
|
|
|
while ((len = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
if (write(1, buf, len) != len)
|
|
|
|
fprintf(stderr, "cat: write error copying\n");
|
2023-10-03 23:09:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(const int argc, const char **argv) {
|
2023-11-04 17:55:02 +03:00
|
|
|
if (argc == 1 || argv[1][0] == '-')
|
2023-10-04 12:21:13 +03:00
|
|
|
cat(STDIN_FILENO);
|
2023-10-03 23:09:17 +03:00
|
|
|
|
|
|
|
else {
|
2023-10-30 18:09:33 +03:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] == '-')
|
|
|
|
break;
|
|
|
|
|
2023-10-03 23:09:17 +03:00
|
|
|
int fd = open(argv[i], O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:21:13 +03:00
|
|
|
cat(fd);
|
|
|
|
close(fd);
|
2023-10-03 23:09:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|