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