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 ***/
191 extern void fmt_ptime30nul(char *, struct taia *);
192 /* NOT terminated! */
193 extern unsigned fmt_taia25(char *, struct taia *);
197 /*** gen_alloc.h ***/
199 #define GEN_ALLOC_typedef(ta,type,field,len,a) \
200 typedef struct ta { type *field; unsigned len; unsigned a; } ta;
203 /*** gen_allocdefs.h ***/
205 #define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
206 int ta_ready(ta *x,unsigned n) \
211 x->a = base + n + (n >> 3); \
212 x->field = realloc(x->field,x->a * sizeof(type)); \
213 if (x->field) return 1; \
214 x->a = i; return 0; } \
217 return !!(x->field = malloc((x->a = n) * sizeof(type))); }
219 #define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
220 int ta_rplus(ta *x,unsigned n) \
223 i = x->a; n += x->len; \
225 x->a = base + n + (n >> 3); \
226 x->field = realloc(x->field,x->a * sizeof(type)); \
227 if (x->field) return 1; \
228 x->a = i; return 0; } \
231 return !!(x->field = malloc((x->a = n) * sizeof(type))); }
233 #define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
234 int ta_append(ta *x,const type *i) \
235 { if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
239 GEN_ALLOC_typedef(stralloc,char,s,len,a)
241 extern int stralloc_ready(stralloc *,unsigned);
242 extern int stralloc_readyplus(stralloc *,unsigned);
243 extern int stralloc_copy(stralloc *,const stralloc *);
244 extern int stralloc_cat(stralloc *,const stralloc *);
245 extern int stralloc_copys(stralloc *,const char *);
246 extern int stralloc_cats(stralloc *,const char *);
247 extern int stralloc_copyb(stralloc *,const char *,unsigned);
248 extern int stralloc_catb(stralloc *,const char *,unsigned);
249 extern int stralloc_append(stralloc *,const char *); /* beware: this takes a pointer to 1 char */
250 extern int stralloc_starts(stralloc *,const char *);
252 #define stralloc_0(sa) stralloc_append(sa,"")
254 extern int stralloc_catulong0(stralloc *,unsigned long,unsigned);
255 extern int stralloc_catlong0(stralloc *,long,unsigned);
257 #define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
258 #define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
259 #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
260 #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
265 typedef struct pollfd iopause_fd;
266 #define IOPAUSE_READ POLLIN
267 #define IOPAUSE_WRITE POLLOUT
269 extern void iopause(iopause_fd *,unsigned,struct taia *,struct taia *);
274 extern int lock_ex(int);
275 extern int lock_un(int);
276 extern int lock_exnb(int);
281 extern int open_read(const char *);
282 extern int open_excl(const char *);
283 extern int open_append(const char *);
284 extern int open_trunc(const char *);
285 extern int open_write(const char *);
288 /*** openreadclose.h ***/
290 extern int openreadclose(const char *,stralloc *,unsigned);
295 extern void pathexec_run(const char *,char *const *,char *const *);
296 extern int pathexec_env(const char *,const char *);
297 extern void pathexec(char **);
302 extern unsigned pmatch(const char *, const char *, unsigned);
307 extern int prot_gid(int);
308 extern int prot_uid(int);
311 /*** readclose.h ***/
313 extern int readclose_append(int,stralloc *,unsigned);
314 extern int readclose(int,stralloc *,unsigned);
320 extern unsigned scan_uint(const char *,unsigned *);
321 extern unsigned scan_xint(const char *,unsigned *);
322 extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *);
323 extern unsigned scan_ushort(const char *,unsigned short *);
324 extern unsigned scan_xshort(const char *,unsigned short *);
325 extern unsigned scan_nbbshort(const char *,unsigned,unsigned,unsigned,unsigned short *);
326 extern unsigned scan_ulong(const char *,unsigned long *);
327 extern unsigned scan_xlong(const char *,unsigned long *);
328 extern unsigned scan_nbblong(const char *,unsigned,unsigned,unsigned,unsigned long *);
330 extern unsigned scan_plusminus(const char *,int *);
331 extern unsigned scan_0x(const char *,unsigned *);
333 extern unsigned scan_whitenskip(const char *,unsigned);
334 extern unsigned scan_nonwhitenskip(const char *,unsigned);
335 extern unsigned scan_charsetnskip(const char *,const char *,unsigned);
336 extern unsigned scan_noncharsetnskip(const char *,const char *,unsigned);
338 extern unsigned scan_strncmp(const char *,const char *,unsigned);
339 extern unsigned scan_memcmp(const char *,const char *,unsigned);
341 extern unsigned scan_long(const char *,long *);
342 extern unsigned scan_8long(const char *,unsigned long *);
348 typedef unsigned long seek_pos;
350 extern seek_pos seek_cur(int);
352 //extern int seek_set(int,seek_pos);
353 extern int seek_end(int);
355 extern int seek_trunc(int,seek_pos);
357 //#define seek_begin(fd) (seek_set((fd),(seek_pos) 0))
362 //extern int sig_alarm;
363 //extern int sig_child;
364 //extern int sig_cont;
365 //extern int sig_hangup;
366 //extern int sig_int;
367 //extern int sig_pipe;
368 //extern int sig_term;
370 extern void sig_catch(int,void (*)(int));
371 #define sig_ignore(s) (sig_catch((s),SIG_IGN))
372 #define sig_uncatch(s) (sig_catch((s),SIG_DFL))
374 extern void sig_block(int);
375 extern void sig_unblock(int);
376 extern void sig_blocknone(void);
377 extern void sig_pause(void);
379 extern void sig_dfl(int);
384 extern unsigned str_chr(const char *,int); /* never returns NULL */
386 #define str_diff(s,t) strcmp((s),(t))
387 #define str_equal(s,t) (!strcmp((s),(t)))
392 extern int wait_pid(int *wstat, int pid);
393 extern int wait_nohang(int *wstat);
395 #define wait_crashed(w) ((w) & 127)
396 #define wait_exitcode(w) ((w) >> 8)
397 #define wait_stopsig(w) ((w) >> 8)
398 #define wait_stopped(w) (((w) & 127) == 127)