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 b2cfc4e2 2003-09-30 devnull /* rfork to create new process running fn(arg) */
73 b2cfc4e2 2003-09-30 devnull
74 b2cfc4e2 2003-09-30 devnull #if defined(__FreeBSD__)
75 b2cfc4e2 2003-09-30 devnull #undef RFFDG
76 b2cfc4e2 2003-09-30 devnull #undef RFNOTEG
77 b2cfc4e2 2003-09-30 devnull #undef RFPROC
78 b2cfc4e2 2003-09-30 devnull #undef RFMEM
79 b2cfc4e2 2003-09-30 devnull #undef RFNOWAIT
80 b2cfc4e2 2003-09-30 devnull #undef RFCFDG
81 b2cfc4e2 2003-09-30 devnull #endif
82 b2cfc4e2 2003-09-30 devnull
83 b2cfc4e2 2003-09-30 devnull enum
84 b2cfc4e2 2003-09-30 devnull {
85 b2cfc4e2 2003-09-30 devnull /* RFNAMEG = (1<<0), */
86 b2cfc4e2 2003-09-30 devnull /* RFENVG = (1<<1), */
87 b2cfc4e2 2003-09-30 devnull RFFDG = (1<<2),
88 b2cfc4e2 2003-09-30 devnull RFNOTEG = (1<<3),
89 b2cfc4e2 2003-09-30 devnull RFPROC = (1<<4),
90 b2cfc4e2 2003-09-30 devnull RFMEM = (1<<5),
91 b2cfc4e2 2003-09-30 devnull RFNOWAIT = (1<<6),
92 b2cfc4e2 2003-09-30 devnull /* RFCNAMEG = (1<<10), */
93 b2cfc4e2 2003-09-30 devnull /* RFCENVG = (1<<11), */
94 b2cfc4e2 2003-09-30 devnull RFCFDG = (1<<12),
95 b2cfc4e2 2003-09-30 devnull /* RFREND = (1<<13), */
96 b2cfc4e2 2003-09-30 devnull /* RFNOMNT = (1<<14) */
97 b2cfc4e2 2003-09-30 devnull };
98 b2cfc4e2 2003-09-30 devnull extern int ffork(int, void(*)(void*), void*);
99 b2cfc4e2 2003-09-30 devnull
100 b2cfc4e2 2003-09-30 devnull /* wait for processes */
101 b2cfc4e2 2003-09-30 devnull #define wait _p9wait
102 b2cfc4e2 2003-09-30 devnull typedef struct Waitmsg Waitmsg;
103 b2cfc4e2 2003-09-30 devnull struct Waitmsg
104 b2cfc4e2 2003-09-30 devnull {
105 b2cfc4e2 2003-09-30 devnull int pid; /* of loved one */
106 b2cfc4e2 2003-09-30 devnull ulong time[3]; /* of loved one & descendants */
107 b2cfc4e2 2003-09-30 devnull char *msg;
108 b2cfc4e2 2003-09-30 devnull };
109 b2cfc4e2 2003-09-30 devnull extern int await(char*, int);
110 b2cfc4e2 2003-09-30 devnull extern Waitmsg* wait(void);
111 b2cfc4e2 2003-09-30 devnull
112 b2cfc4e2 2003-09-30 devnull /* synchronization */
113 b2cfc4e2 2003-09-30 devnull typedef struct Lock Lock;
114 b2cfc4e2 2003-09-30 devnull struct Lock
115 b2cfc4e2 2003-09-30 devnull {
116 b2cfc4e2 2003-09-30 devnull int val;
117 b2cfc4e2 2003-09-30 devnull };
118 b2cfc4e2 2003-09-30 devnull
119 b2cfc4e2 2003-09-30 devnull extern int _tas(void*);
120 b2cfc4e2 2003-09-30 devnull extern void lock(Lock*);
121 b2cfc4e2 2003-09-30 devnull extern void unlock(Lock*);
122 b2cfc4e2 2003-09-30 devnull extern int canlock(Lock*);
123 b2cfc4e2 2003-09-30 devnull
124 b2cfc4e2 2003-09-30 devnull typedef struct QLp QLp;
125 b2cfc4e2 2003-09-30 devnull struct QLp
126 b2cfc4e2 2003-09-30 devnull {
127 b2cfc4e2 2003-09-30 devnull int inuse;
128 b2cfc4e2 2003-09-30 devnull QLp *next;
129 b2cfc4e2 2003-09-30 devnull int state;
130 b2cfc4e2 2003-09-30 devnull };
131 b2cfc4e2 2003-09-30 devnull
132 b2cfc4e2 2003-09-30 devnull typedef struct QLock QLock;
133 b2cfc4e2 2003-09-30 devnull struct QLock
134 b2cfc4e2 2003-09-30 devnull {
135 b2cfc4e2 2003-09-30 devnull Lock lock;
136 b2cfc4e2 2003-09-30 devnull int locked;
137 b2cfc4e2 2003-09-30 devnull QLp *head;
138 b2cfc4e2 2003-09-30 devnull QLp *tail;
139 b2cfc4e2 2003-09-30 devnull };
140 b2cfc4e2 2003-09-30 devnull
141 b2cfc4e2 2003-09-30 devnull extern void qlock(QLock*);
142 b2cfc4e2 2003-09-30 devnull extern void qunlock(QLock*);
143 b2cfc4e2 2003-09-30 devnull extern int canqlock(QLock*);
144 b2cfc4e2 2003-09-30 devnull extern void _qlockinit(ulong (*)(ulong, ulong));
145 b2cfc4e2 2003-09-30 devnull
146 b2cfc4e2 2003-09-30 devnull typedef struct RWLock RWLock;
147 b2cfc4e2 2003-09-30 devnull struct RWLock
148 b2cfc4e2 2003-09-30 devnull {
149 b2cfc4e2 2003-09-30 devnull Lock lock;
150 b2cfc4e2 2003-09-30 devnull int readers;
151 b2cfc4e2 2003-09-30 devnull int writer;
152 b2cfc4e2 2003-09-30 devnull QLp *head;
153 b2cfc4e2 2003-09-30 devnull QLp *tail;
154 b2cfc4e2 2003-09-30 devnull };
155 b2cfc4e2 2003-09-30 devnull
156 b2cfc4e2 2003-09-30 devnull extern void rlock(RWLock*);
157 b2cfc4e2 2003-09-30 devnull extern void runlock(RWLock*);
158 b2cfc4e2 2003-09-30 devnull extern int canrlock(RWLock*);
159 b2cfc4e2 2003-09-30 devnull extern void wlock(RWLock*);
160 b2cfc4e2 2003-09-30 devnull extern void wunlock(RWLock*);
161 b2cfc4e2 2003-09-30 devnull extern int canwlock(RWLock*);
162 b2cfc4e2 2003-09-30 devnull
163 b2cfc4e2 2003-09-30 devnull typedef struct Rendez Rendez;
164 b2cfc4e2 2003-09-30 devnull struct Rendez
165 b2cfc4e2 2003-09-30 devnull {
166 b2cfc4e2 2003-09-30 devnull QLock *l;
167 b2cfc4e2 2003-09-30 devnull QLp *head;
168 b2cfc4e2 2003-09-30 devnull QLp *tail;
169 b2cfc4e2 2003-09-30 devnull };
170 b2cfc4e2 2003-09-30 devnull
171 b2cfc4e2 2003-09-30 devnull extern void rsleep(Rendez*);
172 b2cfc4e2 2003-09-30 devnull extern int rwakeup(Rendez*);
173 b2cfc4e2 2003-09-30 devnull extern int rwakeupall(Rendez*);
174 b2cfc4e2 2003-09-30 devnull
175 b2cfc4e2 2003-09-30 devnull extern ulong rendezvous(ulong, ulong);
176 b2cfc4e2 2003-09-30 devnull
177 b2cfc4e2 2003-09-30 devnull /* one of a kind */
178 b2cfc4e2 2003-09-30 devnull extern void sysfatal(char*, ...);
179 20093746 2003-10-11 devnull extern int nrand(int);
180 20093746 2003-10-11 devnull extern long lrand(void);
181 b2cfc4e2 2003-09-30 devnull extern void setmalloctag(void*, ulong);
182 b2cfc4e2 2003-09-30 devnull extern void setrealloctag(void*, ulong);
183 b2cfc4e2 2003-09-30 devnull extern void *mallocz(ulong, int);
184 b2cfc4e2 2003-09-30 devnull extern long readn(int, void*, long);
185 b2cfc4e2 2003-09-30 devnull extern void exits(char*);
186 b2cfc4e2 2003-09-30 devnull extern void _exits(char*);
187 b2cfc4e2 2003-09-30 devnull extern ulong getcallerpc(void*);
188 b2cfc4e2 2003-09-30 devnull
189 b2cfc4e2 2003-09-30 devnull /* string routines */
190 b2cfc4e2 2003-09-30 devnull extern char* strecpy(char*, char*, char*);
191 b2cfc4e2 2003-09-30 devnull extern int tokenize(char*, char**, int);
192 b2cfc4e2 2003-09-30 devnull extern int cistrncmp(char*, char*, int);
193 b2cfc4e2 2003-09-30 devnull extern int cistrcmp(char*, char*);
194 b2cfc4e2 2003-09-30 devnull extern char* cistrstr(char*, char*);
195 b2cfc4e2 2003-09-30 devnull extern int getfields(char*, char**, int, int, char*);
196 b2cfc4e2 2003-09-30 devnull extern int gettokens(char *, char **, int, char *);
197 b2cfc4e2 2003-09-30 devnull
198 b2cfc4e2 2003-09-30 devnull /* formatting helpers */
199 b2cfc4e2 2003-09-30 devnull extern int dec64(uchar*, int, char*, int);
200 b2cfc4e2 2003-09-30 devnull extern int enc64(char*, int, uchar*, int);
201 b2cfc4e2 2003-09-30 devnull extern int dec32(uchar*, int, char*, int);
202 b2cfc4e2 2003-09-30 devnull extern int enc32(char*, int, uchar*, int);
203 b2cfc4e2 2003-09-30 devnull extern int dec16(uchar*, int, char*, int);
204 b2cfc4e2 2003-09-30 devnull extern int enc16(char*, int, uchar*, int);
205 b2cfc4e2 2003-09-30 devnull extern int encodefmt(Fmt*);
206 b2cfc4e2 2003-09-30 devnull
207 b2cfc4e2 2003-09-30 devnull /* error string */
208 b2cfc4e2 2003-09-30 devnull enum
209 b2cfc4e2 2003-09-30 devnull {
210 b2cfc4e2 2003-09-30 devnull ERRMAX = 128
211 b2cfc4e2 2003-09-30 devnull };
212 b2cfc4e2 2003-09-30 devnull extern void rerrstr(char*, uint);
213 b2cfc4e2 2003-09-30 devnull extern void werrstr(char*, ...);
214 b2cfc4e2 2003-09-30 devnull extern int errstr(char*, uint);
215 b2cfc4e2 2003-09-30 devnull
216 b2cfc4e2 2003-09-30 devnull /* compiler directives on plan 9 */
217 b2cfc4e2 2003-09-30 devnull #define USED(x) if(x){}else{}
218 b2cfc4e2 2003-09-30 devnull #define SET(x) ((x)=0)
219 b2cfc4e2 2003-09-30 devnull
220 b2cfc4e2 2003-09-30 devnull /* command line */
221 b2cfc4e2 2003-09-30 devnull extern char *argv0;
222 a995e477 2003-10-01 devnull extern void __fixargv0(void);
223 a995e477 2003-10-01 devnull #define ARGBEGIN for((argv0||(argv0=(__fixargv0(),*argv))),argv++,argc--;\
224 b2cfc4e2 2003-09-30 devnull argv[0] && argv[0][0]=='-' && argv[0][1];\
225 b2cfc4e2 2003-09-30 devnull argc--, argv++) {\
226 b2cfc4e2 2003-09-30 devnull char *_args, *_argt;\
227 b2cfc4e2 2003-09-30 devnull Rune _argc;\
228 b2cfc4e2 2003-09-30 devnull _args = &argv[0][1];\
229 b2cfc4e2 2003-09-30 devnull if(_args[0]=='-' && _args[1]==0){\
230 b2cfc4e2 2003-09-30 devnull argc--; argv++; break;\
231 b2cfc4e2 2003-09-30 devnull }\
232 b2cfc4e2 2003-09-30 devnull _argc = 0;\
233 b2cfc4e2 2003-09-30 devnull while(*_args && (_args += chartorune(&_argc, _args)))\
234 b2cfc4e2 2003-09-30 devnull switch(_argc)
235 b2cfc4e2 2003-09-30 devnull #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
236 b2cfc4e2 2003-09-30 devnull #define ARGF() (_argt=_args, _args="",\
237 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
238 b2cfc4e2 2003-09-30 devnull #define EARGF(x) (_argt=_args, _args="",\
239 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
240 b2cfc4e2 2003-09-30 devnull
241 b2cfc4e2 2003-09-30 devnull #define ARGC() _argc
242 b2cfc4e2 2003-09-30 devnull
243 b2cfc4e2 2003-09-30 devnull #define OREAD O_RDONLY
244 b2cfc4e2 2003-09-30 devnull #define OWRITE O_WRONLY
245 b2cfc4e2 2003-09-30 devnull #define AEXIST 0
246 b2cfc4e2 2003-09-30 devnull #define AREAD 4
247 b2cfc4e2 2003-09-30 devnull #define AWRITE 2
248 b2cfc4e2 2003-09-30 devnull #define AEXEC 1
249 b2cfc4e2 2003-09-30 devnull #define ORCLOSE 8
250 b2cfc4e2 2003-09-30 devnull #define OCEXEC 16
251 b2cfc4e2 2003-09-30 devnull
252 b2cfc4e2 2003-09-30 devnull #define dup dup2
253 b2cfc4e2 2003-09-30 devnull #define exec execv
254 b2cfc4e2 2003-09-30 devnull #define seek lseek
255 b2cfc4e2 2003-09-30 devnull #define getwd getcwd
256 b2cfc4e2 2003-09-30 devnull
257 b2cfc4e2 2003-09-30 devnull #if defined(__cplusplus)
258 b2cfc4e2 2003-09-30 devnull }
259 b2cfc4e2 2003-09-30 devnull #endif
260 b2cfc4e2 2003-09-30 devnull
261 b2cfc4e2 2003-09-30 devnull #endif /* _LIB9H_ */