-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdz.c
More file actions
339 lines (292 loc) · 7.46 KB
/
Copy pathstdz.c
File metadata and controls
339 lines (292 loc) · 7.46 KB
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
#include "stdz.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(__unix__)
#include <sys/select.h>
#endif
static const char* _z_progname = "stdz";
// getprogname(3) impl.
const char* z_getprogname(void)
{
return _z_progname;
}
// setprogname(3) impl.
void z_setprogname(const char* progname)
{
_z_progname = z_basename(progname);
}
static int _z_is_pathsep(int c)
{
#if defined(_WIN32) || defined(__CYGWIN__)
return c == '/' || c == '\\';
#else
return c == '/';
#endif
}
// basename(3) GNU impl.
char* z_basename(const char* path)
{
size_t i = strlen(path);
while (!_z_is_pathsep(path[i - 1]))
if (--i == 0)
break;
return (char*)&path[i];
}
// basename(3) XPG impl.
char* z_xpg_basename(char* path)
{
if (path == NULL || *path == 0)
return (char*)".";
char* ptr = path + strlen(path) - 1;
for (; _z_is_pathsep(*ptr); *ptr-- = 0)
if (ptr == path)
return (char*)"/";
for (; !_z_is_pathsep(*ptr); ptr--)
if (ptr == path)
return ptr;
return ptr + 1;
}
// dirname(3) impl.
char* z_dirname(char* path)
{
if (path == NULL || *path == 0)
return (char*)".";
char* ptr = path + strlen(path) - 1;
for (; _z_is_pathsep(*ptr); ptr--)
if (ptr == path)
return (char*)"/";
for (; !_z_is_pathsep(*ptr); ptr--)
if (ptr == path)
return (char*)".";
for (; _z_is_pathsep(*ptr); ptr--)
if (ptr == path)
return (char*)"/";
ptr[1] = 0;
return path;
}
// asprintf(3) impl.
int z_asprintf(char** strp, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
int n = z_vasprintf(strp, fmt, args);
va_end(args);
return n;
}
// vasprintf(3) impl.
int z_vasprintf(char** strp, const char* fmt, va_list args)
{
va_list args_copy;
va_copy(args_copy, args);
int n = vsnprintf(NULL, 0, fmt, args_copy);
if (n < 0)
z_error(EXIT_FAILURE, errno, "asprintf(%s)", fmt);
va_end(args_copy);
return vsnprintf(*strp = (char*)z_malloc(n + 1), n + 1, fmt, args);
}
// getdelim(3) impl.
ssize_t z_getdelim(char** linep, size_t* n, int delimiter, FILE* stream)
{
if (linep == NULL || n == NULL)
z_error(EXIT_FAILURE, EINVAL, "getdelim(%p, %p)", linep, n);
size_t sz = (linep != NULL) ? *n : 0, offset = 0;
for (;;) {
int c = fgetc(stream);
if (c == EOF)
break;
if (offset + 1 >= sz) {
if (sz >= INT_MAX)
z_error(EXIT_FAILURE, ENOMEM, "getdelim(%zu)", sz);
*linep = (char*)z_realloc(*linep, sz += sz/2 + 16);
}
(*linep)[offset++] = (char)c;
if (c == delimiter)
break;
}
*n = sz;
if (offset == 0 || ferror(stream))
return -1;
(*linep)[offset] = 0;
return offset;
}
// getline(3) impl.
ssize_t z_getline(char** linep, size_t* n, FILE* stream)
{
return z_getdelim(linep, n, '\n', stream);
}
// fopen(3) with error checking
FILE* z_fopen(const char* fname, const char* mode)
{
if (fname == NULL || (fname[0] == '-' && fname[1] == 0)) {
while (*mode == ' ')
++mode;
if (*mode == 'r')
return stdin;
if (*mode == 'w' || *mode == 'a')
return stdout;
z_error(EXIT_FAILURE, EINVAL, "fopen(%s, %s)", "-", mode);
}
FILE* f = fopen(fname, mode);
if (f == NULL)
z_error(EXIT_FAILURE, errno, "fopen(%s, %s)", fname, mode);
return f;
}
// malloc(3) with error checking
void* z_malloc(size_t n)
{
return z_realloc(NULL, n);
}
// realloc(3) with error checking
void* z_realloc(void* ptr, size_t n)
{
if (n == 0) {
free(ptr);
return NULL;
}
ptr = realloc(ptr, n);
if (ptr == NULL)
z_error(EXIT_FAILURE, errno, "realloc(%zu)", n);
return ptr;
}
// strcasecmp(3) impl.
int z_strcasecmp(const char* str1, const char* str2)
{
return z_strncasecmp(str1, str2, INT_MAX);
}
// strncasecmp(3) impl.
int z_strncasecmp(const char* str1, const char* str2, size_t n)
{
const uint8_t* ptr1 = (const uint8_t*)str1;
const uint8_t* ptr2 = (const uint8_t*)str2;
if (ptr1 == ptr2)
return 0;
for (; n > 0; --n) {
int c1 = tolower(*ptr1++), c2 = tolower(*ptr2++);
if (c1 == 0 || c1 != c2)
return c1 - c2;
}
return 0;
}
// strchrnul(3) impl.
char* z_strchrnul(const char* str, int c)
{
for (; *str != 0 && *(uint8_t*)str != c; ++str) ;
return (char*)str;
}
// strdup(3) impl.
char* z_strdup(const char* str)
{
return z_strndup(str, INT_MAX);
}
// strndup(3) impl.
char* z_strndup(const char* str, size_t n)
{
const char* end = (const char*)memchr(str, 0, n);
if (end != NULL)
n = end - str;
char* str_copy = (char*)memcpy(z_malloc(n + 1), str, n);
str_copy[n] = 0;
return str_copy;
}
// memccpy(3) impl.
void* z_memccpy(void* dst, const void* src, int c, size_t n)
{
uint8_t* ptr1 = (uint8_t*)dst;
const uint8_t* ptr2 = (const uint8_t*)src;
for (; n > 0; --n)
if ((*ptr1++ = *ptr2++) == c)
return ptr1;
return NULL;
}
// strscpy(9) impl.
ssize_t z_strscpy(char* dst, const char* src, size_t n)
{
if ((ssize_t)n <= 0)
return -E2BIG;
char* ptr = (char*)z_memccpy(dst, src, 0, n);
return (ptr != NULL) ? (ptr - 1 - dst) : (dst[n - 1] = 0, -E2BIG);
}
// stpecpy(7) impl.
char* z_stpecpy(char* dst, char* end, const char* src)
{
if (dst == NULL || dst >= end)
return NULL;
char* ptr = (char*)z_memccpy(dst, src, 0, end - dst);
return (ptr != NULL) ? (ptr - 1) : (end[-1] = 0, (char*)NULL);
}
// strerror_r(3) impl.
int z_strerror_r(int errnum, char* buf, size_t n)
{
if (errnum > 0) {
z_strscpy(buf, strerror(errnum), n);
return 0;
}
#if defined(_WIN32)
DWORD dwError;
if (errnum == 0 && (dwError = GetLastError()) != ERROR_SUCCESS)
return FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwError, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), buf, (DWORD)n,
NULL) ? 0 : ERANGE;
#endif
return EINVAL;
}
// delay(milliseconds)
void z_delay(uint32_t ms)
{
#if defined(_WIN32)
Sleep(ms);
#elif defined(__unix__)
select(0, NULL, NULL, NULL, &(struct timeval){ .tv_sec = ms / 1000,
.tv_usec = (ms % 1000) * 1000 });
#endif
}
// error(3) impl.
void z_error(int status, int errnum, const char* fmt, ...)
{
fflush(stdout);
va_list args;
va_start(args, fmt);
z__vwarnx(fmt, args);
va_end(args);
char buf[128];
if (z_strerror_r(errnum, buf, sizeof(buf)) == 0)
fprintf(stderr, ": %s", buf);
fputc('\n', stderr);
if (status != 0)
exit(status);
}
// warnx(3) impl.
void z_warnx(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
z__vwarnx(fmt, args);
fputc('\n', stderr);
va_end(args);
}
// like warnx(3) but no newline
void z__warnx(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
z__vwarnx(fmt, args);
va_end(args);
}
// like vwarnx(3) but no newline
void z__vwarnx(const char* fmt, va_list args)
{
fprintf(stderr, "%s: ", z_getprogname());
if (fmt != NULL)
vfprintf(stderr, fmt, args);
}
// getopt(3) impl.
#define GETOPT_PREFIX z_
#define GETOPT_PRINTN(...) z__warnx(__VA_ARGS__)
#define GETOPT_PRINTLN(...) z_warnx(__VA_ARGS__)
#if !defined(__has_include)
// hope for the best
#include "getopt.c"
#elif __has_include("getopt.c")
#include "getopt.c"
#endif