aboutsummaryrefslogtreecommitdiff
path: root/cfn.c
blob: ad6776e14b072bcea0082a4ce00ab98bc128f41e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
    }
}