File: util.c

package info (click to toggle)
jscoverage 0.3.1-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 7,308 kB
  • ctags: 8,302
  • sloc: ansic: 77,298; sh: 12,541; makefile: 224; perl: 176
file content (340 lines) | stat: -rw-r--r-- 8,139 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
    util.c - general purpose utility routines
    Copyright (C) 2007 siliconforks.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "util.h"

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>

#include <dirent.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

const char * program = NULL;

void fatal(const char * format, ...) {
  fprintf(stderr, "%s: ", program);
  va_list ap;
  va_start(ap, format);
  vfprintf(stderr, format, ap);
  va_end(ap);
  fputc('\n', stderr);
  fprintf(stderr, "Try `%s --help' for more information.\n", program);
  exit(EXIT_FAILURE);
}

void * xmalloc(size_t size) {
  void * result = malloc(size);
  if (result == NULL) {
    fatal("out of memory");
  }
  return result;
}

char * xstrdup(const char * s) {
  char * result = strdup(s);
  if (result == NULL) {
    fatal("out of memory");
  }
  return result;
}

char * xgetcwd(void) {
  char * result = getcwd(NULL, 0);
  if (result == NULL) {
    fatal("out of memory");
  }
  return result;
}

FILE * xfopen(const char * file, const char * mode) {
  FILE * result = fopen(file, mode);
  if (result == NULL) {
    fatal("cannot open file: %s", file);
  }
  return result;
}

DIR * xopendir(const char * directory) {
  DIR * result = opendir(directory);
  if (result == NULL) {
    fatal("cannot open directory: %s", directory);
  }
  return result;
}

void xlstat(const char * file, struct stat * buf) {
#ifdef _WIN32
  return xstat(file, buf);
#else
  if (lstat(file, buf) == -1) {
    fatal("cannot stat file: %s", file);
  }
#endif
}

void xstat(const char * file, struct stat * buf) {
  if (stat(file, buf) == -1) {
    fatal("cannot stat file: %s", file);
  }
}

void xmkdir(const char * directory) {
  int result;
#ifdef _WIN32
  result = mkdir(directory);
#else
  result = mkdir(directory, 0755);
#endif
  if (result == -1) {
    fatal("cannot create directory: %s", directory);
  }
}

void mkdir_if_necessary(const char * directory) {
  struct stat buf;
  if (stat(directory, &buf) == 0) {
    if (! S_ISDIR(buf.st_mode)) {
      fatal("not a directory: %s", directory);
    }
  }
  else {
    if (errno == ENOENT) {
      xmkdir(directory);
    }
    else {
      fatal("cannot stat directory: %s", directory);
    }
  }
}

void mkdirs(const char * directory) {
  char * d = xmalloc(strlen(directory) + 1);
  for (const char * p = directory; *p != '\0'; p++) {
    if (*p == '/' && p > directory) {
      strncpy(d, directory, p - directory);
      d[p - directory] = '\0';
      mkdir_if_necessary(d);
    }
  }
  mkdir_if_necessary(directory);
  free(d);
}

void xchdir(const char * directory) {
  if (chdir(directory) == -1) {
    fatal("cannot change directory: %s", directory);
  }
}

char * make_path(const char * parent, const char * relative_path) {
  size_t parent_length = strlen(parent);
  size_t relative_path_length = strlen(relative_path);
  char * result = xmalloc(parent_length + relative_path_length + 2);
  strcpy(result, parent);
  result[parent_length] = '/';
  strcpy(result + parent_length + 1, relative_path);
  return result;
}

char * make_canonical_path(const char * relative_path) {
  char * original_directory = xgetcwd();
  char * base = make_basename(relative_path);
  char * dir = make_dirname(relative_path);

  xchdir(dir);
  char * canonical_dir = xgetcwd();
  char * result = make_path(canonical_dir, base);

  free(canonical_dir);
  free(base);
  free(dir);
  xchdir(original_directory);
  free(original_directory);

  return result;
}

char * make_basename(const char * path) {
  char * copy = xstrdup(path);
  char * result = xstrdup(basename(copy));
  free(copy);
  return result;
}

char * make_dirname(const char * path) {
  char * copy = xstrdup(path);
  char * result = xstrdup(dirname(copy));
  free(copy);
  return result;
}

