From 4b49eed6098881fbde1f6c7728c1346fc8366979 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 4 Mar 2016 10:36:08 +1300 Subject: 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. --- Makefile | 4 ++-- cat.h | 2 +- cfd.c | 30 ------------------------------ cfn.c | 13 +++++++------ cfp.c | 33 +++++++++++++++++++++++++++++++++ main.c | 4 ++-- 6 files changed, 45 insertions(+), 41 deletions(-) delete mode 100644 cfd.c create mode 100644 cfp.c diff --git a/Makefile b/Makefile index 81db4b5..e373493 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cat.h b/cat.h index 2a2e494..da5e7ed 100644 --- a/cat.h +++ b/cat.h @@ -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 diff --git a/cfd.c b/cfd.c deleted file mode 100644 index 9e8baa6..0000000 --- a/cfd.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "cat.h" - -/* Function writes the contents of an opened file descriptor to stdout */ -int cfd(int fd, 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); - } - - /* If the last return value for br() was -1, there was an error; 0 is what - * we expect */ - if (br == -1) { - perror(__FUNCTION__); - return -1; - } - - /* Force a write of any still-buffered data to stdout */ - if (fflush(stdout) != 0) { - perror(__FUNCTION__); - return -1; - } - - /* Return success, since apparently nothing went wrong before we got here - * */ - return 0; -} - diff --git a/cfn.c b/cfn.c index 9c04fb8..b830f54 100644 --- a/cfn.c +++ b/cfn.c @@ -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; } diff --git a/cfp.c b/cfp.c new file mode 100644 index 0000000..9d0d5a7 --- /dev/null +++ b/cfp.c @@ -0,0 +1,33 @@ +#include "cat.h" + +/* Function writes the contents of an opened file descriptor to stdout */ +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 = 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 (ferror(fp) != 0 || ferror(stdout) != 0) { + perror(__FUNCTION__); + return -1; + } + + /* Force a write of any still-buffered data to stdout */ + if (fflush(stdout) != 0) { + perror(__FUNCTION__); + return -1; + } + + /* Return success, since apparently nothing went wrong before we got here + * */ + return 0; +} + diff --git a/main.c b/main.c index 620cc41..51ee707 100644 --- a/main.c +++ b/main.c @@ -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; } } -- cgit v1.2.3