1 |
/* |
2 |
util.h - 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 |
#ifndef UTIL_H_ |
21 |
#define UTIL_H_ |
22 |
|
23 |
#ifndef HAVE_VASPRINTF |
24 |
#include <stdarg.h> |
25 |
#endif |
26 |
#include <stdbool.h> |
27 |
#include <stdio.h> |
28 |
#include <stdlib.h> |
29 |
|
30 |
#include <sys/stat.h> |
31 |
|
32 |
extern const char * program; |
33 |
|
34 |
void fatal(const char * format, ...) |
35 |
__attribute__((__noreturn__)) |
36 |
__attribute__((__format__(printf, 1, 2))); |
37 |
|
38 |
void fatal_command_line(const char * format, ...) |
39 |
__attribute__((__noreturn__)) |
40 |
__attribute__((__format__(printf, 1, 2))); |
41 |
|
42 |
size_t addst(size_t x, size_t y); |
43 |
|
44 |
size_t mulst(size_t x, size_t y); |
45 |
|
46 |
void * xmalloc(size_t size); |
47 |
|
48 |
#define xnew(type, count) ((type *) xmalloc(mulst((count), sizeof(type)))) |
49 |
|
50 |
void * xrealloc(void * p, size_t size); |
51 |
|
52 |
char * xstrdup(const char * s); |
53 |
|
54 |
char * xstrndup(const char * s, size_t size); |
55 |
|
56 |
int xasprintf(char ** s, const char * template, ...) __attribute__((__format__(printf, 2, 3))); |
57 |
|
58 |
char * xgetcwd(void); |
59 |
|
60 |
FILE * xfopen(const char * file, const char * mode); |
61 |
|
62 |
void xstat(const char * file, struct stat * buf); |
63 |
|
64 |
void xlstat(const char * file, struct stat * buf); |
65 |
|
66 |
void xmkdir(const char * directory); |
67 |
|
68 |
void mkdir_if_necessary(const char * directory); |
69 |
|
70 |
void mkdirs(const char * path); |
71 |
|
72 |
bool str_starts_with(const char * string, const char * prefix); |
73 |
|
74 |
bool str_ends_with(const char * string, const char * suffix); |
75 |
|
76 |
char * make_path(const char * parent, const char * relative_path); |
77 |
|
78 |
char * make_canonical_path(const char * relative_path); |
79 |
|
80 |
char * make_basename(const char * path); |
81 |
|
82 |
char * make_dirname(const char * path); |
83 |
|
84 |
int is_same_file(const char * file1, const char * file2); |
85 |
|
86 |
int contains_file(const char * file1, const char * file2); |
87 |
|
88 |
void copy_stream(FILE * source, FILE * destination); |
89 |
|
90 |
void copy_file(const char * source_file, const char * destination_file); |
91 |
|
92 |
bool directory_is_empty(const char * directory); |
93 |
|
94 |
struct DirListEntry { |
95 |
char * name; |
96 |
struct DirListEntry * next; |
97 |
}; |
98 |
|
99 |
struct DirListEntry * make_recursive_dir_list(const char * directory); |
100 |
|
101 |
void free_dir_list(struct DirListEntry * list); |
102 |
|
103 |
#ifndef HAVE_STRNDUP |
104 |
char * strndup(const char * s, size_t size); |
105 |
#endif |
106 |
|
107 |
#ifndef HAVE_VASPRINTF |
108 |
int vasprintf(char ** s, const char * template, va_list a); |
109 |
#endif |
110 |
|
111 |
#ifndef HAVE_ASPRINTF |
112 |
int asprintf(char ** s, const char * template, ...) __attribute__((__format__(printf, 2, 3))); |
113 |
#endif |
114 |
|
115 |
#endif |