summaryrefslogtreecommitdiff
path: root/src/fdlog.c
blob: e9bcdeb60b235bed9836fdcbea4cd3aa520fa635 (plain)
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
26
27
28
#include "first.h"
#include "fdlog.h"

#include <stdlib.h>
#include <unistd.h>     /* close() STDERR_FILENO */

#include "ck.h"

fdlog_st *
fdlog_init (const char * const fn, const int fd, const int mode)
{
    fdlog_st * const fdlog = calloc(1, sizeof(fdlog_st));
    ck_assert(fdlog);
    fdlog->fn = fn; /* note: fn must persist in memory (or else copy here) */
    fdlog->fd = fd >= 0 ? fd : STDERR_FILENO;
    fdlog->mode = mode;
    return fdlog;
}


void
fdlog_free (fdlog_st * const fdlog)
{
    if (fdlog->fd > STDERR_FILENO)
        close(fdlog->fd);
    free(fdlog->b.ptr);
    free(fdlog);
}