toaruos/userspace/core/cat.c

56 lines
963 B
C
Raw Normal View History

/*
* cat
*
* 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>
2013-03-15 11:52:09 +04:00
#include <sys/stat.h>
#define CHUNK_SIZE 4096
int main(int argc, char ** argv) {
int ret = 0;
for (int i = 1; i < argc; ++i) {
FILE * fd;
if (argc > 1) {
fd = fopen(argv[i], "r");
if (!fd) {
2013-03-18 11:52:12 +04:00
fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[i]);
ret = 1;
continue;
}
}
2013-03-15 11:52:09 +04:00
struct stat _stat;
fstat(fileno(fd), &_stat);
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]);
fclose(fd);
ret = 1;
continue;
}
2013-03-15 11:52:09 +04:00
2013-03-18 11:52:12 +04:00
while (!feof(fd)) {
2013-03-15 11:52:09 +04:00
char buf[CHUNK_SIZE];
2013-03-18 11:52:12 +04:00
int read = fread(buf, 1, CHUNK_SIZE, fd);
fwrite(buf, 1, read, stdout);
}
2013-03-18 11:52:12 +04:00
fflush(stdout);
fclose(fd);
}
return ret;
}
/*
* vim:tabstop=4
* vim:noexpandtab
* vim:shiftwidth=4
*/