Blame


1 b2cfc4e2 2003-09-30 devnull /*
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.
5 b2cfc4e2 2003-09-30 devnull */
6 b2cfc4e2 2003-09-30 devnull #ifndef _LIB9H_
7 b2cfc4e2 2003-09-30 devnull #define _LIB9H_ 1
8 b2cfc4e2 2003-09-30 devnull
9 b2cfc4e2 2003-09-30 devnull #if defined(__cplusplus)
10 b2cfc4e2 2003-09-30 devnull extern "C" {
11 b2cfc4e2 2003-09-30 devnull #endif
12 b2cfc4e2 2003-09-30 devnull
13 b2cfc4e2 2003-09-30 devnull
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>
21 b2cfc4e2 2003-09-30 devnull
22 b2cfc4e2 2003-09-30 devnull #ifndef _FMTH_
23 b2cfc4e2 2003-09-30 devnull # include <fmt.h>
24 b2cfc4e2 2003-09-30 devnull #endif
25 b2cfc4e2 2003-09-30 devnull
26 b2cfc4e2 2003-09-30 devnull #define nil ((void*)0)
27 b2cfc4e2 2003-09-30 devnull #define nelem(x) (sizeof(x)/sizeof((x)[0]))
28 b2cfc4e2 2003-09-30 devnull
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
33 b2cfc4e2 2003-09-30 devnull
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
40 b2cfc4e2 2003-09-30 devnull # endif
41 b2cfc4e2 2003-09-30 devnull #endif
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
47 b2cfc4e2 2003-09-30 devnull # endif
48 a995e477 2003-10-01 devnull #endif
49 a995e477 2003-10-01 devnull #if defined(__APPLE__)
50 a995e477 2003-10-01 devnull # include <sys/types.h>
51 a995e477 2003-10-01 devnull # undef _NEEDUSHORT
52 a995e477 2003-10-01 devnull # undef _NEEDUINT
53 b2cfc4e2 2003-09-30 devnull #endif
54 b2cfc4e2 2003-09-30 devnull
55 b2cfc4e2 2003-09-30 devnull typedef signed char schar;
56 b2cfc4e2 2003-09-30 devnull typedef unsigned int u32int;
57 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUCHAR
58 b2cfc4e2 2003-09-30 devnull typedef unsigned char uchar;
59 b2cfc4e2 2003-09-30 devnull #endif
60 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUSHORT
61 b2cfc4e2 2003-09-30 devnull typedef unsigned short ushort;
62 b2cfc4e2 2003-09-30 devnull #endif
63 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUINT
64 b2cfc4e2 2003-09-30 devnull typedef unsigned int uint;
65 b2cfc4e2 2003-09-30 devnull #endif
66 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDULONG
67 b2cfc4e2 2003-09-30 devnull typedef unsigned long ulong;
68 b2cfc4e2 2003-09-30 devnull #endif
69 b2cfc4e2 2003-09-30 devnull typedef unsigned long long uvlong;
70 b2cfc4e2 2003-09-30 devnull typedef long long vlong;
71 b2cfc4e2 2003-09-30 devnull
72 b3161d97 2003-10-14 devnull #define NAMELEN 28
73 b3161d97 2003-10-14 devnull #define CHDIR 0x80000000 /* mode bit for directories */
74 b3161d97 2003-10-14 devnull #define CHAPPEND 0x40000000 /* mode bit for append only files */
75 b3161d97 2003-10-14 devnull #define CHEXCL 0x20000000 /* mode bit for exclusive use files */
76 b3161d97 2003-10-14 devnull #define CHMOUNT 0x10000000 /* mode bit for mounted channel */
77 b3161d97 2003-10-14 devnull #define CHREAD 0x4 /* mode bit for read permission */
78 b3161d97 2003-10-14 devnull #define CHWRITE 0x2 /* mode bit for write permission */
79 b3161d97 2003-10-14 devnull #define CHEXEC 0x1 /* mode bit for execute permission */
80 b3161d97 2003-10-14 devnull
81 b2cfc4e2 2003-09-30 devnull /* rfork to create new process running fn(arg) */
82 b2cfc4e2 2003-09-30 devnull
83 b2cfc4e2 2003-09-30 devnull #if defined(__FreeBSD__)
84 b2cfc4e2 2003-09-30 devnull #undef RFFDG
85 b2cfc4e2 2003-09-30 devnull #undef RFNOTEG
86 b2cfc4e2 2003-09-30 devnull #undef RFPROC
87 b2cfc4e2 2003-09-30 devnull #undef RFMEM
88 b2cfc4e2 2003-09-30 devnull #undef RFNOWAIT
89 b2cfc4e2 2003-09-30 devnull #undef RFCFDG
90 b2cfc4e2 2003-09-30 devnull #endif
91 b2cfc4e2 2003-09-30 devnull
92 b2cfc4e2 2003-09-30 devnull enum
93 b2cfc4e2 2003-09-30 devnull {
94 b2cfc4e2 2003-09-30 devnull /* RFNAMEG = (1<<0), */
95 b2cfc4e2 2003-09-30 devnull /* RFENVG = (1<<1), */
96 b2cfc4e2 2003-09-30 devnull RFFDG = (1<<2),
97 b2cfc4e2 2003-09-30 devnull RFNOTEG = (1<<3),
98 b2cfc4e2 2003-09-30 devnull RFPROC = (1<<4),
99 b2cfc4e2 2003-09-30 devnull RFMEM = (1<<5),
100 b2cfc4e2 2003-09-30 devnull RFNOWAIT = (1<<6),
101 b2cfc4e2 2003-09-30 devnull /* RFCNAMEG = (1<<10), */
102 b2cfc4e2 2003-09-30 devnull /* RFCENVG = (1<<11), */
103 b2cfc4e2 2003-09-30 devnull RFCFDG = (1<<12),
104 b2cfc4e2 2003-09-30 devnull /* RFREND = (1<<13), */
105 b2cfc4e2 2003-09-30 devnull /* RFNOMNT = (1<<14) */
106 b2cfc4e2 2003-09-30 devnull };
107 b2cfc4e2 2003-09-30 devnull extern int ffork(int, void(*)(void*), void*);
108 b2cfc4e2 2003-09-30 devnull
109 b2cfc4e2 2003-09-30 devnull /* wait for processes */
110 b2cfc4e2 2003-09-30 devnull #define wait _p9wait
111 b2cfc4e2 2003-09-30 devnull typedef struct Waitmsg Waitmsg;
112 b2cfc4e2 2003-09-30 devnull struct Waitmsg
113 b2cfc4e2 2003-09-30 devnull {
114 b2cfc4e2 2003-09-30 devnull int pid; /* of loved one */
115 b2cfc4e2 2003-09-30 devnull ulong time[3]; /* of loved one & descendants */
116 b2cfc4e2 2003-09-30 devnull char *msg;
117 b2cfc4e2 2003-09-30 devnull };
118 b2cfc4e2 2003-09-30 devnull extern int await(char*, int);
119 b2cfc4e2 2003-09-30 devnull extern Waitmsg* wait(void);
120 b2cfc4e2 2003-09-30 devnull
121 b2cfc4e2 2003-09-30 devnull /* synchronization */
122 b2cfc4e2 2003-09-30 devnull typedef struct Lock Lock;
123 b2cfc4e2 2003-09-30 devnull struct Lock
124 b2cfc4e2 2003-09-30 devnull {
125 b2cfc4e2 2003-09-30 devnull int val;
126 b2cfc4e2 2003-09-30 devnull };
127 b2cfc4e2 2003-09-30 devnull
128 b2cfc4e2 2003-09-30 devnull extern int _tas(void*);
129 b2cfc4e2 2003-09-30 devnull extern void lock(Lock*);
130 b2cfc4e2 2003-09-30 devnull extern void unlock(Lock*);
131 b2cfc4e2 2003-09-30 devnull extern int canlock(Lock*);
132 b2cfc4e2 2003-09-30 devnull
133 b2cfc4e2 2003-09-30 devnull typedef struct QLp QLp;
134 b2cfc4e2 2003-09-30 devnull struct QLp
135 b2cfc4e2 2003-09-30 devnull {
136 b2cfc4e2 2003-09-30 devnull int inuse;
137 b2cfc4e2 2003-09-30 devnull QLp *next;
138 b2cfc4e2 2003-09-30 devnull int state;
139 b2cfc4e2 2003-09-30 devnull };
140 b2cfc4e2 2003-09-30 devnull
141 b2cfc4e2 2003-09-30 devnull typedef struct QLock QLock;
142 b2cfc4e2 2003-09-30 devnull struct QLock
143 b2cfc4e2 2003-09-30 devnull {
144 b2cfc4e2 2003-09-30 devnull Lock lock;
145 b2cfc4e2 2003-09-30 devnull int locked;
146 b2cfc4e2 2003-09-30 devnull QLp *head;
147 b2cfc4e2 2003-09-30 devnull QLp *tail;
148 b2cfc4e2 2003-09-30 devnull };
149 b2cfc4e2 2003-09-30 devnull
150 b2cfc4e2 2003-09-30 devnull extern void qlock(QLock*);
151 b2cfc4e2 2003-09-30 devnull extern void qunlock(QLock*);
152 b2cfc4e2 2003-09-30 devnull extern int canqlock(QLock*);
153 b2cfc4e2 2003-09-30 devnull extern void _qlockinit(ulong (*)(ulong, ulong));
154 b2cfc4e2 2003-09-30 devnull
155 b2cfc4e2 2003-09-30 devnull typedef struct RWLock RWLock;
156 b2cfc4e2 2003-09-30 devnull struct RWLock
157 b2cfc4e2 2003-09-30 devnull {
158 b2cfc4e2 2003-09-30 devnull Lock lock;
159 b2cfc4e2 2003-09-30 devnull int readers;
160 b2cfc4e2 2003-09-30 devnull int writer;
161 b2cfc4e2 2003-09-30 devnull QLp *head;
162 b2cfc4e2 2003-09-30 devnull QLp *tail;
163 b2cfc4e2 2003-09-30 devnull };
164 b2cfc4e2 2003-09-30 devnull
165 b2cfc4e2 2003-09-30 devnull extern void rlock(RWLock*);
166 b2cfc4e2 2003-09-30 devnull extern void runlock(RWLock*);
167 b2cfc4e2 2003-09-30 devnull extern int canrlock(RWLock*);
168 b2cfc4e2 2003-09-30 devnull extern void wlock(RWLock*);
169 b2cfc4e2 2003-09-30 devnull extern void wunlock(RWLock*);
170 b2cfc4e2 2003-09-30 devnull extern int canwlock(RWLock*);
171 b2cfc4e2 2003-09-30 devnull
172 b2cfc4e2 2003-09-30 devnull typedef struct Rendez Rendez;
173 b2cfc4e2 2003-09-30 devnull struct Rendez
174 b2cfc4e2 2003-09-30 devnull {
175 b2cfc4e2 2003-09-30 devnull QLock *l;
176 b2cfc4e2 2003-09-30 devnull QLp *head;
177 b2cfc4e2 2003-09-30 devnull QLp *tail;
178 b2cfc4e2 2003-09-30 devnull };
179 b2cfc4e2 2003-09-30 devnull
180 b2cfc4e2 2003-09-30 devnull extern void rsleep(Rendez*);
181 b2cfc4e2 2003-09-30 devnull extern int rwakeup(Rendez*);
182 b2cfc4e2 2003-09-30 devnull extern int rwakeupall(Rendez*);
183 b2cfc4e2 2003-09-30 devnull
184 b2cfc4e2 2003-09-30 devnull extern ulong rendezvous(ulong, ulong);
185 b3161d97 2003-10-14 devnull
186 b3161d97 2003-10-14 devnull typedef struct Qid Qid;
187 b3161d97 2003-10-14 devnull typedef struct Dir Dir;
188 b3161d97 2003-10-14 devnull
189 b3161d97 2003-10-14 devnull struct Qid
190 b3161d97 2003-10-14 devnull {
191 b3161d97 2003-10-14 devnull ulong path;
192 b3161d97 2003-10-14 devnull ulong vers;
193 b3161d97 2003-10-14 devnull };
194 b3161d97 2003-10-14 devnull
195 b3161d97 2003-10-14 devnull struct Dir
196 b3161d97 2003-10-14 devnull {
197 b3161d97 2003-10-14 devnull char name[NAMELEN];
198 b3161d97 2003-10-14 devnull char uid[NAMELEN];
199 b3161d97 2003-10-14 devnull char gid[NAMELEN];
200 b3161d97 2003-10-14 devnull Qid qid;
201 b3161d97 2003-10-14 devnull ulong mode;
202 b3161d97 2003-10-14 devnull long atime;
203 b3161d97 2003-10-14 devnull long mtime;
204 b3161d97 2003-10-14 devnull vlong length;
205 b3161d97 2003-10-14 devnull ushort type;
206 b3161d97 2003-10-14 devnull ushort dev;
207 b3161d97 2003-10-14 devnull };
208 b2cfc4e2 2003-09-30 devnull
209 b3161d97 2003-10-14 devnull extern int dirstat(char*, Dir*);
210 b3161d97 2003-10-14 devnull extern int dirfstat(int, Dir*);
211 b3161d97 2003-10-14 devnull extern int dirwstat(char*, Dir*);
212 b3161d97 2003-10-14 devnull extern int dirfwstat(int, Dir*);
213 b3161d97 2003-10-14 devnull
214 b3161d97 2003-10-14 devnull
215 b2cfc4e2 2003-09-30 devnull /* one of a kind */
216 b2cfc4e2 2003-09-30 devnull extern void sysfatal(char*, ...);
217 20093746 2003-10-11 devnull extern int nrand(int);
218 20093746 2003-10-11 devnull extern long lrand(void);
219 b2cfc4e2 2003-09-30 devnull extern void setmalloctag(void*, ulong);
220 b2cfc4e2 2003-09-30 devnull extern void setrealloctag(void*, ulong);
221 b2cfc4e2 2003-09-30 devnull extern void *mallocz(ulong, int);
222 b2cfc4e2 2003-09-30 devnull extern long readn(int, void*, long);
223 b2cfc4e2 2003-09-30 devnull extern void exits(char*);
224 b2cfc4e2 2003-09-30 devnull extern void _exits(char*);
225 b2cfc4e2 2003-09-30 devnull extern ulong getcallerpc(void*);
226 b3161d97 2003-10-14 devnull extern char* cleanname(char*);
227 b2cfc4e2 2003-09-30 devnull
228 b2cfc4e2 2003-09-30 devnull /* string routines */
229 b2cfc4e2 2003-09-30 devnull extern char* strecpy(char*, char*, char*);
230 b2cfc4e2 2003-09-30 devnull extern int tokenize(char*, char**, int);
231 b2cfc4e2 2003-09-30 devnull extern int cistrncmp(char*, char*, int);
232 b2cfc4e2 2003-09-30 devnull extern int cistrcmp(char*, char*);
233 b2cfc4e2 2003-09-30 devnull extern char* cistrstr(char*, char*);
234 b2cfc4e2 2003-09-30 devnull extern int getfields(char*, char**, int, int, char*);
235 b2cfc4e2 2003-09-30 devnull extern int gettokens(char *, char **, int, char *);
236 b2cfc4e2 2003-09-30 devnull
237 b2cfc4e2 2003-09-30 devnull /* formatting helpers */
238 b2cfc4e2 2003-09-30 devnull extern int dec64(uchar*, int, char*, int);
239 b2cfc4e2 2003-09-30 devnull extern int enc64(char*, int, uchar*, int);
240 b2cfc4e2 2003-09-30 devnull extern int dec32(uchar*, int, char*, int);
241 b2cfc4e2 2003-09-30 devnull extern int enc32(char*, int, uchar*, int);
242 b2cfc4e2 2003-09-30 devnull extern int dec16(uchar*, int, char*, int);
243 b2cfc4e2 2003-09-30 devnull extern int enc16(char*, int, uchar*, int);
244 b2cfc4e2 2003-09-30 devnull extern int encodefmt(Fmt*);
245 b2cfc4e2 2003-09-30 devnull
246 b2cfc4e2 2003-09-30 devnull /* error string */
247 b2cfc4e2 2003-09-30 devnull enum
248 b2cfc4e2 2003-09-30 devnull {
249 b2cfc4e2 2003-09-30 devnull ERRMAX = 128
250 b2cfc4e2 2003-09-30 devnull };
251 b2cfc4e2 2003-09-30 devnull extern void rerrstr(char*, uint);
252 b2cfc4e2 2003-09-30 devnull extern void werrstr(char*, ...);
253 b2cfc4e2 2003-09-30 devnull extern int errstr(char*, uint);
254 b2cfc4e2 2003-09-30 devnull
255 b2cfc4e2 2003-09-30 devnull /* compiler directives on plan 9 */
256 b2cfc4e2 2003-09-30 devnull #define USED(x) if(x){}else{}
257 b2cfc4e2 2003-09-30 devnull #define SET(x) ((x)=0)
258 b2cfc4e2 2003-09-30 devnull
259 b2cfc4e2 2003-09-30 devnull /* command line */
260 b2cfc4e2 2003-09-30 devnull extern char *argv0;
261 a995e477 2003-10-01 devnull extern void __fixargv0(void);
262 a995e477 2003-10-01 devnull #define ARGBEGIN for((argv0||(argv0=(__fixargv0(),*argv))),argv++,argc--;\
263 b2cfc4e2 2003-09-30 devnull argv[0] && argv[0][0]=='-' && argv[0][1];\
264 b2cfc4e2 2003-09-30 devnull argc--, argv++) {\
265 b2cfc4e2 2003-09-30 devnull char *_args, *_argt;\
266 b2cfc4e2 2003-09-30 devnull Rune _argc;\
267 b2cfc4e2 2003-09-30 devnull _args = &argv[0][1];\
268 b2cfc4e2 2003-09-30 devnull if(_args[0]=='-' && _args[1]==0){\
269 b2cfc4e2 2003-09-30 devnull argc--; argv++; break;\
270 b2cfc4e2 2003-09-30 devnull }\
271 b2cfc4e2 2003-09-30 devnull _argc = 0;\
272 b2cfc4e2 2003-09-30 devnull while(*_args && (_args += chartorune(&_argc, _args)))\
273 b2cfc4e2 2003-09-30 devnull switch(_argc)
274 b2cfc4e2 2003-09-30 devnull #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
275 b2cfc4e2 2003-09-30 devnull #define ARGF() (_argt=_args, _args="",\
276 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
277 b2cfc4e2 2003-09-30 devnull #define EARGF(x) (_argt=_args, _args="",\
278 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
279 b2cfc4e2 2003-09-30 devnull
280 b2cfc4e2 2003-09-30 devnull #define ARGC() _argc
281 b2cfc4e2 2003-09-30 devnull
282 b2cfc4e2 2003-09-30 devnull #define OREAD O_RDONLY
283 b2cfc4e2 2003-09-30 devnull #define OWRITE O_WRONLY
284 b3161d97 2003-10-14 devnull #define ORDWR O_RDWR
285 b2cfc4e2 2003-09-30 devnull #define AEXIST 0
286 b2cfc4e2 2003-09-30 devnull #define AREAD 4
287 b2cfc4e2 2003-09-30 devnull #define AWRITE 2
288 b2cfc4e2 2003-09-30 devnull #define AEXEC 1
289 b2cfc4e2 2003-09-30 devnull #define ORCLOSE 8
290 b2cfc4e2 2003-09-30 devnull #define OCEXEC 16
291 b2cfc4e2 2003-09-30 devnull
292 b2cfc4e2 2003-09-30 devnull #define dup dup2
293 b2cfc4e2 2003-09-30 devnull #define exec execv
294 b2cfc4e2 2003-09-30 devnull #define seek lseek
295 b2cfc4e2 2003-09-30 devnull #define getwd getcwd
296 b2cfc4e2 2003-09-30 devnull
297 b2cfc4e2 2003-09-30 devnull #if defined(__cplusplus)
298 b2cfc4e2 2003-09-30 devnull }
299 b2cfc4e2 2003-09-30 devnull #endif
300 b2cfc4e2 2003-09-30 devnull
301 b2cfc4e2 2003-09-30 devnull #endif /* _LIB9H_ */