2 b2cfc4e2 2003-09-30 devnull * Lib9 is miscellany from the Plan 9 C library that doesn't
3 b2cfc4e2 2003-09-30 devnull * fit into libutf or into libfmt, but is still missing from traditional
4 b2cfc4e2 2003-09-30 devnull * Unix C libraries.
6 b2cfc4e2 2003-09-30 devnull #ifndef _LIB9H_
7 b2cfc4e2 2003-09-30 devnull #define _LIB9H_ 1
9 b2cfc4e2 2003-09-30 devnull #if defined(__cplusplus)
10 b2cfc4e2 2003-09-30 devnull extern "C" {
14 b2cfc4e2 2003-09-30 devnull #include <unistd.h>
15 b2cfc4e2 2003-09-30 devnull #include <string.h>
16 b2cfc4e2 2003-09-30 devnull #include <stdlib.h>
17 b2cfc4e2 2003-09-30 devnull #include <stdarg.h>
18 b2cfc4e2 2003-09-30 devnull #include <fcntl.h>
19 b2cfc4e2 2003-09-30 devnull #include <assert.h>
20 b2cfc4e2 2003-09-30 devnull #include <setjmp.h>
22 b2cfc4e2 2003-09-30 devnull #ifndef _FMTH_
23 b2cfc4e2 2003-09-30 devnull # include <fmt.h>
26 b2cfc4e2 2003-09-30 devnull #define nil ((void*)0)
27 b2cfc4e2 2003-09-30 devnull #define nelem(x) (sizeof(x)/sizeof((x)[0]))
29 b2cfc4e2 2003-09-30 devnull #define _NEEDUCHAR 1
30 b2cfc4e2 2003-09-30 devnull #define _NEEDUSHORT 1
31 b2cfc4e2 2003-09-30 devnull #define _NEEDUINT 1
32 b2cfc4e2 2003-09-30 devnull #define _NEEDULONG 1
34 b2cfc4e2 2003-09-30 devnull #if defined(__linux__)
35 b2cfc4e2 2003-09-30 devnull # include <sys/types.h>
36 b2cfc4e2 2003-09-30 devnull # if defined(__USE_MISC)
37 b2cfc4e2 2003-09-30 devnull # undef _NEEDUSHORT
38 b2cfc4e2 2003-09-30 devnull # undef _NEEDUINT
39 b2cfc4e2 2003-09-30 devnull # undef _NEEDULONG
42 b2cfc4e2 2003-09-30 devnull #if defined(__FreeBSD__)
43 b2cfc4e2 2003-09-30 devnull # include <sys/types.h>
44 b2cfc4e2 2003-09-30 devnull # if !defined(_POSIX_SOURCE)
45 b2cfc4e2 2003-09-30 devnull # undef _NEEDUSHORT
46 b2cfc4e2 2003-09-30 devnull # undef _NEEDUINT
50 b2cfc4e2 2003-09-30 devnull typedef signed char schar;
51 b2cfc4e2 2003-09-30 devnull typedef unsigned int u32int;
52 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUCHAR
53 b2cfc4e2 2003-09-30 devnull typedef unsigned char uchar;
55 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUSHORT
56 b2cfc4e2 2003-09-30 devnull typedef unsigned short ushort;
58 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUINT
59 b2cfc4e2 2003-09-30 devnull typedef unsigned int uint;
61 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDULONG
62 b2cfc4e2 2003-09-30 devnull typedef unsigned long ulong;
64 b2cfc4e2 2003-09-30 devnull typedef unsigned long long uvlong;
65 b2cfc4e2 2003-09-30 devnull typedef long long vlong;
67 b2cfc4e2 2003-09-30 devnull /* rfork to create new process running fn(arg) */
69 b2cfc4e2 2003-09-30 devnull #if defined(__FreeBSD__)
70 b2cfc4e2 2003-09-30 devnull #undef RFFDG
71 b2cfc4e2 2003-09-30 devnull #undef RFNOTEG
72 b2cfc4e2 2003-09-30 devnull #undef RFPROC
73 b2cfc4e2 2003-09-30 devnull #undef RFMEM
74 b2cfc4e2 2003-09-30 devnull #undef RFNOWAIT
75 b2cfc4e2 2003-09-30 devnull #undef RFCFDG
80 b2cfc4e2 2003-09-30 devnull /* RFNAMEG = (1<<0), */
81 b2cfc4e2 2003-09-30 devnull /* RFENVG = (1<<1), */
82 b2cfc4e2 2003-09-30 devnull RFFDG = (1<<2),
83 b2cfc4e2 2003-09-30 devnull RFNOTEG = (1<<3),
84 b2cfc4e2 2003-09-30 devnull RFPROC = (1<<4),
85 b2cfc4e2 2003-09-30 devnull RFMEM = (1<<5),
86 b2cfc4e2 2003-09-30 devnull RFNOWAIT = (1<<6),
87 b2cfc4e2 2003-09-30 devnull /* RFCNAMEG = (1<<10), */
88 b2cfc4e2 2003-09-30 devnull /* RFCENVG = (1<<11), */
89 b2cfc4e2 2003-09-30 devnull RFCFDG = (1<<12),
90 b2cfc4e2 2003-09-30 devnull /* RFREND = (1<<13), */
91 b2cfc4e2 2003-09-30 devnull /* RFNOMNT = (1<<14) */
93 b2cfc4e2 2003-09-30 devnull extern int ffork(int, void(*)(void*), void*);
95 b2cfc4e2 2003-09-30 devnull /* wait for processes */
96 b2cfc4e2 2003-09-30 devnull #define wait _p9wait
97 b2cfc4e2 2003-09-30 devnull typedef struct Waitmsg Waitmsg;
98 b2cfc4e2 2003-09-30 devnull struct Waitmsg
100 b2cfc4e2 2003-09-30 devnull int pid; /* of loved one */
101 b2cfc4e2 2003-09-30 devnull ulong time[3]; /* of loved one & descendants */
102 b2cfc4e2 2003-09-30 devnull char *msg;
104 b2cfc4e2 2003-09-30 devnull extern int await(char*, int);
105 b2cfc4e2 2003-09-30 devnull extern Waitmsg* wait(void);
107 b2cfc4e2 2003-09-30 devnull /* synchronization */
108 b2cfc4e2 2003-09-30 devnull typedef struct Lock Lock;
109 b2cfc4e2 2003-09-30 devnull struct Lock
111 b2cfc4e2 2003-09-30 devnull int val;
114 b2cfc4e2 2003-09-30 devnull extern int _tas(void*);
115 b2cfc4e2 2003-09-30 devnull extern void lock(Lock*);
116 b2cfc4e2 2003-09-30 devnull extern void unlock(Lock*);
117 b2cfc4e2 2003-09-30 devnull extern int canlock(Lock*);
119 b2cfc4e2 2003-09-30 devnull typedef struct QLp QLp;
120 b2cfc4e2 2003-09-30 devnull struct QLp
122 b2cfc4e2 2003-09-30 devnull int inuse;
123 b2cfc4e2 2003-09-30 devnull QLp *next;
124 b2cfc4e2 2003-09-30 devnull int state;
127 b2cfc4e2 2003-09-30 devnull typedef struct QLock QLock;
128 b2cfc4e2 2003-09-30 devnull struct QLock
130 b2cfc4e2 2003-09-30 devnull Lock lock;
131 b2cfc4e2 2003-09-30 devnull int locked;
132 b2cfc4e2 2003-09-30 devnull QLp *head;
133 b2cfc4e2 2003-09-30 devnull QLp *tail;
136 b2cfc4e2 2003-09-30 devnull extern void qlock(QLock*);
137 b2cfc4e2 2003-09-30 devnull extern void qunlock(QLock*);
138 b2cfc4e2 2003-09-30 devnull extern int canqlock(QLock*);
139 b2cfc4e2 2003-09-30 devnull extern void _qlockinit(ulong (*)(ulong, ulong));
141 b2cfc4e2 2003-09-30 devnull typedef struct RWLock RWLock;
142 b2cfc4e2 2003-09-30 devnull struct RWLock
144 b2cfc4e2 2003-09-30 devnull Lock lock;
145 b2cfc4e2 2003-09-30 devnull int readers;
146 b2cfc4e2 2003-09-30 devnull int writer;
147 b2cfc4e2 2003-09-30 devnull QLp *head;
148 b2cfc4e2 2003-09-30 devnull QLp *tail;
151 b2cfc4e2 2003-09-30 devnull extern void rlock(RWLock*);
152 b2cfc4e2 2003-09-30 devnull extern void runlock(RWLock*);
153 b2cfc4e2 2003-09-30 devnull extern int canrlock(RWLock*);
154 b2cfc4e2 2003-09-30 devnull extern void wlock(RWLock*);
155 b2cfc4e2 2003-09-30 devnull extern void wunlock(RWLock*);
156 b2cfc4e2 2003-09-30 devnull extern int canwlock(RWLock*);
158 b2cfc4e2 2003-09-30 devnull typedef struct Rendez Rendez;
159 b2cfc4e2 2003-09-30 devnull struct Rendez
161 b2cfc4e2 2003-09-30 devnull QLock *l;
162 b2cfc4e2 2003-09-30 devnull QLp *head;
163 b2cfc4e2 2003-09-30 devnull QLp *tail;
166 b2cfc4e2 2003-09-30 devnull extern void rsleep(Rendez*);
167 b2cfc4e2 2003-09-30 devnull extern int rwakeup(Rendez*);
168 b2cfc4e2 2003-09-30 devnull extern int rwakeupall(Rendez*);
170 b2cfc4e2 2003-09-30 devnull extern ulong rendezvous(ulong, ulong);
172 b2cfc4e2 2003-09-30 devnull /* one of a kind */
173 b2cfc4e2 2003-09-30 devnull extern void sysfatal(char*, ...);
174 b2cfc4e2 2003-09-30 devnull extern int nrand(int);
175 b2cfc4e2 2003-09-30 devnull extern void setmalloctag(void*, ulong);
176 b2cfc4e2 2003-09-30 devnull extern void setrealloctag(void*, ulong);
177 b2cfc4e2 2003-09-30 devnull extern void *mallocz(ulong, int);
178 b2cfc4e2 2003-09-30 devnull extern long readn(int, void*, long);
179 b2cfc4e2 2003-09-30 devnull extern void exits(char*);
180 b2cfc4e2 2003-09-30 devnull extern void _exits(char*);
181 b2cfc4e2 2003-09-30 devnull extern ulong getcallerpc(void*);
183 b2cfc4e2 2003-09-30 devnull /* string routines */
184 b2cfc4e2 2003-09-30 devnull extern char* strecpy(char*, char*, char*);
185 b2cfc4e2 2003-09-30 devnull extern int tokenize(char*, char**, int);
186 b2cfc4e2 2003-09-30 devnull extern int cistrncmp(char*, char*, int);
187 b2cfc4e2 2003-09-30 devnull extern int cistrcmp(char*, char*);
188 b2cfc4e2 2003-09-30 devnull extern char* cistrstr(char*, char*);
189 b2cfc4e2 2003-09-30 devnull extern int getfields(char*, char**, int, int, char*);
190 b2cfc4e2 2003-09-30 devnull extern int gettokens(char *, char **, int, char *);
192 b2cfc4e2 2003-09-30 devnull /* formatting helpers */
193 b2cfc4e2 2003-09-30 devnull extern int dec64(uchar*, int, char*, int);
194 b2cfc4e2 2003-09-30 devnull extern int enc64(char*, int, uchar*, int);
195 b2cfc4e2 2003-09-30 devnull extern int dec32(uchar*, int, char*, int);
196 b2cfc4e2 2003-09-30 devnull extern int enc32(char*, int, uchar*, int);
197 b2cfc4e2 2003-09-30 devnull extern int dec16(uchar*, int, char*, int);
198 b2cfc4e2 2003-09-30 devnull extern int enc16(char*, int, uchar*, int);
199 b2cfc4e2 2003-09-30 devnull extern int encodefmt(Fmt*);
201 b2cfc4e2 2003-09-30 devnull /* error string */
204 b2cfc4e2 2003-09-30 devnull ERRMAX = 128
206 b2cfc4e2 2003-09-30 devnull extern void rerrstr(char*, uint);
207 b2cfc4e2 2003-09-30 devnull extern void werrstr(char*, ...);
208 b2cfc4e2 2003-09-30 devnull extern int errstr(char*, uint);
210 b2cfc4e2 2003-09-30 devnull /* compiler directives on plan 9 */
211 b2cfc4e2 2003-09-30 devnull #define USED(x) if(x){}else{}
212 b2cfc4e2 2003-09-30 devnull #define SET(x) ((x)=0)
214 b2cfc4e2 2003-09-30 devnull /* command line */
215 b2cfc4e2 2003-09-30 devnull extern char *argv0;
216 b2cfc4e2 2003-09-30 devnull #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\
217 b2cfc4e2 2003-09-30 devnull argv[0] && argv[0][0]=='-' && argv[0][1];\
218 b2cfc4e2 2003-09-30 devnull argc--, argv++) {\
219 b2cfc4e2 2003-09-30 devnull char *_args, *_argt;\
220 b2cfc4e2 2003-09-30 devnull Rune _argc;\
221 b2cfc4e2 2003-09-30 devnull _args = &argv[0][1];\
222 b2cfc4e2 2003-09-30 devnull if(_args[0]=='-' && _args[1]==0){\
223 b2cfc4e2 2003-09-30 devnull argc--; argv++; break;\
225 b2cfc4e2 2003-09-30 devnull _argc = 0;\
226 b2cfc4e2 2003-09-30 devnull while(*_args && (_args += chartorune(&_argc, _args)))\
227 b2cfc4e2 2003-09-30 devnull switch(_argc)
228 b2cfc4e2 2003-09-30 devnull #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
229 b2cfc4e2 2003-09-30 devnull #define ARGF() (_argt=_args, _args="",\
230 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
231 b2cfc4e2 2003-09-30 devnull #define EARGF(x) (_argt=_args, _args="",\
232 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
234 b2cfc4e2 2003-09-30 devnull #define ARGC() _argc
236 b2cfc4e2 2003-09-30 devnull #define OREAD O_RDONLY
237 b2cfc4e2 2003-09-30 devnull #define OWRITE O_WRONLY
238 b2cfc4e2 2003-09-30 devnull #define AEXIST 0
239 b2cfc4e2 2003-09-30 devnull #define AREAD 4
240 b2cfc4e2 2003-09-30 devnull #define AWRITE 2
241 b2cfc4e2 2003-09-30 devnull #define AEXEC 1
242 b2cfc4e2 2003-09-30 devnull #define ORCLOSE 8
243 b2cfc4e2 2003-09-30 devnull #define OCEXEC 16
245 b2cfc4e2 2003-09-30 devnull #define dup dup2
246 b2cfc4e2 2003-09-30 devnull #define exec execv
247 b2cfc4e2 2003-09-30 devnull #define seek lseek
248 b2cfc4e2 2003-09-30 devnull #define getwd getcwd
250 b2cfc4e2 2003-09-30 devnull #if defined(__cplusplus)
254 b2cfc4e2 2003-09-30 devnull #endif /* _LIB9H_ */