aboutsummaryrefslogtreecommitdiff
path: root/cfd.c
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-02-28 13:03:40 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-02-28 13:03:40 +1300
commit7208f6fd2d62bef16edbafd327e7e0d4c13b7966 (patch)
tree4c87ce5bcdc9cc44022d2cd06fbab57a56507d96 /cfd.c
downloadcat-7208f6fd2d62bef16edbafd327e7e0d4c13b7966.tar.gz
cat-7208f6fd2d62bef16edbafd327e7e0d4c13b7966.zip
First commit of that cat(1) clone I'm making
Diffstat (limited to 'cfd.c')
-rw-r--r--cfd.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/cfd.c b/cfd.c
new file mode 100644
index 0000000..9e8baa6
--- /dev/null
+++ b/cfd.c
@@ -0,0 +1,30 @@
+#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;
+}
+