summaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 7bab05875b88fe0d95f068f28590da619d80eda5 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h>

#include "options.h"
#include "utils.h"

FILE *genb_fopen(const char *fname, const char *mode)
{
    char *fpath;
    int fpathl;
    FILE *filef;

    fpathl = strlen(options->outdirname) + strlen(fname) + 2;
    fpath = malloc(fpathl);
    snprintf(fpath, fpathl, "%s/%s", options->outdirname, fname);

    filef = fopen(fpath, mode);
    if (filef == NULL) {
        fprintf(stderr, "Error: unable to open file %s (%s)\n",
                fpath, strerror(errno));
        free(fpath);
        return NULL;
    }
    free(fpath);

    return filef;
}

#ifdef NEED_STRNDUP

char *strndup(const char *s, size_t n)
{
	size_t len;
	char *s2;

	for (len = 0; len != n && s[len]; len++)
		continue;

	s2 = malloc(len + 1);
	if (!s2)
		return 0;

	memcpy(s2, s, len);
	s2[len] = 0;
	return s2;
}

#endif