Works at least without crypto.
[userv-utils] / ipif / utils.c
1 /*
2 */
3
4 #include <string.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <assert.h>
9
10 #include "forwarder.h"
11
12 const char *const *argv;
13 char programid[SYS_NMLN+sizeof(PROGRAM)+3];
14
15 void arg_assert_fail(const char *msg) {
16 fprintf(stderr, PROGRAM ": argument error (!`%s')\n",msg);
17 exit(12);
18 }
19
20 void sysfail(const char *msg) {
21 fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
22 exit(8);
23 }
24
25 void fail(const char *msg) {
26 fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
27 exit(4);
28 }
29
30 void sysdiag(const char *msg) {
31 fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
32 }
33
34 void diag(const char *msg) {
35 fprintf(stderr, "%s: %s\n", programid, msg);
36 }
37
38 time_t now(void) {
39 time_t r;
40 if (time(&r) == (time_t)-1) sysfail("get time of day");
41 return r;
42 }
43
44 void *xmalloc(size_t sz) {
45 void *r;
46 r= malloc(sz);
47 if (!r) sysfail("allocate memory");
48 return r;
49 }
50
51 void write_must(int fd, const void *p_in, int sz, const char *what) {
52 const unsigned char *p= p_in;
53 int r;
54
55 while (sz) {
56 r= write(fd,p,sz);
57 if (r<0) {
58 if (errno == EINTR) continue;
59 else sysfail(what);
60 }
61 assert(r && r <= sz);
62 p += r;
63 sz -= r;
64 }
65 }
66
67 void read_must(int fd, void *p_in, int sz, const char *what) {
68 unsigned char *p= p_in;
69 int r;
70
71 while (sz) {
72 r= read(fd,p,sz);
73 if (r<0) {
74 if (errno == EINTR) continue;
75 else sysfail(what);
76 }
77 if (r==0) fail(what);
78 assert(r <= sz);
79 p += r;
80 sz -= r;
81 }
82 }
83
84 const char *getarg_string(void) {
85 const char *arg;
86
87 arg= *++argv;
88 arg_assert(arg);
89 return arg;
90 }
91
92 unsigned long getarg_ulong(void) {
93 char *ep;
94 unsigned long ul;
95
96 ul= strtoul(getarg_string(),&ep,0);
97 arg_assert(!*ep);
98 return ul;
99 }
100
101 void *buf_append(struct buffer *buf, size_t amount) {
102 void *p;
103
104 p= buf->start + buf->size;
105 buf->size += amount;
106 return p;
107 }
108
109 void *buf_prepend(struct buffer *buf, size_t amount) {
110 buf->size += amount;
111 return buf->start -= amount;
112 }
113
114 void *buf_unappend(struct buffer *buf, size_t amount) {
115 if (buf->size < amount) return 0;
116 return buf->start + (buf->size -= amount);
117 }
118
119 void *buf_unprepend(struct buffer *buf, size_t amount) {
120 void *p;
121
122 p= buf->start;
123 buf->start += amount;
124 buf->size -= amount;
125 return p;
126 }