/[jscoverage]/trunk/util.c
ViewVC logotype

Contents of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91 - (show annotations)
Wed May 7 03:59:56 2008 UTC (14 years, 10 months ago) by siliconforks
File MIME type: text/plain
File size: 8298 byte(s)
Add xrealloc.

1 /*
2 util.c - general purpose utility routines
3 Copyright (C) 2007, 2008 siliconforks.com
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "util.h"
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <strings.h>
28
29 #include <dirent.h>
30 #include <libgen.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 const char * program = NULL;
36
37 void fatal(const char * format, ...) {
38 fprintf(stderr, "%s: ", program);
39 va_list ap;
40 va_start(ap, format);
41 vfprintf(stderr, format, ap);
42 va_end(ap);
43 fputc('\n', stderr);
44 fprintf(stderr, "Try `%s --help' for more information.\n", program);
45 exit(EXIT_FAILURE);
46 }
47
48 void * xmalloc(size_t size) {
49 void * result = malloc(size);
50 if (result == NULL) {
51 fatal("out of memory");
52 }
53 return result;
54 }
55
56 void * xrealloc(void * p, size_t size) {
57 void * result = realloc(p, size);
58 if (result == NULL) {
59 fatal("out of memory");
60 }
61 return result;
62 }
63
64 char * xstrdup(const char * s) {
65 char * result = strdup(s);
66 if (result == NULL) {
67 fatal("out of memory");
68 }
69 return result;
70 }
71
72 char * xgetcwd(void) {
73 char * result = getcwd(NULL, 0);
74 if (result == NULL) {
75 fatal("out of memory");
76 }
77 return result;
78 }
79
80 FILE * xfopen(const char * file, const char * mode) {
81 FILE * result = fopen(file, mode);
82 if (result == NULL) {
83 fatal("cannot open file: %s", file);
84 }
85 return result;
86 }
87
88 DIR * xopendir(const char * directory) {
89 DIR * result = opendir(directory);
90 if (result == NULL) {
91 fatal("cannot open directory: %s", directory);
92 }
93 return result;
94 }
95
96 void xlstat(const char * file, struct stat * buf) {
97 #ifdef _WIN32
98 return xstat(file, buf);
99 #else
100 if (lstat(file, buf) == -1) {
101 fatal("cannot stat file: %s", file);
102 }
103 #endif
104 }
105
106 void xstat(const char * file, struct stat * buf) {
107 if (stat(file, buf) == -1) {
108 fatal("cannot stat file: %s", file);
109 }
110 }
111
112 void xmkdir(const char * directory) {
113 int result;
114 #ifdef _WIN32
115 result = mkdir(directory);
116 #else
117 result = mkdir(directory, 0755);
118 #endif
119 if (result == -1) {
120 fatal("cannot create directory: %s", directory);
121 }
122 }
123
124 void mkdir_if_necessary(const char * directory) {
125 struct stat buf;
126 if (stat(directory, &buf) == 0) {
127 if (! S_ISDIR(buf.st_mode)) {
128 fatal("not a directory: %s", directory);
129 }
130 }
131 else {
132 if (errno == ENOENT) {
133 xmkdir(directory);
134 }
135 else {
136 fatal("cannot stat directory: %s", directory);
137 }
138 }
139 }
140
141 void mkdirs(const char * directory) {
142 char * d = xmalloc(strlen(directory) + 1);
143 for (const char * p = directory; *p != '\0'; p++) {
144 if (*p == '/' && p > directory) {
145 strncpy(d, directory, p - directory);
146 d[p - directory] = '\0';
147 mkdir_if_necessary(d);
148 }
149 }
150 mkdir_if_necessary(directory);
151 free(d);
152 }
153
154 void xchdir(const char * directory) {
155 if (chdir(directory) == -1) {
156 fatal("cannot change directory: %s", directory);
157 }
158 }
159
160 char * make_path(const char * parent, const char * relative_path) {
161 size_t parent_length = strlen(parent);
162 size_t relative_path_length = strlen(relative_path);
163 char * result = xmalloc(parent_length + relative_path_length + 2);
164 strcpy(result, parent);
165 result[parent_length] = '/';
166 strcpy(result + parent_length + 1, relative_path);
167 return result;
168 }
169
170 char * make_canonical_path(const char * relative_path) {
171 char * original_directory = xgetcwd();
172 char * base = make_basename(relative_path);
173 char * dir = make_dirname(relative_path);
174
175 xchdir(dir);
176 char * canonical_dir = xgetcwd();
177 char * result = make_path(canonical_dir, base);
178
179 free(canonical_dir);
180 free(base);
181 free(dir);
182 xchdir(original_directory);
183 free(original_directory);
184
185 return result;
186 }
187
188 char * make_basename(const char * path) {
189 char * copy = xstrdup(path);
190 char * result = xstrdup(basename(copy));
191 free(copy);
192 return result;
193 }
194
195 char * make_dirname(const char * path) {
196 char * copy = xstrdup(path);
197 char * result = xstrdup(dirname(copy));
198 free(copy);
199 return result;
200 }
201
202 int is_same_file(const char * file1, const char * file2) {
203 #ifdef _WIN32
204 #define FILECMP strcasecmp
205 #else
206 #define FILECMP strcmp
207 #endif
208 if (FILECMP(file1, file2) == 0) {
209 return 1;
210 }
211
212 char * canonical1 = make_canonical_path(file1);
213 char * canonical2 = make_canonical_path(file2);
214 int cmp = FILECMP(canonical1, canonical2);
215 free(canonical1);
216 free(canonical2);
217 if (cmp == 0) {
218 return 1;
219 }
220
221 #ifndef _WIN32
222 struct stat buf1;
223 if (stat(file1, &buf1) == -1) {
224 if (errno == ENOENT) {
225 return 0;
226 }
227 else {
228 fatal("cannot stat file: %s", file1);
229 }
230 }
231 struct stat buf2;
232 if (stat(file2, &buf2) == -1) {
233 if (errno == ENOENT) {
234 return 0;
235 }
236 else {
237 fatal("cannot stat file: %s", file2);
238 }
239 }
240 if (buf1.st_dev == buf2.st_dev &&
241 buf1.st_ino == buf2.st_ino) {
242 return 1;
243 }
244 #endif
245 return 0;
246 #undef FILECMP
247 }
248
249 int contains_file(const char * file1, const char * file2) {
250 int result = 0;
251 char * ancestor = make_canonical_path(file1);
252 char * d = make_canonical_path(file2);
253 char * parent = make_dirname(d);
254 while (strcmp(d, parent) != 0) {
255 if (is_same_file(ancestor, parent)) {
256 result = 1;
257 break;
258 }
259 free(d);
260 d = parent;
261 parent = make_dirname(d);
262 }
263 free(d);
264 free(parent);
265 free(ancestor);
266 return result;
267 }
268
269 void copy_stream(FILE * source, FILE * destination) {
270 unsigned char buffer[8192];
271 for (;;) {
272 int bytes_read = fread(buffer, 1, sizeof(buffer), source);
273 if (bytes_read == 0) {
274 break;
275 }
276 fwrite(buffer, 1, bytes_read, destination);
277 }
278 }
279
280 void copy_file(const char * source_file, const char * destination_file) {
281 FILE * source = xfopen(source_file, "rb");
282 FILE * destination = xfopen(destination_file, "wb");
283
284 copy_stream(source, destination);
285
286 fclose(source);
287 fclose(destination);
288 }
289
290 int directory_is_empty(const char * directory) {
291 DIR * dir = xopendir(directory);
292 int num_entries = 0;
293 struct dirent * e;
294 while ((e = readdir(dir)) != NULL) {
295 if (strcmp(e->d_name, ".") != 0 &&
296 strcmp(e->d_name, "..") != 0) {
297 num_entries++;
298 }
299 }
300 closedir(dir);
301 return num_entries == 0;
302 }
303
304 static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {
305 char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root);
306 DIR * dir = xopendir(directory);
307 struct dirent * e;
308 while ((e = readdir(dir)) != NULL) {
309 if (strcmp(e->d_name, ".") == 0 ||
310 strcmp(e->d_name, "..") == 0) {
311 continue;
312 }
313 char * entry = make_path(directory, e->d_name);
314 char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name);
315 struct stat buf;
316 xlstat(entry, &buf);
317 if (S_ISREG(buf.st_mode)) {
318 struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
319 p->name = entry_wrt_root;
320 p->next = head;
321 head = p;
322 }
323 else if (S_ISDIR(buf.st_mode)) {
324 head = recursive_dir_list(root, entry_wrt_root, head);
325 free(entry_wrt_root);
326 }
327 else {
328 fatal("unknown file type: %s", entry);
329 }
330 free(entry);
331 }
332 closedir(dir);
333 free(directory);
334 return head;
335 }
336
337 struct DirListEntry * make_recursive_dir_list(const char * directory) {
338 return recursive_dir_list(directory, NULL, NULL);
339 }
340
341 void free_dir_list(struct DirListEntry * list) {
342 while (list != NULL) {
343 struct DirListEntry * next = list->next;
344 free(list->name);
345 free(list);
346 list = next;
347 }
348 }

  ViewVC Help
Powered by ViewVC 1.1.24