int is_same_file(const char * file1, const char * file2) {
#ifdef _WIN32
#define FILECMP strcasecmp
#else
#define FILECMP strcmp
#endif
  if (FILECMP(file1, file2) == 0) {
    return 1;
  }

  char * canonical1 = make_canonical_path(file1);
  char * canonical2 = make_canonical_path(file2);
  int cmp = FILECMP(canonical1, canonical2);
  free(canonical1);
  free(canonical2);
  if (cmp == 0) {
    return 1;
  }

#ifndef _WIN32
  struct stat buf1;
  if (stat(file1, &buf1) == -1) {
    if (errno == ENOENT) {
      return 0;
    }
    else {
      fatal("cannot stat file: %s", file1);
    }
  }
  struct stat buf2;
  if (stat(file2, &buf2) == -1) {
    if (errno == ENOENT) {
      return 0;
    }
    else {
      fatal("cannot stat file: %s", file2);
    }
  }
  if (buf1.st_dev == buf2.st_dev &&
      buf1.st_ino == buf2.st_ino) {
    return 1;
  }
#endif
  return 0;
#undef FILECMP
}

int contains_file(const char * file1, const char * file2) {
  int result = 0;
  char * ancestor = make_canonical_path(file1);
  char * d = make_canonical_path(file2);
  char * parent = make_dirname(d);
  while (strcmp(d, parent) != 0) {
    if (is_same_file(ancestor, parent)) {
      result = 1;
      break;
    }
    free(d);
    d = parent;
    parent = make_dirname(d);
  }
  free(d);
  free(parent);
  free(ancestor);
  return result;
}

void copy_stream(FILE * source, FILE * destination) {
  unsigned char buffer[8192];
  for (;;) {
    int bytes_read = fread(buffer, 1, sizeof(buffer), source);
    if (bytes_read == 0) {
      break;
    }
    fwrite(buffer, 1, bytes_read, destination);
  }
}

void copy_file(const char * source_file, const char * destination_file) {
  FILE * source = xfopen(source_file, "rb");
  FILE * destination = xfopen(destination_file, "wb");

  copy_stream(source, destination);

  fclose(source);
  fclose(destination);
}

int directory_is_empty(const char * directory) {
  DIR * dir = xopendir(directory);
  int num_entries = 0;
  struct dirent * e;
  while ((e = readdir(dir)) != NULL) {
    if (strcmp(e->d_name, ".") != 0 &&
        strcmp(e->d_name, "..") != 0) {
      num_entries++;
    }
  }
  closedir(dir);
  return num_entries == 0;
}

static struct DirListEntry * recursive_dir_list(const char * root, const char * directory_wrt_root, struct DirListEntry * head) {
  char * directory = directory_wrt_root == NULL? xstrdup(root): make_path(root, directory_wrt_root);
  DIR * dir = xopendir(directory);
  struct dirent * e;
  while ((e = readdir(dir)) != NULL) {
    if (strcmp(e->d_name, ".") == 0 ||
        strcmp(e->d_name, "..") == 0) {
      continue;
    }
    char * entry = make_path(directory, e->d_name);
    char * entry_wrt_root = directory_wrt_root == NULL? xstrdup(e->d_name): make_path(directory_wrt_root, e->d_name);
    struct stat buf;
    xlstat(entry, &buf);
    if (S_ISREG(buf.st_mode)) {
      struct DirListEntry * p = xmalloc(sizeof(struct DirListEntry));
      p->name = entry_wrt_root;
      p->next = head;
      head = p;
    }
    else if (S_ISDIR(buf.st_mode)) {
      head = recursive_dir_list(root, entry_wrt_root, head);
      free(entry_wrt_root);
    }
    else {
      fatal("unknown file type: %s", entry);
    }
    free(entry);
  }
  closedir(dir);
  free(directory);
  return head;
}

struct DirListEntry * make_recursive_dir_list(const char * directory) {
  return recursive_dir_list(directory, NULL, NULL);
}

void free_dir_list(struct DirListEntry * list) {
  while (list != NULL) {
    struct DirListEntry * next = list->next;
    free(list->name);
    free(list);
    list = next;
  }
}