2 util.h - general purpose utility routines
3 Copyright (C) 2007, 2008 siliconforks.com
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.
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.
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.
23 #ifndef HAVE_VASPRINTF
36 extern const char * program;
38 void fatal(const char * format, ...)
39 __attribute__((__noreturn__))
40 __attribute__((__format__(printf, 1, 2)));
42 void fatal_command_line(const char * format, ...)
43 __attribute__((__noreturn__))
44 __attribute__((__format__(printf, 1, 2)));
46 void fatal_source(const char * source_file, unsigned int line_number, const char * format, ...)
47 __attribute__((__noreturn__))
48 __attribute__((__format__(printf, 3, 4)));
50 void warn_source(const char * source_file, unsigned int line_number, const char * format, ...)
51 __attribute__((__format__(printf, 3, 4)));
54 __attribute__((__noreturn__));
56 size_t addst(size_t x, size_t y);
58 size_t mulst(size_t x, size_t y);
60 void * xmalloc(size_t size);
62 #define xnew(type, count) ((type *) xmalloc(mulst((count), sizeof(type))))
64 void * xrealloc(void * p, size_t size);
66 char * xstrdup(const char * s);
68 char * xstrndup(const char * s, size_t size);
70 int xasprintf(char ** s, const char * format, ...) __attribute__((__format__(printf, 2, 3)));
74 FILE * xfopen(const char * file, const char * mode);
76 void xstat(const char * file, struct stat * buf);
78 void xlstat(const char * file, struct stat * buf);
80 void xmkdir(const char * directory);
82 void mkdir_if_necessary(const char * directory);
84 void mkdirs(const char * path);
86 bool str_starts_with(const char * string, const char * prefix);
88 bool str_ends_with(const char * string, const char * suffix);
90 char * make_path(const char * parent, const char * relative_path);
92 char * make_canonical_path(const char * relative_path);
94 char * make_basename(const char * path);
96 char * make_dirname(const char * path);
98 int is_same_file(const char * file1, const char * file2);
100 int contains_file(const char * file1, const char * file2);
102 void copy_stream(FILE * source, FILE * destination);
104 void copy_file(const char * source_file, const char * destination_file);
106 bool directory_is_empty(const char * directory);
108 struct DirListEntry {
110 struct DirListEntry * next;
113 struct DirListEntry * make_recursive_dir_list(const char * directory);
115 void free_dir_list(struct DirListEntry * list);
118 char * strndup(const char * s, size_t size);
121 #ifndef HAVE_VASPRINTF
122 int vasprintf(char ** s, const char * format, va_list a);
125 #ifndef HAVE_ASPRINTF
126 int asprintf(char ** s, const char * format, ...) __attribute__((__format__(printf, 2, 3)));