diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-04 10:36:08 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-04 10:36:51 +1300 |
commit | 4b49eed6098881fbde1f6c7728c1346fc8366979 (patch) | |
tree | 7c5a3d7ee9ec01e71ae1e6eea1f59533c0ca4c8d | |
parent | Clean up Makefile a bit (diff) | |
download | cat-4b49eed6098881fbde1f6c7728c1346fc8366979.tar.gz cat-4b49eed6098881fbde1f6c7728c1346fc8366979.zip |
Switch to fopen/fread etc
Not quite finished yet -- this does things with stdin I didn't expect,
like not printing it line-by-line, and requiring two EOFs for some
reason.
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | cat.h | 2 | ||||
-rw-r--r-- | cfn.c | 13 | ||||
-rw-r--r-- | cfp.c (renamed from cfd.c) | 11 | ||||
-rw-r--r-- | main.c | 4 |
5 files changed, 19 insertions, 15 deletions
@@ -1,8 +1,8 @@ CC = gcc CFLAGS = -Wall -Wpedantic -ansi -pedantic-errors -cat : main.o cfn.o cfd.o - $(CC) $(CFLAGS) -o cat main.o cfn.o cfd.o +cat : main.o cfn.o cfp.o + $(CC) $(CFLAGS) -o cat main.o cfn.o cfp.o clean : rm -f -- *.o cat @@ -19,7 +19,7 @@ /* Function prototypes so that I can refer to these functions in main() before * I actually define them */ int cfn(const char *fn, void *buf); -int cfd(int fd, void *buf); +int cfp(FILE *fp, void *buf); #endif @@ -3,10 +3,11 @@ /* Function opens and writes the contents of a named file to stdout; * effectively a wrapper around cfd() */ int cfn(const char *fn, void *buf) { - int fd; + FILE *fp; + int cfpr; /* Open the file to get a read-only file descriptor */ - if ((fd = open(fn, O_RDONLY)) == -1) { + if ((fp = fopen(fn, "r")) == NULL) { perror(__FUNCTION__); return -1; } @@ -14,15 +15,15 @@ int cfn(const char *fn, void *buf) { /* Pass the opened descriptor to cfd() to read it; we keep going even if * there are problems, because we need the descriptor closed even if we * couldn't read it */ - cfd(fd, buf); + cfpr = cfp(fp, buf); /* Close the descriptor, since we should now be done with it */ - if (close(fd) == -1) { + if (fclose(fp) != 0) { perror(__FUNCTION__); return -1; } - /* Done, assume success */ - return 0; + /* Done, return the result of the cfp() call */ + return cfpr; } @@ -1,18 +1,21 @@ #include "cat.h" /* Function writes the contents of an opened file descriptor to stdout */ -int cfd(int fd, void *buf) { +int cfp(FILE *fp, void *buf) { int br; /* Use the buffer to read the file in blocks, writing each block to stdout * as we go */ - while ((br = read(fd, buf, BUFLEN)) > 0) { - fwrite(buf, 1, br, stdout); + while ((br = fread(buf, 1, BUFLEN, fp)) > 0) { + if (fwrite(buf, 1, br, stdout) == 0) { + perror(__FUNCTION__); + return -1; + } } /* If the last return value for br() was -1, there was an error; 0 is what * we expect */ - if (br == -1) { + if (ferror(fp) != 0 || ferror(stdout) != 0) { perror(__FUNCTION__); return -1; } @@ -25,7 +25,7 @@ int main(int argc, const char *argv[]) { /* If the filename is the special case of -, we emit stdin */ if (strncmp(argv[i], "-", 1) == 0) { - if (cfd(0, buf) == -1) { + if (cfp(stdin, buf) == -1) { exv = EXIT_FAILURE; } @@ -40,7 +40,7 @@ int main(int argc, const char *argv[]) { /* If there were no arguments, we assume the user wants us to read from * stdin, which should already be opened */ } else { - if (cfd(0, buf) == -1) { + if (cfp(stdin, buf) == -1) { exv = EXIT_FAILURE; } } |