aboutsummaryrefslogtreecommitdiff
path: root/cfn.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 /cfn.c
downloadcat-7208f6fd2d62bef16edbafd327e7e0d4c13b7966.tar.gz
cat-7208f6fd2d62bef16edbafd327e7e0d4c13b7966.zip
First commit of that cat(1) clone I'm making
Diffstat (limited to 'cfn.c')
-rw-r--r--cfn.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/cfn.c b/cfn.c
new file mode 100644
index 0000000..ad6776e
--- /dev/null
+++ b/cfn.c
@@ -0,0 +1,25 @@
+#include "cat.h"
+
+/* 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;
+
+ /* Open the file to get a read-only file descriptor */
+ if ((fd = open(fn, O_RDONLY)) == -1) {
+ perror(__FUNCTION__);
+ return -1;
+ }
+
+ /* 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);
+
+ /* Close the descriptor, since we should now be done with it */
+ if (close(fd) == -1) {
+ perror(__FUNCTION__);
+ return -1;
+ }
+}
+