blob: f4beb560e180a6a60968c2278ea622b6618896e5 [file] [log] [blame]
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001/*
2Copyright (c) 2001-2006, Gerrit Pape
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/*** buffer.h ***/
29
30typedef struct buffer {
31 char *x;
32 unsigned p;
33 unsigned n;
34 int fd;
35 int (*op)(int fd,char *buf,unsigned len);
36} buffer;
37
38#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, (len), (fd), (op) }
39#define BUFFER_INSIZE 8192
40#define BUFFER_OUTSIZE 8192
41
42extern void buffer_init(buffer *,int (*)(int fd,char *buf,unsigned len),int,char *,unsigned);
43
44extern int buffer_flush(buffer *);
45extern int buffer_put(buffer *,const char *,unsigned);
46extern int buffer_putalign(buffer *,const char *,unsigned);
47extern int buffer_putflush(buffer *,const char *,unsigned);
48extern int buffer_puts(buffer *,const char *);
49extern int buffer_putsalign(buffer *,const char *);
50extern int buffer_putsflush(buffer *,const char *);
51
52#define buffer_PUTC(s,c) \
53 ( ((s)->n != (s)->p) \
54 ? ( (s)->x[(s)->p++] = (c), 0 ) \
55 : buffer_put((s),&(c),1) \
56 )
57
58extern int buffer_get(buffer *,char *,unsigned);
59extern int buffer_bget(buffer *,char *,unsigned);
60extern int buffer_feed(buffer *);
61
62extern char *buffer_peek(buffer *);
63extern void buffer_seek(buffer *,unsigned);
64
65#define buffer_PEEK(s) ( (s)->x + (s)->n )
66#define buffer_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) )
67
68#define buffer_GETC(s,c) \
69 ( ((s)->p > 0) \
70 ? ( *(c) = (s)->x[(s)->n], buffer_SEEK((s),1), 1 ) \
71 : buffer_get((s),(c),1) \
72 )
73
74extern int buffer_copy(buffer *,buffer *);
75
76extern int buffer_unixread(int,char *,unsigned);
77/* Actually, int buffer_unixwrite(int,const char *,unsigned),
78 but that 'const' will produce warnings... oh well */
79extern int buffer_unixwrite(int,char *,unsigned);
80
81
82/*** byte.h ***/
83
84extern unsigned byte_chr(char *s,unsigned n,int c);
85
86
87/*** coe.h ***/
88
89extern int coe(int);
90
91
92/*** direntry.h ***/
93
94#define direntry struct dirent
95
96
97/*** fd.h ***/
98
99extern int fd_copy(int,int);
100extern int fd_move(int,int);
101
102
103/*** fifo.h ***/
104
105extern int fifo_make(const char *,int);
106
107
108/*** fmt.h ***/
109
110#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
111#define FMT_LEN ((char *) 0) /* convenient abbreviation */
112
113extern unsigned fmt_uint(char *,unsigned);
114extern unsigned fmt_uint0(char *,unsigned,unsigned);
115extern unsigned fmt_xint(char *,unsigned);
116extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned);
117extern unsigned fmt_ushort(char *,unsigned short);
118extern unsigned fmt_xshort(char *,unsigned short);
119extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short);
120extern unsigned fmt_ulong(char *,unsigned long);
121extern unsigned fmt_xlong(char *,unsigned long);
122extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long);
123
124extern unsigned fmt_plusminus(char *,int);
125extern unsigned fmt_minus(char *,int);
126extern unsigned fmt_0x(char *,int);
127
128extern unsigned fmt_str(char *,const char *);
129extern unsigned fmt_strn(char *,const char *,unsigned);
130
131
132/*** tai.h ***/
133
134struct tai {
135 uint64_t x;
136} ;
137
138#define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
139
140extern void tai_now(struct tai *);
141
142#define tai_approx(t) ((double) ((t)->x))
143
144extern void tai_add(struct tai *,const struct tai *,const struct tai *);
145extern void tai_sub(struct tai *,const struct tai *,const struct tai *);
146#define tai_less(t,u) ((t)->x < (u)->x)
147
148#define TAI_PACK 8
149extern void tai_pack(char *,const struct tai *);
150extern void tai_unpack(const char *,struct tai *);
151
152extern void tai_uint(struct tai *,unsigned);
153
154
155/*** taia.h ***/
156
157struct taia {
158 struct tai sec;
159 unsigned long nano; /* 0...999999999 */
160 unsigned long atto; /* 0...999999999 */
161} ;
162
163extern void taia_tai(const struct taia *,struct tai *);
164
165extern void taia_now(struct taia *);
166
167extern double taia_approx(const struct taia *);
168extern double taia_frac(const struct taia *);
169
170extern void taia_add(struct taia *,const struct taia *,const struct taia *);
171extern void taia_addsec(struct taia *,const struct taia *,int);
172extern void taia_sub(struct taia *,const struct taia *,const struct taia *);
173extern void taia_half(struct taia *,const struct taia *);
174extern int taia_less(const struct taia *,const struct taia *);
175
176#define TAIA_PACK 16
177extern void taia_pack(char *,const struct taia *);
178extern void taia_unpack(const char *,struct taia *);
179
180#define TAIA_FMTFRAC 19
181extern unsigned taia_fmtfrac(char *,const struct taia *);
182
183extern void taia_uint(struct taia *,unsigned);
184
185
186/*** fmt_ptime.h ***/
187
188#define FMT_PTIME 30
189
190extern unsigned fmt_ptime(char *, struct taia *);
191extern unsigned fmt_taia(char *, struct taia *);
192
193
194/*** gen_alloc.h ***/
195
196#define GEN_ALLOC_typedef(ta,type,field,len,a) \
197 typedef struct ta { type *field; unsigned len; unsigned a; } ta;
198
199
200/*** gen_allocdefs.h ***/
201
202#define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
203int ta_ready(ta *x,unsigned n) \
204{ unsigned i; \
205 if (x->field) { \
206 i = x->a; \
207 if (n > i) { \
208 x->a = base + n + (n >> 3); \
209 x->field = realloc(x->field,x->a * sizeof(type)); \
210 if (x->field) return 1; \
211 x->a = i; return 0; } \
212 return 1; } \
213 x->len = 0; \
214 return !!(x->field = malloc((x->a = n) * sizeof(type))); }
215
216#define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
217int ta_rplus(ta *x,unsigned n) \
218{ unsigned i; \
219 if (x->field) { \
220 i = x->a; n += x->len; \
221 if (n > i) { \
222 x->a = base + n + (n >> 3); \
223 x->field = realloc(x->field,x->a * sizeof(type)); \
224 if (x->field) return 1; \
225 x->a = i; return 0; } \
226 return 1; } \
227 x->len = 0; \
228 return !!(x->field = malloc((x->a = n) * sizeof(type))); }
229
230#define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
231int ta_append(ta *x,const type *i) \
232{ if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
233
234
235/*** stralloc.h ***/
Denis Vlasenko8a00f442006-11-17 18:58:16 +0000236#if 0
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000237GEN_ALLOC_typedef(stralloc,char,s,len,a)
238
239extern int stralloc_ready(stralloc *,unsigned);
240extern int stralloc_readyplus(stralloc *,unsigned);
241extern int stralloc_copy(stralloc *,const stralloc *);
242extern int stralloc_cat(stralloc *,const stralloc *);
243extern int stralloc_copys(stralloc *,const char *);
244extern int stralloc_cats(stralloc *,const char *);
245extern int stralloc_copyb(stralloc *,const char *,unsigned);
246extern int stralloc_catb(stralloc *,const char *,unsigned);
247extern int stralloc_append(stralloc *,const char *); /* beware: this takes a pointer to 1 char */
248extern int stralloc_starts(stralloc *,const char *);
249
250#define stralloc_0(sa) stralloc_append(sa,"")
251
252extern int stralloc_catulong0(stralloc *,unsigned long,unsigned);
253extern int stralloc_catlong0(stralloc *,long,unsigned);
254
255#define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
256#define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
257#define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
258#define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
Denis Vlasenko8a00f442006-11-17 18:58:16 +0000259#endif
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000260
261/*** iopause.h ***/
262
263typedef struct pollfd iopause_fd;
264#define IOPAUSE_READ POLLIN
265#define IOPAUSE_WRITE POLLOUT
266
267extern void iopause(iopause_fd *,unsigned,struct taia *,struct taia *);
268
269
270/*** lock.h ***/
271
272extern int lock_ex(int);
273extern int lock_un(int);
274extern int lock_exnb(int);
275
276
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000277/*** open.h ***/
278
279extern int open_read(const char *);
280extern int open_excl(const char *);
281extern int open_append(const char *);
282extern int open_trunc(const char *);
283extern int open_write(const char *);
284
285
286/*** openreadclose.h ***/
Denis Vlasenko8a00f442006-11-17 18:58:16 +0000287#if 0
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000288extern int openreadclose(const char *,stralloc *,unsigned);
Denis Vlasenko8a00f442006-11-17 18:58:16 +0000289#endif
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000290
291/*** pathexec.h ***/
292
293extern void pathexec_run(const char *,char *const *,char *const *);
294extern int pathexec_env(const char *,const char *);
295extern void pathexec(char **);
296
297
298/*** pmatch.h ***/
299
300extern unsigned pmatch(const char *, const char *, unsigned);
301
302
303/*** prot.h ***/
304
305extern int prot_gid(int);
306extern int prot_uid(int);
307
308
309/*** readclose.h ***/
Denis Vlasenko8a00f442006-11-17 18:58:16 +0000310#if 0
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000311extern int readclose_append(int,stralloc *,unsigned);
312extern int readclose(int,stralloc *,unsigned);
Denis Vlasenko8a00f442006-11-17 18:58:16 +0000313#endif
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000314
315/*** scan.h ***/
316
317extern unsigned scan_uint(const char *,unsigned *);
318extern unsigned scan_xint(const char *,unsigned *);
319extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *);
320extern unsigned scan_ushort(const char *,unsigned short *);
321extern unsigned scan_xshort(const char *,unsigned short *);
322extern unsigned scan_nbbshort(const char *,unsigned,unsigned,unsigned,unsigned short *);
323extern unsigned scan_ulong(const char *,unsigned long *);
324extern unsigned scan_xlong(const char *,unsigned long *);
325extern unsigned scan_nbblong(const char *,unsigned,unsigned,unsigned,unsigned long *);
326
327extern unsigned scan_plusminus(const char *,int *);
328extern unsigned scan_0x(const char *,unsigned *);
329
330extern unsigned scan_whitenskip(const char *,unsigned);
331extern unsigned scan_nonwhitenskip(const char *,unsigned);
332extern unsigned scan_charsetnskip(const char *,const char *,unsigned);
333extern unsigned scan_noncharsetnskip(const char *,const char *,unsigned);
334
335extern unsigned scan_strncmp(const char *,const char *,unsigned);
336extern unsigned scan_memcmp(const char *,const char *,unsigned);
337
338extern unsigned scan_long(const char *,long *);
339extern unsigned scan_8long(const char *,unsigned long *);
340
341
342/*** seek.h ***/
343
344typedef unsigned long seek_pos;
345
346extern seek_pos seek_cur(int);
347
348extern int seek_set(int,seek_pos);
349extern int seek_end(int);
350
351extern int seek_trunc(int,seek_pos);
352
353#define seek_begin(fd) (seek_set((fd),(seek_pos) 0))
354
355
356/*** sig.h ***/
357
358extern int sig_alarm;
359extern int sig_child;
360extern int sig_cont;
361extern int sig_hangup;
362extern int sig_int;
363extern int sig_pipe;
364extern int sig_term;
365
366extern void (*sig_defaulthandler)(int);
367extern void (*sig_ignorehandler)(int);
368
369extern void sig_catch(int,void (*)(int));
370#define sig_ignore(s) (sig_catch((s),sig_ignorehandler))
371#define sig_uncatch(s) (sig_catch((s),sig_defaulthandler))
372
373extern void sig_block(int);
374extern void sig_unblock(int);
375extern void sig_blocknone(void);
376extern void sig_pause(void);
377
378extern void sig_dfl(int);
379
380
381/*** str.h ***/
382
383extern unsigned str_chr(const char *,int); /* never returns NULL */
384
385#define str_diff(s,t) strcmp((s),(t))
386#define str_equal(s,t) (!strcmp((s),(t)))
387
388
389/*** wait.h ***/
390
391extern int wait_pid(int *wstat, int pid);
392extern int wait_nohang(int *wstat);
393
394#define wait_crashed(w) ((w) & 127)
395#define wait_exitcode(w) ((w) >> 8)
396#define wait_stopsig(w) ((w) >> 8)
397#define wait_stopped(w) (((w) & 127) == 127)