diff options
Diffstat (limited to 'src/cmd/cat/cat.c')
| -rwxr-xr-x | src/cmd/cat/cat.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/cmd/cat/cat.c b/src/cmd/cat/cat.c new file mode 100755 index 0000000..1c3e40a --- /dev/null +++ b/src/cmd/cat/cat.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stdbool.h> + +void fileoutput(FILE *, const char *); +bool isdir(const char *); + +/* concatinate files to a file stream */ +int main(int argc, char **argv) +{ + FILE *fp; + if (argc == 1) { + fileoutput(stdin, "stdin"); + } else { + while (--argc > 0 && !ferror(stdout)) { + if (isdir(*++argv)) { + perror("cat"); + continue; + } + if ((fp = fopen(*argv, "r")) == NULL) { + perror("cat"); + continue; + } + fileoutput(fp, *argv); + } + } + if (ferror(stdout)) { + perror("cat"); + return -1; + } + return 0; +} + +/* fileoutput: output a file to stdout */ +void fileoutput(FILE *fp, const char *name) +{ + int c; + while ((c = getc(fp)) != EOF) + putc(c, stdout); + if (ferror(fp)) { + perror("cat"); + } + fclose(fp); +} + +/* isdir: return true if the file as the path is a directory */ +bool isdir(const char *path) +{ + struct stat sbuf; + stat(path, &sbuf); + return S_ISDIR(sbuf.st_mode); +} + |
