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
21 b2cfc4e2 2003-09-30 devnull #ifndef _FMTH_
22 b2cfc4e2 2003-09-30 devnull # include <fmt.h>
23 b2cfc4e2 2003-09-30 devnull #endif
24 b2cfc4e2 2003-09-30 devnull
25 b2cfc4e2 2003-09-30 devnull #define nil ((void*)0)
26 b2cfc4e2 2003-09-30 devnull #define nelem(x) (sizeof(x)/sizeof((x)[0]))
27 b2cfc4e2 2003-09-30 devnull
28 b2cfc4e2 2003-09-30 devnull #define _NEEDUCHAR 1
29 b2cfc4e2 2003-09-30 devnull #define _NEEDUSHORT 1
30 b2cfc4e2 2003-09-30 devnull #define _NEEDUINT 1
31 b2cfc4e2 2003-09-30 devnull #define _NEEDULONG 1
32 b2cfc4e2 2003-09-30 devnull
33 b2cfc4e2 2003-09-30 devnull #if defined(__linux__)
34 b2cfc4e2 2003-09-30 devnull # include <sys/types.h>
35 b2cfc4e2 2003-09-30 devnull # if defined(__USE_MISC)
36 b2cfc4e2 2003-09-30 devnull # undef _NEEDUSHORT
37 b2cfc4e2 2003-09-30 devnull # undef _NEEDUINT
38 b2cfc4e2 2003-09-30 devnull # undef _NEEDULONG
39 b2cfc4e2 2003-09-30 devnull # endif
40 b2cfc4e2 2003-09-30 devnull #endif
41 b2cfc4e2 2003-09-30 devnull #if defined(__FreeBSD__)
42 b2cfc4e2 2003-09-30 devnull # include <sys/types.h>
43 b2cfc4e2 2003-09-30 devnull # if !defined(_POSIX_SOURCE)
44 b2cfc4e2 2003-09-30 devnull # undef _NEEDUSHORT
45 b2cfc4e2 2003-09-30 devnull # undef _NEEDUINT
46 b2cfc4e2 2003-09-30 devnull # endif
47 b2cfc4e2 2003-09-30 devnull #endif
48 b2cfc4e2 2003-09-30 devnull
49 b2cfc4e2 2003-09-30 devnull typedef signed char schar;
50 b2cfc4e2 2003-09-30 devnull typedef unsigned int u32int;
51 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUCHAR
52 b2cfc4e2 2003-09-30 devnull typedef unsigned char uchar;
53 b2cfc4e2 2003-09-30 devnull #endif
54 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUSHORT
55 b2cfc4e2 2003-09-30 devnull typedef unsigned short ushort;
56 b2cfc4e2 2003-09-30 devnull #endif
57 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDUINT
58 b2cfc4e2 2003-09-30 devnull typedef unsigned int uint;
59 b2cfc4e2 2003-09-30 devnull #endif
60 b2cfc4e2 2003-09-30 devnull #ifdef _NEEDULONG
61 b2cfc4e2 2003-09-30 devnull typedef unsigned long ulong;
62 b2cfc4e2 2003-09-30 devnull #endif
63 b2cfc4e2 2003-09-30 devnull typedef unsigned long long uvlong;
64 b2cfc4e2 2003-09-30 devnull typedef long long vlong;
65 b2cfc4e2 2003-09-30 devnull
66 b2cfc4e2 2003-09-30 devnull /* rfork to create new process running fn(arg) */
67 b2cfc4e2 2003-09-30 devnull
68 b2cfc4e2 2003-09-30 devnull #if defined(__FreeBSD__)
69 b2cfc4e2 2003-09-30 devnull #undef RFFDG
70 b2cfc4e2 2003-09-30 devnull #undef RFNOTEG
71 b2cfc4e2 2003-09-30 devnull #undef RFPROC
72 b2cfc4e2 2003-09-30 devnull #undef RFMEM
73 b2cfc4e2 2003-09-30 devnull #undef RFNOWAIT
74 b2cfc4e2 2003-09-30 devnull #undef RFCFDG
75 b2cfc4e2 2003-09-30 devnull #endif
76 b2cfc4e2 2003-09-30 devnull
77 b2cfc4e2 2003-09-30 devnull enum
78 b2cfc4e2 2003-09-30 devnull {
79 b2cfc4e2 2003-09-30 devnull /* RFNAMEG = (1<<0), */
80 b2cfc4e2 2003-09-30 devnull /* RFENVG = (1<<1), */
81 b2cfc4e2 2003-09-30 devnull RFFDG = (1<<2),
82 b2cfc4e2 2003-09-30 devnull RFNOTEG = (1<<3),
83 b2cfc4e2 2003-09-30 devnull RFPROC = (1<<4),
84 b2cfc4e2 2003-09-30 devnull RFMEM = (1<<5),
85 b2cfc4e2 2003-09-30 devnull RFNOWAIT = (1<<6),
86 b2cfc4e2 2003-09-30 devnull /* RFCNAMEG = (1<<10), */
87 b2cfc4e2 2003-09-30 devnull /* RFCENVG = (1<<11), */
88 b2cfc4e2 2003-09-30 devnull RFCFDG = (1<<12),
89 b2cfc4e2 2003-09-30 devnull /* RFREND = (1<<13), */
90 b2cfc4e2 2003-09-30 devnull /* RFNOMNT = (1<<14) */
91 b2cfc4e2 2003-09-30 devnull };
92 b2cfc4e2 2003-09-30 devnull extern int ffork(int, void(*)(void*), void*);
93 b2cfc4e2 2003-09-30 devnull
94 b2cfc4e2 2003-09-30 devnull /* wait for processes */
95 b2cfc4e2 2003-09-30 devnull #define wait _p9wait
96 b2cfc4e2 2003-09-30 devnull typedef struct Waitmsg Waitmsg;
97 b2cfc4e2 2003-09-30 devnull struct Waitmsg
98 b2cfc4e2 2003-09-30 devnull {
99 b2cfc4e2 2003-09-30 devnull int pid; /* of loved one */
100 b2cfc4e2 2003-09-30 devnull ulong time[3]; /* of loved one & descendants */
101 b2cfc4e2 2003-09-30 devnull char *msg;
102 b2cfc4e2 2003-09-30 devnull };
103 b2cfc4e2 2003-09-30 devnull extern int await(char*, int);
104 b2cfc4e2 2003-09-30 devnull extern Waitmsg* wait(void);
105 b2cfc4e2 2003-09-30 devnull
106 b2cfc4e2 2003-09-30 devnull /* synchronization */
107 b2cfc4e2 2003-09-30 devnull typedef struct Lock Lock;
108 b2cfc4e2 2003-09-30 devnull struct Lock
109 b2cfc4e2 2003-09-30 devnull {
110 b2cfc4e2 2003-09-30 devnull int val;
111 b2cfc4e2 2003-09-30 devnull };
112 b2cfc4e2 2003-09-30 devnull
113 b2cfc4e2 2003-09-30 devnull extern int _tas(void*);
114 b2cfc4e2 2003-09-30 devnull extern void lock(Lock*);
115 b2cfc4e2 2003-09-30 devnull extern void unlock(Lock*);
116 b2cfc4e2 2003-09-30 devnull extern int canlock(Lock*);
117 b2cfc4e2 2003-09-30 devnull
118 b2cfc4e2 2003-09-30 devnull typedef struct QLp QLp;
119 b2cfc4e2 2003-09-30 devnull struct QLp
120 b2cfc4e2 2003-09-30 devnull {
121 b2cfc4e2 2003-09-30 devnull int inuse;
122 b2cfc4e2 2003-09-30 devnull QLp *next;
123 b2cfc4e2 2003-09-30 devnull int state;
124 b2cfc4e2 2003-09-30 devnull };
125 b2cfc4e2 2003-09-30 devnull
126 b2cfc4e2 2003-09-30 devnull typedef struct QLock QLock;
127 b2cfc4e2 2003-09-30 devnull struct QLock
128 b2cfc4e2 2003-09-30 devnull {
129 b2cfc4e2 2003-09-30 devnull Lock lock;
130 b2cfc4e2 2003-09-30 devnull int locked;
131 b2cfc4e2 2003-09-30 devnull QLp *head;
132 b2cfc4e2 2003-09-30 devnull QLp *tail;
133 b2cfc4e2 2003-09-30 devnull };
134 b2cfc4e2 2003-09-30 devnull
135 b2cfc4e2 2003-09-30 devnull extern void qlock(QLock*);
136 b2cfc4e2 2003-09-30 devnull extern void qunlock(QLock*);
137 b2cfc4e2 2003-09-30 devnull extern int canqlock(QLock*);
138 b2cfc4e2 2003-09-30 devnull extern void _qlockinit(ulong (*)(ulong, ulong));
139 b2cfc4e2 2003-09-30 devnull
140 b2cfc4e2 2003-09-30 devnull typedef struct RWLock RWLock;
141 b2cfc4e2 2003-09-30 devnull struct RWLock
142 b2cfc4e2 2003-09-30 devnull {
143 b2cfc4e2 2003-09-30 devnull Lock lock;
144 b2cfc4e2 2003-09-30 devnull int readers;
145 b2cfc4e2 2003-09-30 devnull int writer;
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 rlock(RWLock*);
151 b2cfc4e2 2003-09-30 devnull extern void runlock(RWLock*);
152 b2cfc4e2 2003-09-30 devnull extern int canrlock(RWLock*);
153 b2cfc4e2 2003-09-30 devnull extern void wlock(RWLock*);
154 b2cfc4e2 2003-09-30 devnull extern void wunlock(RWLock*);
155 b2cfc4e2 2003-09-30 devnull extern int canwlock(RWLock*);
156 b2cfc4e2 2003-09-30 devnull
157 b2cfc4e2 2003-09-30 devnull typedef struct Rendez Rendez;
158 b2cfc4e2 2003-09-30 devnull struct Rendez
159 b2cfc4e2 2003-09-30 devnull {
160 b2cfc4e2 2003-09-30 devnull QLock *l;
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 rsleep(Rendez*);
166 b2cfc4e2 2003-09-30 devnull extern int rwakeup(Rendez*);
167 b2cfc4e2 2003-09-30 devnull extern int rwakeupall(Rendez*);
168 b2cfc4e2 2003-09-30 devnull
169 b2cfc4e2 2003-09-30 devnull extern ulong rendezvous(ulong, ulong);
170 b2cfc4e2 2003-09-30 devnull
171 b2cfc4e2 2003-09-30 devnull /* one of a kind */
172 b2cfc4e2 2003-09-30 devnull extern void sysfatal(char*, ...);
173 b2cfc4e2 2003-09-30 devnull extern int nrand(int);
174 b2cfc4e2 2003-09-30 devnull extern void setmalloctag(void*, ulong);
175 b2cfc4e2 2003-09-30 devnull extern void setrealloctag(void*, ulong);
176 b2cfc4e2 2003-09-30 devnull extern void *mallocz(ulong, int);
177 b2cfc4e2 2003-09-30 devnull extern long readn(int, void*, long);
178 b2cfc4e2 2003-09-30 devnull extern void exits(char*);
179 b2cfc4e2 2003-09-30 devnull extern void _exits(char*);
180 b2cfc4e2 2003-09-30 devnull extern ulong getcallerpc(void*);
181 b2cfc4e2 2003-09-30 devnull
182 b2cfc4e2 2003-09-30 devnull /* string routines */
183 b2cfc4e2 2003-09-30 devnull extern char* strecpy(char*, char*, char*);
184 b2cfc4e2 2003-09-30 devnull extern int tokenize(char*, char**, int);
185 b2cfc4e2 2003-09-30 devnull extern int cistrncmp(char*, char*, int);
186 b2cfc4e2 2003-09-30 devnull extern int cistrcmp(char*, char*);
187 b2cfc4e2 2003-09-30 devnull extern char* cistrstr(char*, char*);
188 b2cfc4e2 2003-09-30 devnull extern int getfields(char*, char**, int, int, char*);
189 b2cfc4e2 2003-09-30 devnull extern int gettokens(char *, char **, int, char *);
190 b2cfc4e2 2003-09-30 devnull
191 b2cfc4e2 2003-09-30 devnull /* formatting helpers */
192 b2cfc4e2 2003-09-30 devnull extern int dec64(uchar*, int, char*, int);
193 b2cfc4e2 2003-09-30 devnull extern int enc64(char*, int, uchar*, int);
194 b2cfc4e2 2003-09-30 devnull extern int dec32(uchar*, int, char*, int);
195 b2cfc4e2 2003-09-30 devnull extern int enc32(char*, int, uchar*, int);
196 b2cfc4e2 2003-09-30 devnull extern int dec16(uchar*, int, char*, int);
197 b2cfc4e2 2003-09-30 devnull extern int enc16(char*, int, uchar*, int);
198 b2cfc4e2 2003-09-30 devnull extern int encodefmt(Fmt*);
199 b2cfc4e2 2003-09-30 devnull
200 b2cfc4e2 2003-09-30 devnull /* error string */
201 b2cfc4e2 2003-09-30 devnull enum
202 b2cfc4e2 2003-09-30 devnull {
203 b2cfc4e2 2003-09-30 devnull ERRMAX = 128
204 b2cfc4e2 2003-09-30 devnull };
205 b2cfc4e2 2003-09-30 devnull extern void rerrstr(char*, uint);
206 b2cfc4e2 2003-09-30 devnull extern void werrstr(char*, ...);
207 b2cfc4e2 2003-09-30 devnull extern int errstr(char*, uint);
208 b2cfc4e2 2003-09-30 devnull
209 b2cfc4e2 2003-09-30 devnull /* compiler directives on plan 9 */
210 b2cfc4e2 2003-09-30 devnull #define USED(x) if(x){}else{}
211 b2cfc4e2 2003-09-30 devnull #define SET(x) ((x)=0)
212 b2cfc4e2 2003-09-30 devnull
213 b2cfc4e2 2003-09-30 devnull /* command line */
214 b2cfc4e2 2003-09-30 devnull extern char *argv0;
215 b2cfc4e2 2003-09-30 devnull #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\
216 b2cfc4e2 2003-09-30 devnull argv[0] && argv[0][0]=='-' && argv[0][1];\
217 b2cfc4e2 2003-09-30 devnull argc--, argv++) {\
218 b2cfc4e2 2003-09-30 devnull char *_args, *_argt;\
219 b2cfc4e2 2003-09-30 devnull Rune _argc;\
220 b2cfc4e2 2003-09-30 devnull _args = &argv[0][1];\
221 b2cfc4e2 2003-09-30 devnull if(_args[0]=='-' && _args[1]==0){\
222 b2cfc4e2 2003-09-30 devnull argc--; argv++; break;\
223 b2cfc4e2 2003-09-30 devnull }\
224 b2cfc4e2 2003-09-30 devnull _argc = 0;\
225 b2cfc4e2 2003-09-30 devnull while(*_args && (_args += chartorune(&_argc, _args)))\
226 b2cfc4e2 2003-09-30 devnull switch(_argc)
227 b2cfc4e2 2003-09-30 devnull #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
228 b2cfc4e2 2003-09-30 devnull #define ARGF() (_argt=_args, _args="",\
229 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
230 b2cfc4e2 2003-09-30 devnull #define EARGF(x) (_argt=_args, _args="",\
231 b2cfc4e2 2003-09-30 devnull (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
232 b2cfc4e2 2003-09-30 devnull
233 b2cfc4e2 2003-09-30 devnull #define ARGC() _argc
234 b2cfc4e2 2003-09-30 devnull
235 b2cfc4e2 2003-09-30 devnull #define OREAD O_RDONLY
236 b2cfc4e2 2003-09-30 devnull #define OWRITE O_WRONLY
237 b2cfc4e2 2003-09-30 devnull #define AEXIST 0
238 b2cfc4e2 2003-09-30 devnull #define AREAD 4
239 b2cfc4e2 2003-09-30 devnull #define AWRITE 2
240 b2cfc4e2 2003-09-30 devnull #define AEXEC 1
241 b2cfc4e2 2003-09-30 devnull
242 b2cfc4e2 2003-09-30 devnull #if defined(__cplusplus)
243 b2cfc4e2 2003-09-30 devnull }
244 b2cfc4e2 2003-09-30 devnull #endif
245 b2cfc4e2 2003-09-30 devnull
246 b2cfc4e2 2003-09-30 devnull #endif /* _LIB9H_ */