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

Contents of /trunk/util.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 311 - (show annotations)
Mon Oct 13 17:51:29 2008 UTC (9 years, 11 months ago) by siliconforks
File MIME type: text/plain
File size: 12017 byte(s)
With the --version option, print out character encoding support of this executable.
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 #define _GNU_SOURCE
21
22 #include <config.h>
23
24 #include "util.h"
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <strings.h>
34
35 #include <dirent.h>
36 #include <libgen.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 const char * program = NULL;
42
43 void fatal(const char * format, ...) {
44 fprintf(stderr, "%s: ", program);
45 va_list ap;
46 va_start(ap, format);
47 vfprintf(stderr, format, ap);
48 va_end(ap);
49 fputc('\n', stderr);
50 exit(EXIT_FAILURE);
51 }
52
53 void fatal_command_line(const char * format, ...) {
54 fprintf(stderr, "%s: ", program);
55 va_list ap;
56 va_start(ap, format);
57 vfprintf(stderr, format, ap);
58 va_end(ap);
59 fputc('\n', stderr);
60 fprintf(stderr, "Try `%s --help' for more information.\n", program);
61 exit(EXIT_FAILURE);
62 }
63
64 void version(void) {
65 printf("%s %s\n", program, VERSION);
66 printf("Character encoding support: ");
67 #if HAVE_ICONV
68 printf("iconv\n");
69 #elif HAVE_MULTIBYTETOWIDECHAR
70 printf("MultiByteToWideChar\n");
71 #else
72 printf("none\n");
73 #endif
74 exit(EXIT_SUCCESS);
75 }
76
77 size_t addst(size_t x, size_t y) {
78 if (SIZE_MAX - x < y) {
79 fatal("integer overflow");
80 }
81 return x + y;
82 }
83
84 size_t mulst(size_t x, size_t y) {
85 if (x == 0 || y == 0) {
86 return 0;
87 }
88 if (SIZE_MAX / x < y) {
89 fatal("integer overflow");
90 }
91 return x * y;
92 }
93
94 void * xmalloc(size_t size) {
95 void * result = malloc(size);
96 if (result == NULL) {
97 fatal("out of memory");
98 }
99 return result;
100 }
101
102 void * xrealloc(void * p, size_t size) {
103 void * result = realloc(p, size);
104 if (result == NULL) {
105 fatal("out of memory");
106 }
107 return result;
108 }
109
110 char * xstrdup(const char * s) {
111 char * result = strdup(s);
112 if (result == NULL) {
113 fatal("out of memory");
114 }
115 return result;
116 }
117
118 char * xstrndup(const char * s, size_t size) {
119 char * result = strndup(s, size);
120 if (result == NULL) {
121 fatal("out of memory");
122 }
123 return result;
124 }
125
126 int xasprintf(char ** s, const char * template, ...) {
127 va_list a;
128 va_start(a, template);
129 int result = vasprintf(s, template, a);
130 va_end(a);
131 if (result < 0) {
132 fatal("out of memory");
133 }
134 return result;
135 }
136
137 char * xgetcwd(void) {
138 char * result = getcwd(NULL, 0);
139 if (result == NULL) {
140 fatal("out of memory");
141 }
142 return result;
143 }
144
145 FILE * xfopen(const char * file, const char * mode) {
146 FILE * result = fopen(file, mode);
147 if (result == NULL) {
148 fatal("cannot open file: %s", file);
149 }
150 return result;
151 }
152
153 DIR * xopendir(const char * directory) {
154 DIR * result = opendir(directory);
155 if (result == NULL) {
156 fatal("cannot open directory: %s", directory);
157 }
158 return result;
159 }
160
161 void xlstat(const char * file, struct stat * buf) {
162 #ifdef _WIN32
163 return xstat(file, buf);
164 #else
165 if (lstat(file, buf) == -1) {
166 fatal("cannot stat file: %s", file);
167 }
168 #endif
169 }
170
171 void xstat(const char * file, struct stat * buf) {
172 if (stat(file, buf) == -1) {
173 fatal("cannot stat file: %s", file);
174 }
175 }
176
177 void xmkdir(const char * directory) {
178 int result;
179 #ifdef _WIN32
180 result = mkdir(directory);
181 #else
182 result = mkdir(directory, 0755);
183 #endif
184 if (result == -1) {
185 fatal("cannot create directory: %s", directory);
186 }
187 }
188
189 void mkdir_if_necessary(const char * directory) {
190 struct stat buf;
191 if (stat(directory, &buf) == 0) {
192 if (! S_ISDIR(buf.st_mode)) {
193 fatal("not a directory: %s", directory);
194 }
195 }
196 else {
197 if (errno == ENOENT) {
198 xmkdir(directory);
199 }
200 else {
201 fatal("cannot stat directory: %s", directory);
202 }
203 }
204 }
205
206 void mkdirs(const char * directory) {
207 char * d = xmalloc(strlen(directory) + 1);
208 for (const char * p = directory; *p != '\0'; p++) {
209 if (*p == '/' && p > directory) {
210 strncpy(d, directory, p - directory);
211 d[p - directory] = '\0';
212 mkdir_if_necessary(d);
213 }
214 }
215 mkdir_if_necessary(directory);
216 free(d);
217 }
218
219 void xchdir(const char * directory) {
220 if (chdir(directory) == -1) {
221 fatal("cannot change directory: %s", directory);
222 }
223 }
224
225 bool str_starts_with(const char * string, const char * prefix) {
226 const char * string_ptr = string;
227 const char * prefix_ptr = prefix;
228 while (*string_ptr != '\0' && *prefix_ptr != '\0') {
229 if (*string_ptr != *prefix_ptr) {
230 return false;
231 }
232 string_ptr++;
233 prefix_ptr++;
234 }
235 if (*string_ptr == '\0' && *prefix_ptr != '\0') {
236 return false;
237 }
238 return true;
239 }
240
241 bool str_ends_with(const char * string, const char * suffix) {
242 size_t string_length = strlen(string);
243 size_t suffix_length = strlen(suffix);
244 if (string_length < suffix_length) {
245 return false;
246 }
247 return strcmp(string + string_length - suffix_length, suffix) == 0;
248 }
249
250 char * make_path(const char * parent, const char * relative_path) {
251 size_t parent_length = strlen(parent);
252 size_t relative_path_length = strlen(relative_path);
253 size_t result_length = addst(parent_length, relative_path_length);
254 result_length = addst(result_length, 2);
255 char * result = xmalloc(result_length);
256 strcpy(result, parent);
257 result[parent_length] = '/';
258 strcpy(result + parent_length + 1, relative_path);
259 return result;
260 }
261
262 char * make_canonical_path(const char * relative_path) {
263 char * original_directory = xgetcwd();
264 char * base = make_basename(relative_path);
265 char * dir = make_dirname(relative_path);
266
267 xchdir(dir);
268 char * canonical_dir = xgetcwd();
269 char * result = make_path(canonical_dir, base);
270
271 free(canonical_dir);
272 free(base);
273 free(dir);
274 xchdir(original_directory);
275 free(original_directory);
276
277 return result;
278 }
279
280 char * make_basename(const char * path) {
281 char * copy = xstrdup(path);
282 char * result = xstrdup(basename(copy));
283 free(copy);
284 return result;
285 }
286
287 char * make_dirname(const char * path) {
288 char * copy = xstrdup(path);
289 char * result = xstrdup(dirname(copy));
290 free(copy);
291 return result;
292 }
293
294 int is_same_file(const char * file1, const char * file2) {
295 #ifdef _WIN32
296 #define FILECMP strcasecmp
297 #else
298 #define FILECMP strcmp
299 #endif
300 if (FILECMP(file1, file2) == 0) {
301 return 1;
302 }
303
304 char * canonical1 = make_canonical_path(file1);
305 char * canonical2 = make_canonical_path(file2);
306 int cmp = FILECMP(canonical1, canonical2);
307 free(canonical1);
308 free(canonical2);
309 if (cmp == 0) {
310 return 1;
311 }
312
313 #ifndef _WIN32
314 struct stat buf1;
315 if (stat(file1, &buf1) == -1) {
316 if (errno == ENOENT) {
317 return 0;
318 }
319 else {
320 fatal("cannot stat file: %s", file1);
321 }
322 }
323 struct stat buf2;
324 if (stat(file2, &buf2) == -1) {
325 if (errno == ENOENT) {
326 return 0;
327 }
328 else {
329 fatal("cannot stat file: %s", file2);
330 }
331 }
332 if (buf1.st_dev == buf2.st_dev &&
333 buf1.st_ino == buf2.st_ino) {
334 return 1;
335 }
336 #endif
337 return 0;
338 #undef FILECMP
339 }
340
341 int contains_file(const char * file1, const char * file2) {
342 int result = 0;
343 char * ancestor = make_canonical_path(file1);
344 char * d = make_canonical_path(file2);
345 char * parent = make_dirname(d);
346 while (strcmp(d, parent) != 0) {
347 if (is_same_file(ancestor, parent)) {
348 result = 1;
349 break;
350 }
351 free(d);
352 d = parent;
353 parent = make_dirname(d);
354 }
355 free(d);
356 free(parent);
357 free(ancestor);
358 return result;
359 }
360
361 void copy_stream(FILE * source, FILE * destination) {
362 unsigned char buffer[8192];
363 for (;;) {
364 int bytes_read = fread(buffer, 1, sizeof(buffer), source);
365 if (bytes_read == 0) {
366 break;
367 }
368 fwrite(buffer, 1, bytes_read, destination);
369 }
370 }
371
372 void copy_file(const char * source_file, const char * destination_file) {
373 FILE * source = xfopen(source_file, "rb");
374 FILE * destination = xfopen(destination_file, "wb");
375
376 copy_stream(source, destination);
377
378 fclose(source);
379 fclose(destination);
380 }
381
382 bool directory_is_empty(const char * directory) {
383 bool result = true;
384 DIR * dir = xopendir(directory);
385 struct dirent * e;
386 while ((e = readdir(dir)) != NULL) {
387 if (strcmp(e->d_name, ".") != 0 &&
388 strcmp(e->d_name, "..") != 0) {
389 result = false;
390 break;
391 }
392 }
393 closedir(dir);
394 return result;
395 }
396
397 static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {
398 char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root);
399 DIR * dir = xopendir(directory);
400 struct dirent * e;
401 while ((e = readdir(dir)) != NULL) {
402 if (strcmp(e->d_name, ".") == 0 ||
403 strcmp(e->d_name, "..") == 0) {
404 continue;
405 }
406 char * entry = make_path(directory, e->d_name);
407 char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name);
408 struct stat buf;
409 xlstat(entry, &buf);
410 if (S_ISREG(buf.st_mode)) {
411 struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
412 p->name = entry_wrt_root;
413 p->next = head;
414 head = p;
415 }
416 else if (S_ISDIR(buf.st_mode)) {
417 head = recursive_dir_list(root, entry_wrt_root, head);
418 free(entry_wrt_root);
419 }
420 else {
421 fatal("unknown file type: %s", entry);
422 }
423 free(entry);
424 }
425 closedir(dir);
426 free(directory);
427 return head;
428 }
429
430 struct DirListEntry * make_recursive_dir_list(const char * directory) {
431 return recursive_dir_list(directory, NULL, NULL);
432 }
433
434 void free_dir_list(struct DirListEntry * list) {
435 while (list != NULL) {
436 struct DirListEntry * next = list->next;
437 free(list->name);
438 free(list);
439 list = next;
440 }
441 }
442
443 #ifndef HAVE_STRNDUP
444 char * strndup(const char * s, size_t size) {
445 size_t length = strlen(s);
446 if (length > size) {
447 char * result = xmalloc(size + 1);
448 strncpy(result, s, size);
449 result[size] = '\0';
450 return result;
451 }
452 else {
453 char * result = xmalloc(length + 1);
454 strcpy(result, s);
455 return result;
456 }
457 }
458 #endif
459
460 #ifndef HAVE_VASPRINTF
461 int vasprintf(char ** s, const char * template, va_list a) {
462 int size = 100;
463 *s = malloc(size);
464 if (*s == NULL) {
465 return -1;
466 }
467
468 va_list copy;
469 va_copy(copy, a);
470 int result = vsnprintf(*s, size, template, copy);
471 if (result >= size) {
472 int new_size = result;
473 if (new_size == INT_MAX) {
474 free(*s);
475 return -1;
476 }
477 new_size++;
478 char * new_s = realloc(*s, new_size);
479 if (new_s == NULL) {
480 free(*s);
481 return -1;
482 }
483 *s = new_s;
484 size = new_size;
485 va_copy(copy, a);
486 result = vsnprintf(*s, size, template, copy);
487 assert(result == size - 1);
488 }
489 else if (result == -1) {
490 while (result == -1) {
491 if (size == INT_MAX) {
492 free(*s);
493 return -1;
494 }
495 int new_size;
496 if (size > INT_MAX / 2) {
497 new_size = INT_MAX;
498 }
499 else {
500 new_size = 2 * size;
501 }
502 char * new_s = realloc(*s, new_size);
503 if (new_s == NULL) {
504 free(*s);
505 return -1;
506 }
507 *s = new_s;
508 size = new_size;
509 va_copy(copy, a);
510 result = vsnprintf(*s, size, template, copy);
511 }
512 assert(result <= size - 1);
513 }
514
515 return result;
516 }
517 #endif
518
519 #ifndef HAVE_ASPRINTF
520 int asprintf(char ** s, const char * template, ...) {
521 va_list a;
522 va_start(a, template);
523 int result = vasprintf(s, template, a);
524 va_end(a);
525 return result;
526 }
527 #endif

  ViewVC Help
Powered by ViewVC 1.1.24