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