Debian Codegraph @

src/utils.c

raw
1 /*
2 * Copyright © 2013 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include "utils.h"
11
12 #ifdef HAVE_MMAP
13
14 #include <unistd.h>
15 #include <sys/mman.h>
16 #include <sys/types.h>
17
18 bool
19 map_file(FILE *file, char **string_out, size_t *size_out)
20 {
21 struct stat stat_buf;
22 int fd;
23 char *string;
24
25 /* Make sure to keep the errno on failure! */
26 fd = fileno(file);
27 if (fd < 0)
28 return false;
29
30 if (fstat(fd, &stat_buf) != 0)
31 return false;
32
33 string = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
34 if (string == MAP_FAILED)
35 return false;
36
37 *string_out = string;
38 *size_out = stat_buf.st_size;
39 return true;
40 }
41
42 void
43 unmap_file(char *str, size_t size)
44 {
45 munmap(str, size);
46 }
47
48 #else
49
50 bool
51 map_file(FILE *file, char **string_out, size_t *size_out)
52 {
53 long ret;
54 size_t ret_s;
55 char *string;
56 size_t size;
57
58 /* Make sure to keep the errno on failure! */
59
60 ret = fseek(file, 0, SEEK_END);
61 if (ret != 0)
62 return false;
63
64 ret = ftell(file);
65 if (ret < 0)
66 return false;
67 size = (size_t) ret;
68
69 ret = fseek(file, 0, SEEK_SET);
70 if (ret < 0)
71 return false;
72
73 string = malloc(size);
74 if (!string)
75 return false;
76
77 ret_s = fread(string, 1, size, file);
78 if (ret_s < size) {
79 free(string);
80 return false;
81 }
82
83 *string_out = string;
84 *size_out = size;
85 return true;
86 }
87
88 void
89 unmap_file(char *str, size_t size)
90 {
91 free(str);
92 }
93
94 #endif
95
96 /* Open a file and ensure it is a regular file.
97 * Returns NULL in case of error. */
98 FILE*
99 open_file(const char *path)
100 {
101 if (!path)
102 return NULL;
103
104 int fd = open(path, O_RDONLY);
105 if (fd < 0)
106 return NULL;
107
108 struct stat stat_buf;
109 int err = fstat(fd, &stat_buf);
110
111 if (err != 0 || !S_ISREG(stat_buf.st_mode)) {
112 close(fd);
113 return NULL;
114 }
115
116 /* While unlikely to happen, if `fp` is NULL then we must close the file
117 * descriptor. This is poorly documented and missing from the man page; see instead:
118 * https://www.ibm.com/docs/en/i/7.3?topic=functions-fdopen-associates-stream-file-descriptor)
119 * Confirmed by Peter to be necessary in glibc. */
120 FILE *fp = fdopen(fd, "rb");
121 if (fp == NULL) {
122 close(fd);
123 }
124
125 return fp;
126 }
127
128 // ASCII lower-case map.
129 static const unsigned char lower_map[] = {
130 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
131 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
132 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
133 59, 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
134 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
135 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
136 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
137 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
138 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
139 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
140 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
141 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
142 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
143 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
144 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
145 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
146 };
147
148 // ASCII tolower (to avoid locale issues).
149 char
150 to_lower(char c)
151 {
152 return (char) lower_map[(unsigned char) c];
153 }
154
155 // ASCII strcasecmp (to avoid locale issues).
156 int
157 istrcmp(const char *a, const char *b)
158 {
159 for (size_t i = 0; ; i++) {
160 if (to_lower(a[i]) != to_lower(b[i]))
161 return (int) to_lower(a[i]) - (int) to_lower(b[i]);
162 if (!a[i])
163 break;
164 }
165 return 0;
166 }
167
168 // ASCII strncasecmp (to avoid locale issues).
169 int
170 istrncmp(const char *a, const char *b, size_t n)
171 {
172 for (size_t i = 0; i < n; i++) {
173 if (to_lower(a[i]) != to_lower(b[i]))
174 return (int) to_lower(a[i]) - (int) to_lower(b[i]);
175 if (!a[i])
176 break;
177 }
178 return 0;
179 }
180
181 #if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF)
182 int
183 asprintf(char **strp, const char *fmt, ...)
184 {
185 int ret;
186 va_list ap;
187 va_start(ap, fmt);
188 ret = vasprintf(strp, fmt, ap);
189 va_end(ap);
190 return ret;
191 }
192
193 # if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF)
194 int
195 vasprintf(char **strp, const char *fmt, va_list ap)
196 {
197 int ret;
198 char *buf;
199 va_list ap_copy;
200
201 /*
202 * The value of the va_list parameter is undefined after the call to
203 * vsnprintf() returns: pass a copy to make sure "ap" remains valid.
204 */
205 va_copy(ap_copy, ap);
206 ret = vsnprintf(NULL, 0, fmt, ap_copy);
207 va_end(ap_copy);
208
209 if (ret < 0)
210 return ret;
211
212 if (!(buf = malloc(ret + 1)))
213 return -1;
214
215 if ((ret = vsnprintf(buf, ret + 1, fmt, ap)) < 0) {
216 free(buf);
217 return ret;
218 }
219
220 *strp = buf;
221 return ret;
222 }
223 # endif /* !HAVE_VASPRINTF */
224 #endif /* !HAVE_ASPRINTF */