aboutsummaryrefslogtreecommitdiff
path: root/cfp.c
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 /cfp.c
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.
Diffstat (limited to 'cfp.c')
-rw-r--r--cfp.c33
1 files changed, 33 insertions, 0 deletions
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;
+}
+