2014-06-08 10:13:29 +04:00
|
|
|
/* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2013-2014 Kevin Lange
|
|
|
|
*/
|
2012-01-23 22:36:59 +04:00
|
|
|
/*
|
|
|
|
* cat
|
2012-07-07 08:08:28 +04: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.
|
2012-01-23 22:36:59 +04:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2013-07-27 11:18:29 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2014-05-28 08:38:30 +04:00
|
|
|
#include <string.h>
|
2012-01-23 22:36:59 +04:00
|
|
|
|
|
|
|
#define CHUNK_SIZE 4096
|
|
|
|
|
2013-07-27 11:18:29 +04:00
|
|
|
void doit(int fd) {
|
|
|
|
while (1) {
|
|
|
|
char buf[CHUNK_SIZE];
|
2014-05-28 08:38:30 +04:00
|
|
|
memset(buf, 0, CHUNK_SIZE);
|
|
|
|
ssize_t r = read(fd, buf, CHUNK_SIZE);
|
2013-07-27 11:18:29 +04:00
|
|
|
if (!r) return;
|
2014-05-28 08:38:30 +04:00
|
|
|
write(STDOUT_FILENO, buf, r);
|
2013-07-27 11:18:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 22:36:59 +04:00
|
|
|
int main(int argc, char ** argv) {
|
2012-02-17 08:43:41 +04:00
|
|
|
int ret = 0;
|
2013-07-27 11:18:29 +04:00
|
|
|
if (argc == 1) {
|
|
|
|
doit(0);
|
|
|
|
}
|
2012-02-17 08:43:41 +04:00
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
2013-07-27 11:18:29 +04:00
|
|
|
int fd = open(argv[i], O_RDONLY);
|
2014-04-02 09:59:29 +04:00
|
|
|
if (fd == -1) {
|
2013-07-27 11:18:29 +04:00
|
|
|
fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[i]);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
2012-01-24 00:24:23 +04:00
|
|
|
}
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2013-03-15 11:52:09 +04:00
|
|
|
struct stat _stat;
|
2013-07-27 11:18:29 +04:00
|
|
|
fstat(fd, &_stat);
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2013-03-18 11:52:12 +04:00
|
|
|
if (S_ISDIR(_stat.st_mode)) {
|
|
|
|
fprintf(stderr, "%s: %s: Is a directory\n", argv[0], argv[i]);
|
2013-07-27 11:18:29 +04:00
|
|
|
close(fd);
|
2013-03-18 11:52:12 +04:00
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-15 11:52:09 +04:00
|
|
|
|
2013-07-27 11:18:29 +04:00
|
|
|
doit(fd);
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2013-07-27 11:18:29 +04:00
|
|
|
close(fd);
|
2012-02-17 08:43:41 +04:00
|
|
|
}
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2012-02-17 08:43:41 +04:00
|
|
|
return ret;
|
2012-01-23 22:36:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vim:tabstop=4
|
|
|
|
* vim:noexpandtab
|
|
|
|
* vim:shiftwidth=4
|
|
|
|
*/
|