2 Copyright (c) 2001-2006, Gerrit Pape
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 typedef struct buffer {
35 int (*op)(int fd,char *buf,unsigned len);
38 #define BUFFER_INIT(op,fd,buf,len) { (buf), 0, (len), (fd), (op) }
39 #define BUFFER_INSIZE 8192
40 #define BUFFER_OUTSIZE 8192
42 extern void buffer_init(buffer *,int (*)(int fd,char *buf,unsigned len),int,char *,unsigned);
44 extern int buffer_flush(buffer *);
45 extern int buffer_put(buffer *,const char *,unsigned);
46 extern int buffer_putalign(buffer *,const char *,unsigned);
47 extern int buffer_putflush(buffer *,const char *,unsigned);
48 extern int buffer_puts(buffer *,const char *);
49 extern int buffer_putsalign(buffer *,const char *);
50 extern int buffer_putsflush(buffer *,const char *);
52 #define buffer_PUTC(s,c) \
53 ( ((s)->n != (s)->p) \
54 ? ( (s)->x[(s)->p++] = (c), 0 ) \
55 : buffer_put((s),&(c),1) \
58 extern int buffer_get(buffer *,char *,unsigned);
59 extern int buffer_bget(buffer *,char *,unsigned);
60 extern int buffer_feed(buffer *);
62 extern char *buffer_peek(buffer *);
63 extern void buffer_seek(buffer *,unsigned);
65 #define buffer_PEEK(s) ( (s)->x + (s)->n )
66 #define buffer_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) )
68 #define buffer_GETC(s,c) \
70 ? ( *(c) = (s)->x[(s)->n], buffer_SEEK((s),1), 1 ) \
71 : buffer_get((s),(c),1) \
74 extern int buffer_copy(buffer *,buffer *);
76 extern int buffer_unixread(int,char *,unsigned);
77 /* Actually, int buffer_unixwrite(int,const char *,unsigned),
78 but that 'const' will produce warnings... oh well */
79 extern int buffer_unixwrite(int,char *,unsigned);
84 extern unsigned byte_chr(char *s,unsigned n,int c);
94 #define direntry struct dirent
99 extern int fd_copy(int,int);
100 extern int fd_move(int,int);
105 extern int fifo_make(const char *,int);
110 #define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
111 #define FMT_LEN ((char *) 0) /* convenient abbreviation */
113 extern unsigned fmt_uint(char *,unsigned);
114 extern unsigned fmt_uint0(char *,unsigned,unsigned);
115 extern unsigned fmt_xint(char *,unsigned);
116 extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned);
117 extern unsigned fmt_ushort(char *,unsigned short);
118 extern unsigned fmt_xshort(char *,unsigned short);
119 extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short);
120 extern unsigned fmt_ulong(char *,unsigned long);
121 extern unsigned fmt_xlong(char *,unsigned long);
122 extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long);
124 extern unsigned fmt_plusminus(char *,int);
125 extern unsigned fmt_minus(char *,int);
126 extern unsigned fmt_0x(char *,int);
128 extern unsigned fmt_str(char *,const char *);
129 extern unsigned fmt_strn(char *,const char *,unsigned);
138 #define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
140 extern void tai_now(struct tai *);
142 #define tai_approx(t) ((double) ((t)->x))
144 extern void tai_add(struct tai *,const struct tai *,const struct tai *);
145 extern void tai_sub(struct tai *,const struct tai *,const struct tai *);
146 #define tai_less(t,u) ((t)->x < (u)->x)
149 extern void tai_pack(char *,const struct tai *);
150 extern void tai_unpack(const char *,struct tai *);
152 extern void tai_uint(struct tai *,unsigned);
159 unsigned long nano; /* 0...999999999 */
160 unsigned long atto; /* 0...999999999 */
163 extern void taia_tai(const struct taia *,struct tai *);
165 extern void taia_now(struct taia *);
167 extern double taia_approx(const struct taia *);
168 extern double taia_frac(const struct taia *);
170 extern void taia_add(struct taia *,const struct taia *,const struct taia *);
171 extern void taia_addsec(struct taia *,const struct taia *,int);
172 extern void taia_sub(struct taia *,const struct taia *,const struct taia *);
173 extern void taia_half(struct taia *,const struct taia *);
174 extern int taia_less(const struct taia *,const struct taia *);
177 extern void taia_pack(char *,const struct taia *);
178 extern void taia_unpack(const char *,struct taia *);
180 #define TAIA_FMTFRAC 19
181 extern unsigned taia_fmtfrac(char *,const struct taia *);
183 extern void taia_uint(struct taia *,unsigned);
186 /*** fmt_ptime.h ***/
190 extern unsigned fmt_ptime(char *, struct taia *);
191 extern unsigned fmt_taia(char *, struct taia *);
194 /*** gen_alloc.h ***/
196 #define GEN_ALLOC_typedef(ta,type,field,len,a) \
197 typedef struct ta { type *field; unsigned len; unsigned a; } ta;
200 /*** gen_allocdefs.h ***/
202 #define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
203 int ta_ready(ta *x,unsigned n) \
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; } \
214 return !!(x->field = malloc((x->a = n) * sizeof(type))); }
216 #define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
217 int ta_rplus(ta *x,unsigned n) \
220 i = x->a; n += x->len; \
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; } \
228 return !!(x->field = malloc((x->a = n) * sizeof(type))); }
230 #define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
231 int ta_append(ta *x,const type *i) \
232 { if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
237 GEN_ALLOC_typedef(stralloc,char,s,len,a)
239 extern int stralloc_ready(stralloc *,unsigned);
240 extern int stralloc_readyplus(stralloc *,unsigned);
241 extern int stralloc_copy(stralloc *,const stralloc *);
242 extern int stralloc_cat(stralloc *,const stralloc *);
243 extern int stralloc_copys(stralloc *,const char *);
244 extern int stralloc_cats(stralloc *,const char *);
245 extern int stralloc_copyb(stralloc *,const char *,unsigned);
246 extern int stralloc_catb(stralloc *,const char *,unsigned);
247 extern int stralloc_append(stralloc *,const char *); /* beware: this takes a pointer to 1 char */
248 extern int stralloc_starts(stralloc *,const char *);
250 #define stralloc_0(sa) stralloc_append(sa,"")
252 extern int stralloc_catulong0(stralloc *,unsigned long,unsigned);
253 extern int stralloc_catlong0(stralloc *,long,unsigned);
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))
263 typedef struct pollfd iopause_fd;
264 #define IOPAUSE_READ POLLIN
265 #define IOPAUSE_WRITE POLLOUT
267 extern void iopause(iopause_fd *,unsigned,struct taia *,struct taia *);
272 extern int lock_ex(int);
273 extern int lock_un(int);
274 extern int lock_exnb(int);
279 extern int open_read(const char *);
280 extern int open_excl(const char *);
281 extern int open_append(const char *);
282 extern int open_trunc(const char *);
283 extern int open_write(const char *);
286 /*** openreadclose.h ***/
288 extern int openreadclose(const char *,stralloc *,unsigned);
293 extern void pathexec_run(const char *,char *const *,char *const *);
294 extern int pathexec_env(const char *,const char *);
295 extern void pathexec(char **);
300 extern unsigned pmatch(const char *, const char *, unsigned);
305 extern int prot_gid(int);
306 extern int prot_uid(int);
309 /*** readclose.h ***/
311 extern int readclose_append(int,stralloc *,unsigned);
312 extern int readclose(int,stralloc *,unsigned);
317 extern unsigned scan_uint(const char *,unsigned *);
318 extern unsigned scan_xint(const char *,unsigned *);
319 extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *);
320 extern unsigned scan_ushort(const char *,unsigned short *);
321 extern unsigned scan_xshort(const char *,unsigned short *);
322 extern unsigned scan_nbbshort(const char *,unsigned,unsigned,unsigned,unsigned short *);
323 extern unsigned scan_ulong(const char *,unsigned long *);
324 extern unsigned scan_xlong(const char *,unsigned long *);
325 extern unsigned scan_nbblong(const char *,unsigned,unsigned,unsigned,unsigned long *);
327 extern unsigned scan_plusminus(const char *,int *);
328 extern unsigned scan_0x(const char *,unsigned *);
330 extern unsigned scan_whitenskip(const char *,unsigned);
331 extern unsigned scan_nonwhitenskip(const char *,unsigned);
332 extern unsigned scan_charsetnskip(const char *,const char *,unsigned);
333 extern unsigned scan_noncharsetnskip(const char *,const char *,unsigned);
335 extern unsigned scan_strncmp(const char *,const char *,unsigned);
336 extern unsigned scan_memcmp(const char *,const char *,unsigned);
338 extern unsigned scan_long(const char *,long *);
339 extern unsigned scan_8long(const char *,unsigned long *);
344 typedef unsigned long seek_pos;
346 extern seek_pos seek_cur(int);
348 extern int seek_set(int,seek_pos);
349 extern int seek_end(int);
351 extern int seek_trunc(int,seek_pos);
353 #define seek_begin(fd) (seek_set((fd),(seek_pos) 0))
358 extern int sig_alarm;
359 extern int sig_child;
361 extern int sig_hangup;
366 extern void (*sig_defaulthandler)(int);
367 extern void (*sig_ignorehandler)(int);
369 extern 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))
373 extern void sig_block(int);
374 extern void sig_unblock(int);
375 extern void sig_blocknone(void);
376 extern void sig_pause(void);
378 extern void sig_dfl(int);
383 extern unsigned str_chr(const char *,int); /* never returns NULL */
385 #define str_diff(s,t) strcmp((s),(t))
386 #define str_equal(s,t) (!strcmp((s),(t)))
391 extern int wait_pid(int *wstat, int pid);
392 extern int wait_nohang(int *wstat);
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)