summaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 09527446d0e85a75d852efd7f1fb01264f78dbe2 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* utility functions
 *
 * This file is part of nsgenbind.
 * Published under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
 */

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

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

/* exported function documented in utils.h */
char *genb_fpath(const char *fname)
{
        char *fpath;
        int fpathl;

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

        return fpath;
}

static char *genb_fpath_tmp(const char *fname)
{
        char *fpath;
        int fpathl;

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

        return fpath;
}

/* exported function documented in utils.h */
FILE *genb_fopen(const char *fname, const char *mode)
{
        char *fpath;
        FILE *filef;

        fpath = genb_fpath(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;
}

/* exported function documented in utils.h */
FILE *genb_fopen_tmp(const char *fname)
{
        char *fpath;
        FILE *filef;

        fpath = genb_fpath_tmp(fname);

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

        return filef;
}

int genb_fclose_tmp(FILE *filef_tmp, const char *fname)
{
        char *fpath;
        char *tpath;
        FILE *filef;
        char tbuf[1024];
        char fbuf[1024];
        size_t trd;
        size_t frd;

        fpath = genb_fpath(fname);
        tpath = genb_fpath_tmp(fname);

        filef = fopen(fpath, "r");
        if (filef == NULL) {
                /* unable to open target file for comparison */

                fclose(filef_tmp); /*  close tmpfile */

                remove(fpath);
                rename(tpath, fpath);
        } else {
                rewind(filef_tmp);

                frd = fread(fbuf, 1, 1024, filef);
                while (frd != 0) {
                        trd = fread(tbuf, 1, frd, filef_tmp);
                        if ((trd != frd) ||
                            (memcmp(tbuf, fbuf, trd) != 0)) {
                                /* file doesnt match */
                                fclose(filef_tmp);
                                fclose(filef);

                                remove(fpath);
                                rename(tpath, fpath);

                                goto close_done;
                        }

                        frd = fread(fbuf, 1, 1024, filef);
                }

                /* was the same kill temporary file */
                fclose(filef_tmp);
                fclose(filef);
                remove(tpath);
        }

close_done:
        free(fpath);
        free(tpath);

        return 0;
}


#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