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>
|
|
|
|
|
|
|
|
#define CHUNK_SIZE 4096
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
2012-02-17 08:43:41 +04:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
FILE * fd;
|
|
|
|
if (argc > 1) {
|
|
|
|
fd = fopen(argv[i], "r");
|
|
|
|
if (!fd) {
|
|
|
|
fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[1]);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-24 00:24:23 +04:00
|
|
|
}
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2012-02-17 08:43:41 +04:00
|
|
|
size_t length;
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2012-02-17 08:43:41 +04:00
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
length = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2012-02-17 08:43:41 +04:00
|
|
|
char buf[CHUNK_SIZE];
|
|
|
|
while (length > CHUNK_SIZE) {
|
|
|
|
fread( buf, 1, CHUNK_SIZE, fd);
|
|
|
|
fwrite(buf, 1, CHUNK_SIZE, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
length -= CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
if (length > 0) {
|
|
|
|
fread( buf, 1, length, fd);
|
|
|
|
fwrite(buf, 1, length, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2012-01-23 22:36:59 +04:00
|
|
|
|
2012-02-17 08:43:41 +04:00
|
|
|
fclose(fd);
|
|
|
|
}
|
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
|
|
|
|
*/
|