aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-03-04 10:36:08 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-03-04 10:36:51 +1300
commit4b49eed6098881fbde1f6c7728c1346fc8366979 (patch)
tree7c5a3d7ee9ec01e71ae1e6eea1f59533c0ca4c8d
parentClean up Makefile a bit (diff)
downloadcat-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--Makefile4
-rw-r--r--cat.h2
-rw-r--r--cfn.c13
-rw-r--r--cfp.c (renamed from cfd.c)11
-rw-r--r--main.c4
5 files changed, 19 insertions, 15 deletions
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/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/cfd.c b/cfp.c
index 9e8baa6..9d0d5a7 100644
--- a/cfd.c
+++ b/cfp.c
@@ -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;
}
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;
}
}