nsgenbind
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/* utility functions
2 *
3 * This file is part of nsgenbind.
4 * Published under the MIT License,
5 * http://www.opensource.org/licenses/mit-license.php
6 * Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <stdbool.h>
12#include <errno.h>
13#include <stdlib.h>
14#include <sys/types.h>
15#include <unistd.h>
16
17#include "options.h"
18#include "utils.h"
19
20/* exported function documented in utils.h */
21char *genb_fpath(const char *fname)
22{
23 char *fpath;
24 int fpathl;
25
26 fpathl = strlen(options->outdirname) + strlen(fname) + 2;
27 fpath = malloc(fpathl);
28 snprintf(fpath, fpathl, "%s/%s", options->outdirname, fname);
29
30 return fpath;
31}
32
33static char *genb_fpath_tmp(const char *fname)
34{
35 char *fpath;
36 int fpathl;
37
38 fpathl = strlen(options->outdirname) + strlen(fname) + 3;
39 fpath = malloc(fpathl);
40 snprintf(fpath, fpathl, "%s/%s.%d", options->outdirname, fname, getpid());
41
42 return fpath;
43}
44
45/* exported function documented in utils.h */
46FILE *genb_fopen(const char *fname, const char *mode)
47{
48 char *fpath;
49 FILE *filef;
50
51 if (options->dryrun) {
52 fpath = strdup("/dev/null");
53 } else {
54 fpath = genb_fpath(fname);
55 }
56
57 filef = fopen(fpath, mode);
58 if (filef == NULL) {
59 fprintf(stderr, "Error: unable to open file %s (%s)\n",
60 fpath, strerror(errno));
61 free(fpath);
62 return NULL;
63 }
64 free(fpath);
65
66 return filef;
67}
68
69/* exported function documented in utils.h */
70FILE *genb_fopen_tmp(const char *fname)
71{
72 char *fpath;
73 FILE *filef;
74
75 if (options->dryrun) {
76 fpath = strdup("/dev/null");
77 } else {
78 fpath = genb_fpath_tmp(fname);
79 }
80
81 filef = fopen(fpath, "w+");
82 if (filef == NULL) {
83 fprintf(stderr, "Error: unable to open file %s (%s)\n",
84 fpath, strerror(errno));
85 free(fpath);
86 return NULL;
87 }
88 free(fpath);
89
90 return filef;
91}
92
93int genb_fclose_tmp(FILE *filef_tmp, const char *fname)
94{
95 char *fpath;
96 char *tpath;
97 FILE *filef;
98 char tbuf[1024];
99 char fbuf[1024];
100 size_t trd;
101 size_t frd;
102
103 if (options->dryrun) {
104 fclose(filef_tmp);
105 return 0;
106 }
107
108 fpath = genb_fpath(fname);
109 tpath = genb_fpath_tmp(fname);
110
111 filef = fopen(fpath, "r");
112 if (filef == NULL) {
113 /* unable to open target file for comparison */
114
115 fclose(filef_tmp); /* close tmpfile */
116
117 remove(fpath);
118 rename(tpath, fpath);
119 } else {
120 rewind(filef_tmp);
121
122 frd = fread(fbuf, 1, 1024, filef);
123 while (frd != 0) {
124 trd = fread(tbuf, 1, frd, filef_tmp);
125 if ((trd != frd) ||
126 (memcmp(tbuf, fbuf, trd) != 0)) {
127 /* file doesnt match */
128 fclose(filef_tmp);
129 fclose(filef);
130
131 remove(fpath);
132 rename(tpath, fpath);
133
134 goto close_done;
135 }
136
137 frd = fread(fbuf, 1, 1024, filef);
138 }
139
140 /* was the same kill temporary file */
141 fclose(filef_tmp);
142 fclose(filef);
143 remove(tpath);
144 }
145
146close_done:
147 free(fpath);
148 free(tpath);
149
150 return 0;
151}
152
153
154#ifdef NEED_STRNDUP
155
156char *strndup(const char *s, size_t n)
157{
158 size_t len;
159 char *s2;
160
161 for (len = 0; len != n && s[len]; len++)
162 continue;
163
164 s2 = malloc(len + 1);
165 if (!s2)
166 return 0;
167
168 memcpy(s2, s, len);
169 s2[len] = 0;
170 return s2;
171}
172
173#endif
void * malloc(YYSIZE_T)
void free(void *)
char * outdirname
Definition options.h:15
bool dryrun
Definition options.h:21
FILE * genb_fopen_tmp(const char *fname)
Definition utils.c:70
char * genb_fpath(const char *fname)
Definition utils.c:21
FILE * genb_fopen(const char *fname, const char *mode)
Definition utils.c:46
int genb_fclose_tmp(FILE *filef_tmp, const char *fname)
Definition utils.c:93