-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.c
More file actions
700 lines (584 loc) · 12.7 KB
/
common.c
File metadata and controls
700 lines (584 loc) · 12.7 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Synopsis:
* Common, interruptable system calls, die[123]() and _strcat().
* Usage:
* char *progname = "append-brr";
*
* #define COMMON_NEED_READ
* #define COMMON_NEED_DIE3
* #include "../../common.c"
*
* #define COMMON_NEED_READ
* #define EXIT_BAD_READ 4
* #include "../../common.c"
* See:
* flip-tail and c programs in schema directories.
* Note:
* Deprecated these function with the libjmscott versions.
* Move {common,common-epcg}.c to $JMSCOTT_ROOT/lib
*/
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
// Note:
// should be PIPE_BUF or COMMON_ATOMIC_MSG_SIZE
#ifndef PIPE_MAX
#define PIPE_MAX 4096
#endif
#if defined(COMMON_NEED_SLURP)
#define COMMON_NEED_CLOSE
#define COMMON_NEED_DIE2
#define COMMON_NEED_OPEN
#define COMMON_NEED_READ_BLOB
#define COMMON_NEED_STAT
#endif
#if defined (COMMON_NEED_FILE_EXISTS)
#define COMMON_NEED_STAT
#endif
#if defined(COMMON_NEED_READ_BLOB)
#define COMMON_NEED_READ
#endif
#if defined(COMMON_NEED_READ) || \
defined(COMMON_NEED_READ_BLOB) || \
defined(COMMON_NEED_WRITE) || \
defined(COMMON_NEED_CLOSE) || \
defined(COMMON_NEED_FCHMOD) || \
defined(COMMON_NEED_STAT)
#define COMMON_NEED_DIE2
#endif
#if defined(COMMON_NEED_OPEN)
#define COMMON_NEED_DIE3
#endif
#if defined(COMMON_NEED_A2UI32)
#define COMMON_NEED_DIE3
#endif
#if defined(COMMON_NEED_UNLINK)
#define COMMON_NEED_DIE2
#endif
#if defined(COMMON_NEED_DIE2)
#define COMMON_NEED_DIE
#endif
/*
* Synopsis:
* Fast, safe and simple string concatenator
* Usage:
* buf[0] = 0
* _strcat(buf, sizeof buf, "hello, world");
* _strcat(buf, sizeof buf, ": ");
* _strcat(buf, sizeof buf, "good bye, cruel world");
*/
static void
_strcat(char *restrict tgt, int tgtsize, const char *restrict src)
{
// find null terminated end of target buffer
while (*tgt++)
--tgtsize;
--tgt;
// copy non-null src bytes, leaving room for trailing null
while (--tgtsize > 0 && *src)
*tgt++ = *src++;
// target always null terminated
*tgt = 0;
}
/*
* Write error message to standard error and exit process with status code.
*/
static void
die(int status, char *msg1)
{
char msg[PIPE_MAX];
static char ERROR[] = "ERROR: ";
static char colon[] = ": ";
static char nl[] = "\n";
msg[0] = 0;
_strcat(msg, sizeof msg, progname);
_strcat(msg, sizeof msg, colon);
_strcat(msg, sizeof msg, ERROR);
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, nl);
write(2, msg, strlen(msg));
_exit(status);
}
#ifdef COMMON_NEED_DIE2
static void
die2(int status, char *msg1, char *msg2)
{
static char colon[] = ": ";
char msg[PIPE_MAX];
msg[0] = 0;
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, colon);
_strcat(msg, sizeof msg, msg2);
die(status, msg);
}
#endif
#ifdef COMMON_NEED_DIE3
static void
die3(int status, char *msg1, char *msg2, char *msg3)
{
static char colon[] = ": ";
char msg[PIPE_MAX];
msg[0] = 0;
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, colon);
_strcat(msg, sizeof msg, msg2);
_strcat(msg, sizeof msg, colon);
_strcat(msg, sizeof msg, msg3);
die(status, msg);
}
#endif
/*
* To include _read() add the following to source which includes
* this file:
*
* #define COMMON_NEED_READ
* #define EXIT_BAD_READ 3 // any code is ok
*/
#ifdef COMMON_NEED_READ
/*
* read() bytes from file fd, restarting on interrupt and dieing on error.
*/
static ssize_t
_read(int fd, void *p, ssize_t nbytes)
{
ssize_t nb;
again:
nb = read(fd, p, nbytes);
if (nb >= 0)
return nb;
if (errno == EINTR) // try read()
goto again;
die2(EXIT_BAD_READ, "read() failed", strerror(errno));
/*NOTREACHED*/
return -1;
}
#endif
/*
* To include _read() add the following to source which includes
* this file:
*
* #define COMMON_NEED_READ_BLOB
* #define EXIT_BLOB_SMALL 4 // any code is ok
* #define EXIT_BLOB_BIG 5 // any code is ok
*/
#ifdef COMMON_NEED_READ_BLOB
/*
* _read() exactly n bytes from fd, die() if too few or too many bytes exist.
*
* Note:
* the name "_read_blob" implies we ought to also verify the blob.
* maybe something like _read_exact() would be a better name?
*/
static void
_read_blob(int fd, void *blob, ssize_t size)
{
int nread = 0, nr;
again:
nr = _read(fd, blob + nread, size - nread);
if (nr > 0) {
nread += nr;
if (nread < size)
goto again;
}
if (nread < size)
die(EXIT_BLOB_SMALL, "blob too small");
/*
* Verify no bytes remain
*/
if (_read(fd, blob, 1) != 0)
die(EXIT_BLOB_BIG, "blob too big");
}
#endif
/*
* To include _write() add the following to source which includes
* this file:
*
* #define COMMON_NEED_WRITE
* #define EXIT_BAD_WRITE 4 // any code is ok
*/
#ifdef COMMON_NEED_WRITE
/*
* write() exactly nbytes bytes, restarting on interrupt and dieing on error.
*/
static void
_write(int fd, void *p, ssize_t nbytes)
{
int nb = 0;
again:
nb = write(fd, p + nb, nbytes);
if (nb < 0) {
if (errno == EINTR)
goto again;
die2(EXIT_BAD_WRITE, "write() failed", strerror(errno));
}
nbytes -= nb;
if (nbytes > 0)
goto again;
}
#endif
/*
* To include _open() add the following to source which includes
* this file:
*
* #define COMMON_NEED_OPEN
* #define EXIT_BAD_OPEN 4 // any code is ok
*/
#ifdef COMMON_NEED_OPEN
/*
* open() a file.
*/
static int
_open(char *path, int oflag, int mode)
{
int fd;
again:
fd = open(path, oflag, mode);
if (fd < 0) {
if (errno == EINTR)
goto again;
die3(EXIT_BAD_OPEN, "open() failed", strerror(errno), path);
}
return fd;
}
#endif
/*
* To include _close() add the following to source which includes
* this file:
*
* #define COMMON_NEED_CLOSE
* #define EXIT_BAD_CLOSE 4 // any code is ok
*/
#ifdef COMMON_NEED_CLOSE
/*
* close() a file descriptor.
*/
static void
_close(int fd)
{
again:
if (close(fd) < 0) {
if (errno == EINTR)
goto again;
die2(EXIT_BAD_CLOSE, "close() failed", strerror(errno));
}
}
#endif
/*
* To include _stat() add the following to source which includes
* this file:
*
* #define COMMON_NEED_STAT
* #define EXIT_BAD_STAT 4 // any code is ok
*/
#ifdef COMMON_NEED_STAT
#include <sys/stat.h>
/*
* stat() a file descriptor, abort on error.
* return 1 if the file exists, 0 otherwise.
* otherwise return the size of the file.
*/
static int
_stat(char *path, struct stat *st)
{
again:
if (stat(path, st) < 0) {
if (errno == EINTR)
goto again;
if (errno == ENOENT)
return 0;
die2(EXIT_BAD_STAT, "stat() failed", strerror(errno));
}
return 1;
}
#endif
/*
* To include _file_exists() add the following to source which includes
* this file:
*
* #define COMMON_NEED_FILE_EXISTS
*/
#ifdef COMMON_NEED_FILE_EXISTS
/*
* determine if file exists using stat() system call.
*/
static int
_file_exists(char *path)
{
struct stat st;
return _stat(path, &st) == 1;
}
#endif
/*
* To include _slurp() add the following to source which includes
* this file:
*
* #define COMMON_NEED_SLURP
*/
#ifdef COMMON_NEED_SLURP
#include <sys/stat.h>
/*
* slurp a stable file into a buffer, reallocing the buffer and size as needed.
* allocate an extra byte for null termination.
* return the number of bytes read.
*/
static ssize_t
_slurp(char *path, void **buf, ssize_t *buf_size)
{
struct stat st;
_stat(path, &st);
if (st.st_size + 1 > *buf_size) {
void *p = realloc(*buf, st.st_size + 1);
if (p == NULL)
die(EXIT_BAD_MALLOC, "_slurp: realloc() failed");
*buf = p;
*buf_size = st.st_size + 1;
}
int fd = _open(path, O_RDONLY, 0);
_read_blob(fd, *buf, st.st_size);
_close(fd);
return st.st_size;
}
#endif
/*
* To include _fchmod() add the following to source which includes
* this file:
*
* #define COMMON_NEED_FCHMOD
* #define EXIT_BAD_FCHMOD 11 // any code is ok
*/
#ifdef COMMON_NEED_FCHMOD
static void
_fchmod(int fd, int mode)
{
again:
if (fchmod(fd, mode) < 0) {
if (errno == EINTR)
goto again;
die2(EXIT_BAD_FCHMOD, "fchmod() failed", strerror(errno));
}
}
#endif
#ifdef COMMON_NEED_ULLTOA
/*
* Convert unsigned long long to decimal ascii string.
* Return the pointer to byte after final digit:
*
* *ulltoa(ull, digits) = 0;
*/
static char *
ulltoa(unsigned long long ull, char *digits)
{
char const digit[] = "0123456789";
char* p, *end_p;
unsigned long long power = ull;
// find the end of the formated ascii string
p = digits;
do
{
++p;
power = power / 10;
} while (power);
end_p = p;
// in reverse order, replace byte with the ascii chars
do
{
*--p = digit[ull % 10];
ull = ull / 10;
} while (ull);
return end_p;
}
#endif
#ifdef COMMON_NEED_A2UI32
/*
* Synopsis:
* Strictly parse an ascii string representing an unsigned 32 bit int
* in the range 0 <= 4294967295. die() upon error.
* Usage:
* blob_size = a2ui32(argv[1], "blob size", 1);
*/
unsigned int
a2ui32(char *src, char *what, int die_status)
{
int len = 0;
char *p = src, c;
long ll;
/*
* Note:
* Must be a compile time option!
*/
if (sizeof (long int) < 8)
die(255, "sizeof (long int) < 8");
// verify source is <= 10 digits
while ((c = *p++)) {
if (c < '0' || c > '9')
die3(die_status, what, "not a cardinal number", src);
if (len++ > 10)
die3(die_status, what, "cardinal > 10 digits", src);
}
// build the long long int number
ll = 0;
p = src;
while ((c = *p++))
ll = ll * 10 + (c - '0');
// is the number too big?
if (ll > 4294967295)
die2(die_status, what, "> 4294967295");
return (unsigned int)ll;
}
#endif
#ifdef COMMON_NEED_IS_UDIG
#include <ctype.h>
/*
* Is a syntactically correct udig.
*/
static int
_is_udig(char *udig)
{
int len = strlen(udig);
if (len < 30 || len > 137)
return 0;
// first char is lower case ascii alpha.
char c = *udig;
if (!isascii(c) || !islower(c))
return 0;
// insure algorithm after second char is lower case alnum
// terminated by colon < 8 chars.
char *p = udig + 1, *p_end = udig + 8;
while (p < p_end) {
c = *p++;
if (c == ':')
break;
if (!isascii(c) || !(isdigit(c) || islower(c)))
return 0;
}
// verify digest is 1 to 128 chars of ascii() isgraph() chars
p_end = udig + len;
while (p < p_end) {
c = *p++;
if (!isascii(c) || !isgraph(c))
return 0;
}
return 1;
}
#endif
#ifdef COMMON_NEED_UNLINK
int
_unlink(char *path)
{
again:
if (unlink(path) == 0)
return 0;
if (errno == ENOENT)
return 1;
if (errno != EINTR)
die2(EXIT_BAD_UNLINK, "unlink() failed", strerror(errno));
goto again;
}
#endif
#ifdef COMMON_NEED_HEXDUMP
#include <ctype.h>
#include <stdio.h>
/*
* Ye'ol hex dump.
*
* Derived from a hexdump written by Andy Fullford, eons ago.
*
* https://github.com/akfullfo
*/
void
hexdump(int fd, unsigned char *src, int src_size, char direction)
{
char *t;
unsigned char *s, *s_end;
int need;
char tbuf[64 * 1024], *tgt;
int tgt_size;
char buf[1024];
if (src_size == 0) {
static char zero[] = "hexdump: 0 bytes in source buffer\n";
write(fd, zero, sizeof zero - 1);
return;
}
tgt = tbuf;
tgt_size = sizeof tbuf;
static char hexchar[] = "0123456789abcdef";
/*
* Insure we have plenty of room in the target buffer.
*
* Each line of ascii output formats up to 16 bytes in the source
* buffer, so the number of lines is the ceiling(src_size/16).
* The number of characters per line is 76 characters plus the
* trailing new line.
*
* ((src_size - 1) / 16 + 1) * (76 + 1) + 1
*
* with the entire ascii string terminated with a null byte.
*/
need = ((src_size - 1) / 16 + 1) * (76 + 1) + 1;
/*
* Not enough space in the target buffer ... probably ought
* to truncate instead of punting.
*/
if (need > tgt_size) {
snprintf(tgt, tgt_size,
"hexdump: source bigger than target: %d > %d\n", need,
tgt_size);
write(fd, tgt, strlen(tgt));
return;
}
s = src;
s_end = s + src_size;
t = tgt;
while (s < s_end) {
int i;
/*
* Start of line in the target.
*/
char *l = t;
/*
* Write the offset into the source buffer.
*/
sprintf(l, "%6lu", (long unsigned)(s - src));
/*
* Write direction of flow
*/
l[6] = l[7] = ' ';
l[8] = direction;
l[9] = ' ';
/*
* Format up to next 16 bytes from the source.
*/
for (i = 0; s < s_end && i < 16; i++) {
l[10 + i * 3] = ' ';
l[10 + i * 3 + 1] = hexchar[(*s >> 4) & 0x0F];
l[10 + i * 3 + 2] = hexchar[*s & 0x0F];
if (isprint(*s))
l[60 + i] = *s;
else
l[60 + i] = '.';
s++;
}
/*
* Pad out last line with white space.
*/
while (i < 16) {
l[10 + i * 3] =
l[10 + i * 3 + 1] =
l[10 + i * 3 + 2] = ' ';
l[60 + i] = ' ';
i++;
}
l[58] = l[59] = ' ';
l[76] = '\n';
t = l + 77;
}
tgt[need - 1] = 0;
snprintf(buf, sizeof buf, "\ndump of %d bytes\n", src_size);
write(fd, buf, strlen(buf));
write(fd, tgt, strlen(tgt));
write(fd, "\n", 1);
}
#endif