2018-08-15 04:07:33 +03:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2018-02-25 12:22:44 +03:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2018-05-01 11:12:56 +03:00
|
|
|
* Copyright (C) 2013-2018 K. Lange
|
2018-08-15 04:07:33 +03:00
|
|
|
*
|
|
|
|
* cat - Concatenate files
|
2018-02-25 12:22:44 +03:00
|
|
|
*
|
|
|
|
* Concatenates files together to standard output.
|
|
|
|
* In a supporting terminal, you can then pipe
|
|
|
|
* standard out to another file or other useful
|
|
|
|
* things like that.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2018-07-18 03:43:39 +03:00
|
|
|
#include <errno.h>
|
2018-03-16 16:40:23 +03:00
|
|
|
#include <sys/stat.h>
|
2018-02-25 12:22:44 +03:00
|
|
|
|
|
|
|
#define CHUNK_SIZE 4096
|
|
|
|
|
|
|
|
void doit(int fd) {
|
|
|
|
while (1) {
|
|
|
|
char buf[CHUNK_SIZE];
|
|
|
|
memset(buf, 0, CHUNK_SIZE);
|
|
|
|
ssize_t r = read(fd, buf, CHUNK_SIZE);
|
|
|
|
if (!r) return;
|
|
|
|
write(STDOUT_FILENO, buf, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
int ret = 0;
|
|
|
|
if (argc == 1) {
|
|
|
|
doit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
2018-10-29 07:47:05 +03:00
|
|
|
if (!strcmp(argv[i],"-")) {
|
|
|
|
doit(0);
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-25 12:22:44 +03:00
|
|
|
int fd = open(argv[i], O_RDONLY);
|
2018-07-18 03:43:39 +03:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: %s: %s\n", argv[0], argv[i], strerror(errno));
|
2018-02-25 12:22:44 +03:00
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat _stat;
|
|
|
|
fstat(fd, &_stat);
|
|
|
|
|
|
|
|
if (S_ISDIR(_stat.st_mode)) {
|
|
|
|
fprintf(stderr, "%s: %s: Is a directory\n", argv[0], argv[i]);
|
|
|
|
close(fd);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
doit(fd);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|