aboutsummaryrefslogblamecommitdiff
path: root/cfd.c
blob: 9e8baa6e7602498bc634f2b316678c2b1890342b (plain) (tree)





























                                                                              
#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;
}