1
0

libc.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #pragma lib "libc.a"
  10. #pragma src "/sys/src/libc"
  11. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  12. #define offsetof(s, m) (uintptr_t)(&(((s*)0)->m))
  13. #define assert(x) if(x){}else _assert("x")
  14. /*
  15. * mem routines
  16. */
  17. extern void* memccpy(void*, void*, int, uint32_t);
  18. extern void* memset(void*, int, uint32_t);
  19. extern int memcmp(const void*, const void*, uint32_t);
  20. extern void* memcpy(void*, const void*, size_t);
  21. extern void* memmove(void*, const void*, size_t);
  22. extern void* memchr(const void*, int, uint32_t);
  23. /*
  24. * string routines
  25. */
  26. extern char* strcat(char*, char*);
  27. extern char* strchr(char*, int);
  28. extern int strcmp(char*, char*);
  29. extern char* strcpy(char*, char*);
  30. extern char* strecpy(char*, char*, char*);
  31. extern char* strdup(char*);
  32. extern char* strncat(char*, char*, int32_t);
  33. extern char* strncpy(char*, char*, uint32_t);
  34. extern int strncmp(char*, char*, int32_t);
  35. extern char* strpbrk(char*, char*);
  36. extern char* strrchr(char*, int);
  37. extern char* strtok(char*, char*);
  38. extern int32_t strlen(char*);
  39. extern int32_t strspn(char*, char*);
  40. extern int32_t strcspn(char*, char*);
  41. extern char* strstr(char*, char*);
  42. extern int cistrncmp(char*, char*, int);
  43. extern int cistrcmp(char*, char*);
  44. extern char* cistrstr(char*, char*);
  45. extern int tokenize(char*, char**, int);
  46. enum
  47. {
  48. UTFmax = 4, /* maximum bytes per rune */
  49. Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
  50. Runeself = 0x80, /* rune and UTF sequences are the same (<) */
  51. Runeerror = 0xFFFD, /* decoding error in UTF */
  52. Runemax = 0x10FFFF, /* 21-bit rune */
  53. Runemask = 0x1FFFFF, /* bits used by runes (see grep) */
  54. };
  55. /*
  56. * rune routines
  57. */
  58. extern int runetochar(char*, Rune*);
  59. extern int chartorune(Rune*, char*);
  60. extern int runelen(int32_t);
  61. extern int runenlen(Rune*, int);
  62. extern int fullrune(char*, int);
  63. extern int utflen(char*);
  64. extern int utfnlen(char*, int32_t);
  65. extern char* utfrune(char*, int32_t);
  66. extern char* utfrrune(char*, int32_t);
  67. extern char* utfutf(char*, char*);
  68. extern char* utfecpy(char*, char*, char*);
  69. extern Rune* runestrcat(Rune*, Rune*);
  70. extern Rune* runestrchr(Rune*, Rune);
  71. extern int runestrcmp(Rune*, Rune*);
  72. extern Rune* runestrcpy(Rune*, Rune*);
  73. extern Rune* runestrncpy(Rune*, Rune*, int32_t);
  74. extern Rune* runestrecpy(Rune*, Rune*, Rune*);
  75. extern Rune* runestrdup(Rune*);
  76. extern Rune* runestrncat(Rune*, Rune*, int32_t);
  77. extern int runestrncmp(Rune*, Rune*, int32_t);
  78. extern Rune* runestrrchr(Rune*, Rune);
  79. extern int32_t runestrlen(Rune*);
  80. extern Rune* runestrstr(Rune*, Rune*);
  81. extern Rune tolowerrune(Rune);
  82. extern Rune totitlerune(Rune);
  83. extern Rune toupperrune(Rune);
  84. extern Rune tobaserune(Rune);
  85. extern int isalpharune(Rune);
  86. extern int isbaserune(Rune);
  87. extern int isdigitrune(Rune);
  88. extern int islowerrune(Rune);
  89. extern int isspacerune(Rune);
  90. extern int istitlerune(Rune);
  91. extern int isupperrune(Rune);
  92. /*
  93. * malloc
  94. */
  95. extern void* malloc(size_t);
  96. extern void* mallocz(uint32_t, int);
  97. extern void free(void*);
  98. extern uint32_t msize(void*);
  99. extern void* mallocalign(uint32_t, uint32_t, int32_t, uint32_t);
  100. extern void* calloc(uint32_t, size_t);
  101. extern void* realloc(void*, size_t);
  102. void setmalloctag(void*, uintptr_t);
  103. void setrealloctag(void*, uintptr_t);
  104. uintptr_t getmalloctag(void*);
  105. uintptr_t getrealloctag(void*);
  106. void* malloctopoolblock(void*);
  107. /*
  108. * print routines
  109. */
  110. typedef struct Fmt Fmt;
  111. struct Fmt{
  112. uint8_t runes; /* output buffer is runes or chars? */
  113. void *start; /* of buffer */
  114. void *to; /* current place in the buffer */
  115. void *stop; /* end of the buffer; overwritten if flush fails */
  116. int (*flush)(Fmt *); /* called when to == stop */
  117. void *farg; /* to make flush a closure */
  118. int nfmt; /* num chars formatted so far */
  119. va_list args; /* args passed to dofmt */
  120. int r; /* % format Rune */
  121. int width;
  122. int prec;
  123. uint32_t flags;
  124. };
  125. enum{
  126. FmtWidth = 1,
  127. FmtLeft = FmtWidth << 1,
  128. FmtPrec = FmtLeft << 1,
  129. FmtSharp = FmtPrec << 1,
  130. FmtSpace = FmtSharp << 1,
  131. FmtSign = FmtSpace << 1,
  132. FmtZero = FmtSign << 1,
  133. FmtUnsigned = FmtZero << 1,
  134. FmtShort = FmtUnsigned << 1,
  135. FmtLong = FmtShort << 1,
  136. FmtVLong = FmtLong << 1,
  137. FmtComma = FmtVLong << 1,
  138. FmtByte = FmtComma << 1,
  139. FmtFlag = FmtByte << 1
  140. };
  141. extern int print(char*, ...);
  142. extern char* seprint(char*, char*, char*, ...);
  143. extern char* vseprint(char*, char*, char*, va_list);
  144. extern int snprint(char*, int, char*, ...);
  145. extern int vsnprint(char*, int, char*, va_list);
  146. extern char* smprint(char*, ...);
  147. extern char* vsmprint(char*, va_list);
  148. extern int sprint(char*, char*, ...);
  149. extern int fprint(int, char*, ...);
  150. extern int vfprint(int, char*, va_list);
  151. extern int runesprint(Rune*, char*, ...);
  152. extern int runesnprint(Rune*, int, char*, ...);
  153. extern int runevsnprint(Rune*, int, char*, va_list);
  154. extern Rune* runeseprint(Rune*, Rune*, char*, ...);
  155. extern Rune* runevseprint(Rune*, Rune*, char*, va_list);
  156. extern Rune* runesmprint(char*, ...);
  157. extern Rune* runevsmprint(char*, va_list);
  158. extern int fmtfdinit(Fmt*, int, char*, int);
  159. extern int fmtfdflush(Fmt*);
  160. extern int fmtstrinit(Fmt*);
  161. extern char* fmtstrflush(Fmt*);
  162. extern int runefmtstrinit(Fmt*);
  163. extern Rune* runefmtstrflush(Fmt*);
  164. #pragma varargck argpos fmtprint 2
  165. #pragma varargck argpos fprint 2
  166. #pragma varargck argpos print 1
  167. #pragma varargck argpos runeseprint 3
  168. #pragma varargck argpos runesmprint 1
  169. #pragma varargck argpos runesnprint 3
  170. #pragma varargck argpos runesprint 2
  171. #pragma varargck argpos seprint 3
  172. #pragma varargck argpos smprint 1
  173. #pragma varargck argpos snprint 3
  174. #pragma varargck argpos sprint 2
  175. #pragma varargck type "lld" vlong
  176. #pragma varargck type "llo" vlong
  177. #pragma varargck type "llx" vlong
  178. #pragma varargck type "llb" vlong
  179. #pragma varargck type "lld" uvlong
  180. #pragma varargck type "llo" uvlong
  181. #pragma varargck type "llx" uvlong
  182. #pragma varargck type "llb" uvlong
  183. #pragma varargck type "ld" long
  184. #pragma varargck type "lo" long
  185. #pragma varargck type "lx" long
  186. #pragma varargck type "lb" long
  187. #pragma varargck type "ld" ulong
  188. #pragma varargck type "lo" ulong
  189. #pragma varargck type "lx" ulong
  190. #pragma varargck type "lb" ulong
  191. #pragma varargck type "d" int
  192. #pragma varargck type "o" int
  193. #pragma varargck type "x" int
  194. #pragma varargck type "c" int
  195. #pragma varargck type "C" int
  196. #pragma varargck type "b" int
  197. #pragma varargck type "d" uint
  198. #pragma varargck type "x" uint
  199. #pragma varargck type "c" uint
  200. #pragma varargck type "C" uint
  201. #pragma varargck type "b" uint
  202. #pragma varargck type "f" double
  203. #pragma varargck type "e" double
  204. #pragma varargck type "g" double
  205. #pragma varargck type "s" char*
  206. #pragma varargck type "q" char*
  207. #pragma varargck type "S" Rune*
  208. #pragma varargck type "Q" Rune*
  209. #pragma varargck type "r" void
  210. #pragma varargck type "%" void
  211. #pragma varargck type "n" int*
  212. #pragma varargck type "p" uintptr
  213. #pragma varargck type "p" void*
  214. #pragma varargck flag ','
  215. #pragma varargck flag ' '
  216. #pragma varargck flag 'h'
  217. #pragma varargck type "<" void*
  218. #pragma varargck type "[" void*
  219. #pragma varargck type "H" void*
  220. #pragma varargck type "lH" void*
  221. extern int fmtinstall(int, int (*)(Fmt*));
  222. extern int dofmt(Fmt*, char*);
  223. extern int dorfmt(Fmt*, Rune*);
  224. extern int fmtprint(Fmt*, char*, ...);
  225. extern int fmtvprint(Fmt*, char*, va_list);
  226. extern int fmtrune(Fmt*, int);
  227. extern int fmtstrcpy(Fmt*, char*);
  228. extern int fmtrunestrcpy(Fmt*, Rune*);
  229. /*
  230. * error string for %r
  231. * supplied on per os basis, not part of fmt library
  232. */
  233. extern int errfmt(Fmt *f);
  234. /*
  235. * quoted strings
  236. */
  237. extern char *unquotestrdup(char*);
  238. extern Rune *unquoterunestrdup(Rune*);
  239. extern char *quotestrdup(char*);
  240. extern Rune *quoterunestrdup(Rune*);
  241. extern int quotestrfmt(Fmt*);
  242. extern int quoterunestrfmt(Fmt*);
  243. extern void quotefmtinstall(void);
  244. extern int (*doquote)(int);
  245. extern int needsrcquote(int);
  246. /*
  247. * random number
  248. */
  249. extern void srand(int32_t);
  250. extern int rand(void);
  251. extern int nrand(int);
  252. extern int32_t lrand(void);
  253. extern int32_t lnrand(int32_t);
  254. extern double frand(void);
  255. extern uint32_t truerand(void); /* uses /dev/random */
  256. extern uint32_t ntruerand(uint32_t); /* uses /dev/random */
  257. /*
  258. * math
  259. */
  260. extern uint32_t getfcr(void);
  261. extern void setfsr(uint32_t);
  262. extern uint32_t getfsr(void);
  263. extern void setfcr(uint32_t);
  264. extern double NaN(void);
  265. extern double Inf(int);
  266. extern int isNaN(double);
  267. extern int isInf(double, int);
  268. extern uint32_t umuldiv(uint32_t, uint32_t, uint32_t);
  269. extern int32_t muldiv(int32_t, int32_t, int32_t);
  270. extern double pow(double, double);
  271. extern double atan2(double, double);
  272. extern double fabs(double);
  273. extern double atan(double);
  274. extern double log(double);
  275. extern double log10(double);
  276. extern double exp(double);
  277. extern double floor(double);
  278. extern double ceil(double);
  279. extern double hypot(double, double);
  280. extern double sin(double);
  281. extern double cos(double);
  282. extern double tan(double);
  283. extern double asin(double);
  284. extern double acos(double);
  285. extern double sinh(double);
  286. extern double cosh(double);
  287. extern double tanh(double);
  288. extern double sqrt(double);
  289. extern double fmod(double, double);
  290. #define HUGE 3.4028234e38
  291. #define PIO2 1.570796326794896619231e0
  292. #define PI (PIO2+PIO2)
  293. /*
  294. * Time-of-day
  295. */
  296. typedef
  297. struct Tm
  298. {
  299. int sec;
  300. int min;
  301. int hour;
  302. int mday;
  303. int mon;
  304. int year;
  305. int wday;
  306. int yday;
  307. char zone[4];
  308. int tzoff;
  309. } Tm;
  310. extern Tm* gmtime(int32_t);
  311. extern Tm* localtime(int32_t);
  312. extern char* asctime(Tm*);
  313. extern char* ctime(int32_t);
  314. extern double cputime(void);
  315. extern int32_t times(int32_t*);
  316. extern int32_t tm2sec(Tm*);
  317. extern int64_t nsec(void);
  318. extern void cycles(uint64_t*); /* 64-bit value of the cycle counter if there is one, 0 if there isn't */
  319. /*
  320. * one-of-a-kind
  321. */
  322. enum
  323. {
  324. PNPROC = 1,
  325. PNGROUP = 2,
  326. };
  327. extern void _assert(char*);
  328. extern int abs(int);
  329. extern int atexit(void(*)(void));
  330. extern void atexitdont(void(*)(void));
  331. extern int atnotify(int(*)(void*, char*), int);
  332. extern double atof(char*);
  333. extern int atoi(char*);
  334. extern int32_t atol(char*);
  335. extern int64_t atoll(char*);
  336. extern double charstod(int(*)(void*), void*);
  337. extern char* cleanname(char*);
  338. extern int decrypt(void*, void*, int);
  339. extern int encrypt(void*, void*, int);
  340. extern int dec64(uint8_t*, int, char*, int);
  341. extern int enc64(char*, int, uint8_t*, int);
  342. extern int dec32(uint8_t*, int, char*, int);
  343. extern int enc32(char*, int, uint8_t*, int);
  344. extern int dec16(uint8_t*, int, char*, int);
  345. extern int enc16(char*, int, uint8_t*, int);
  346. extern int encodefmt(Fmt*);
  347. extern void exits(char*);
  348. extern double frexp(double, int*);
  349. extern uintptr getcallerpc(void*);
  350. extern char* getenv(char*);
  351. extern int getfields(char*, char**, int, int, char*);
  352. extern int gettokens(char *, char **, int, char *);
  353. extern char* getuser(void);
  354. extern char* getwd(char*, int);
  355. extern int iounit(int);
  356. extern int32_t labs(int32_t);
  357. extern double ldexp(double, int);
  358. extern void longjmp(jmp_buf, int);
  359. extern char* mktemp(char*);
  360. extern double modf(double, double*);
  361. extern int netcrypt(void*, void*);
  362. extern void notejmp(void*, jmp_buf, int);
  363. extern void perror(const char*);
  364. extern int postnote(int, int, char *);
  365. extern double pow10(int);
  366. extern int putenv(char*, char*);
  367. extern void qsort(void*, int32_t, int32_t,
  368. int (*)(const void*, const void*));
  369. extern int setjmp(jmp_buf);
  370. extern double strtod(char*, char**);
  371. extern int32_t strtol(char*, char**, int);
  372. extern uint32_t strtoul(char*, char**, int);
  373. extern int64_t strtoll(char*, char**, int);
  374. extern uint64_t strtoull(char*, char**, int);
  375. extern void sysfatal(char*, ...);
  376. #pragma varargck argpos sysfatal 1
  377. extern void syslog(int, char*, char*, ...);
  378. #pragma varargck argpos syslog 3
  379. extern int32_t time(int32_t*);
  380. extern int tolower(int);
  381. extern int toupper(int);
  382. /*
  383. * profiling
  384. */
  385. enum {
  386. Profoff, /* No profiling */
  387. Profuser, /* Measure user time only (default) */
  388. Profkernel, /* Measure user + kernel time */
  389. Proftime, /* Measure total time */
  390. Profsample, /* Use clock interrupt to sample (default when there is no cycle counter) */
  391. }; /* what */
  392. extern void prof(void (*fn)(void*), void *arg, int entries, int what);
  393. /*
  394. * atomic
  395. */
  396. int32_t ainc(int32_t*);
  397. int32_t adec(int32_t*);
  398. int cas32(uint32_t*, uint32_t, uint32_t);
  399. int casp(void**, void*, void*);
  400. int casl(uint32_t*, uint32_t, uint32_t);
  401. /*
  402. * synchronization
  403. */
  404. typedef
  405. struct Lock {
  406. int32_t key;
  407. int32_t sem;
  408. } Lock;
  409. extern int _tas(int*);
  410. extern void lock(Lock*);
  411. extern void unlock(Lock*);
  412. extern int canlock(Lock*);
  413. typedef struct QLp QLp;
  414. struct QLp
  415. {
  416. int inuse;
  417. QLp *next;
  418. char state;
  419. };
  420. typedef
  421. struct QLock
  422. {
  423. Lock lock;
  424. int locked;
  425. QLp *head;
  426. QLp *tail;
  427. } QLock;
  428. extern void qlock(QLock*);
  429. extern void qunlock(QLock*);
  430. extern int canqlock(QLock*);
  431. extern void _qlockinit(void* (*)(void*, void*)); /* called only by the thread library */
  432. typedef
  433. struct RWLock
  434. {
  435. Lock lock;
  436. int _readers; /* number of readers */
  437. int writer; /* number of writers */
  438. QLp *head; /* list of waiting processes */
  439. QLp *tail;
  440. } RWLock;
  441. extern void rlock(RWLock*);
  442. extern void runlock(RWLock*);
  443. extern int canrlock(RWLock*);
  444. extern void wlock(RWLock*);
  445. extern void wunlock(RWLock*);
  446. extern int canwlock(RWLock*);
  447. typedef
  448. struct Rendez
  449. {
  450. QLock *l;
  451. QLp *head;
  452. QLp *tail;
  453. } Rendez;
  454. extern void rsleep(Rendez*); /* unlocks r->l, sleeps, locks r->l again */
  455. extern int rwakeup(Rendez*);
  456. extern int rwakeupall(Rendez*);
  457. extern void** privalloc(void);
  458. extern void privfree(void**);
  459. /*
  460. * network dialing
  461. */
  462. #define NETPATHLEN 40
  463. extern int accept(int, char*);
  464. extern int announce(char*, char*);
  465. extern int dial(char*, char*, char*, int*);
  466. extern void setnetmtpt(char*, int, char*);
  467. extern int hangup(int);
  468. extern int listen(char*, char*);
  469. extern char* netmkaddr(char*, char*, char*);
  470. extern int reject(int, char*, char*);
  471. /*
  472. * encryption
  473. */
  474. extern int pushssl(int, char*, char*, char*, int*);
  475. extern int pushtls(int, char*, char*, int, char*,
  476. char*);
  477. /*
  478. * network services
  479. */
  480. typedef struct NetConnInfo NetConnInfo;
  481. struct NetConnInfo
  482. {
  483. char *dir; /* connection directory */
  484. char *root; /* network root */
  485. char *spec; /* binding spec */
  486. char *lsys; /* local system */
  487. char *lserv; /* local service */
  488. char *rsys; /* remote system */
  489. char *rserv; /* remote service */
  490. char *laddr; /* local address */
  491. char *raddr; /* remote address */
  492. };
  493. extern NetConnInfo* getnetconninfo(char*, int);
  494. extern void freenetconninfo(NetConnInfo*);
  495. /*
  496. * system calls
  497. *
  498. */
  499. #define STATMAX 65535U /* max length of machine-independent stat structure */
  500. #define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */
  501. #define ERRMAX 128 /* max length of error string */
  502. #define MORDER 0x0003 /* mask for bits defining order of mounting */
  503. #define MREPL 0x0000 /* mount replaces object */
  504. #define MBEFORE 0x0001 /* mount goes before others in union directory */
  505. #define MAFTER 0x0002 /* mount goes after others in union directory */
  506. #define MCREATE 0x0004 /* permit creation in mounted directory */
  507. #define MCACHE 0x0010 /* cache some data */
  508. #define MMASK 0x0017 /* all bits on */
  509. #define OREAD 0 /* open for read */
  510. #define OWRITE 1 /* write */
  511. #define ORDWR 2 /* read and write */
  512. #define OEXEC 3 /* execute, == read but check execute permission */
  513. #define OTRUNC 16 /* or'ed in (except for exec), truncate file first */
  514. #define OCEXEC 32 /* or'ed in, close on exec */
  515. #define ORCLOSE 64 /* or'ed in, remove on close */
  516. #define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */
  517. // #define OBEHIND 0x2000 /* use write behind for writes [for 9n] */
  518. #define AEXIST 0 /* accessible: exists */
  519. #define AEXEC 1 /* execute access */
  520. #define AWRITE 2 /* write access */
  521. #define AREAD 4 /* read access */
  522. /* Segattch */
  523. #define SG_RONLY 0040 /* read only */
  524. #define SG_CEXEC 0100 /* detach on exec */
  525. #define NCONT 0 /* continue after note */
  526. #define NDFLT 1 /* terminate after note */
  527. #define NSAVE 2 /* clear note but hold state */
  528. #define NRSTR 3 /* restore saved state */
  529. /* bits in Qid.type */
  530. #define QTDIR 0x80 /* type bit for directories */
  531. #define QTAPPEND 0x40 /* type bit for append only files */
  532. #define QTEXCL 0x20 /* type bit for exclusive use files */
  533. #define QTMOUNT 0x10 /* type bit for mounted channel */
  534. #define QTAUTH 0x08 /* type bit for authentication file */
  535. #define QTTMP 0x04 /* type bit for not-backed-up file */
  536. #define QTFILE 0x00 /* plain file */
  537. /* bits in Dir.mode */
  538. #define DMDIR 0x80000000 /* mode bit for directories */
  539. #define DMAPPEND 0x40000000 /* mode bit for append only files */
  540. #define DMEXCL 0x20000000 /* mode bit for exclusive use files */
  541. #define DMMOUNT 0x10000000 /* mode bit for mounted channel */
  542. #define DMAUTH 0x08000000 /* mode bit for authentication file */
  543. #define DMTMP 0x04000000 /* mode bit for non-backed-up files */
  544. #define DMREAD 0x4 /* mode bit for read permission */
  545. #define DMWRITE 0x2 /* mode bit for write permission */
  546. #define DMEXEC 0x1 /* mode bit for execute permission */
  547. /* rfork */
  548. enum
  549. {
  550. RFNAMEG = (1<<0),
  551. RFENVG = (1<<1),
  552. RFFDG = (1<<2),
  553. RFNOTEG = (1<<3),
  554. RFPROC = (1<<4),
  555. RFMEM = (1<<5),
  556. RFNOWAIT = (1<<6),
  557. RFCNAMEG = (1<<10),
  558. RFCENVG = (1<<11),
  559. RFCFDG = (1<<12),
  560. RFREND = (1<<13),
  561. RFNOMNT = (1<<14)
  562. };
  563. typedef
  564. struct Qid
  565. {
  566. uint64_t path;
  567. uint32_t vers;
  568. uint8_t type;
  569. } Qid;
  570. typedef
  571. struct Dir {
  572. /* system-modified data */
  573. uint16_t type; /* server type */
  574. uint dev; /* server subtype */
  575. /* file data */
  576. Qid qid; /* unique id from server */
  577. uint32_t mode; /* permissions */
  578. uint32_t atime; /* last read time */
  579. uint32_t mtime; /* last write time */
  580. int64_t length; /* file length */
  581. char *name; /* last element of path */
  582. char *uid; /* owner name */
  583. char *gid; /* group name */
  584. char *muid; /* last modifier name */
  585. } Dir;
  586. /* keep /sys/src/ape/lib/ap/plan9/sys9.h in sync with this -rsc */
  587. typedef
  588. struct Waitmsg
  589. {
  590. int pid; /* of loved one */
  591. uint32_t time[3]; /* of loved one & descendants */
  592. char *msg;
  593. } Waitmsg;
  594. typedef
  595. struct IOchunk
  596. {
  597. void *addr;
  598. uint32_t len;
  599. } IOchunk;
  600. extern void _exits(char*);
  601. extern void abort(void);
  602. extern int access(char*, int);
  603. extern int32_t alarm(uint32_t);
  604. extern int await(char*, int);
  605. extern int bind(char*, char*, int);
  606. extern int brk(void*);
  607. extern int chdir(char*);
  608. extern int close(int);
  609. extern int create(char*, int, uint32_t);
  610. extern int dup(int, int);
  611. extern int errstr(char*, uint);
  612. extern int exec(char*, char*[]);
  613. extern int execl(char*, ...);
  614. extern int fork(void);
  615. extern int rfork(int);
  616. extern int fauth(int, char*);
  617. extern int fstat(int, uint8_t*, int);
  618. extern int fwstat(int, uint8_t*, int);
  619. extern int fversion(int, int, char*, int);
  620. extern int mount(int, int, char*, int, char*, int);
  621. extern int unmount(char*, char*);
  622. extern int noted(int);
  623. extern int notify(void(*)(void*, char*));
  624. extern int open(char*, int);
  625. extern int fd2path(int, char*, int);
  626. // extern int fdflush(int);
  627. extern int pipe(int*);
  628. extern int32_t pread(int, void*, int32_t, int64_t);
  629. extern int32_t preadv(int, IOchunk*, int, int64_t);
  630. extern int32_t pwrite(int, void*, int32_t, int64_t);
  631. extern int32_t pwritev(int, IOchunk*, int, int64_t);
  632. extern int32_t read(int, void*, int32_t);
  633. extern int32_t readn(int, void*, int32_t);
  634. extern int32_t readv(int, IOchunk*, int);
  635. extern int remove(char*);
  636. extern void* sbrk(uint32_t);
  637. extern int32_t oseek(int, int32_t, int);
  638. extern int64_t seek(int, int64_t, int);
  639. extern void* segattach(int, char*, void*, uint32_t);
  640. extern void* segbrk(void*, void*);
  641. extern int segdetach(void*);
  642. extern int segflush(void*, uint32_t);
  643. extern int segfree(void*, uint32_t);
  644. extern int semacquire(int32_t*, int);
  645. extern int32_t semrelease(int32_t*, int32_t);
  646. extern int sleep(int32_t);
  647. extern int stat(char*, uint8_t*, int);
  648. extern int tsemacquire(int32_t*, uint32_t);
  649. extern Waitmsg* wait(void);
  650. extern int waitpid(void);
  651. extern int32_t write(int, void*, int32_t);
  652. extern int32_t writev(int, IOchunk*, int);
  653. extern int wstat(char*, uint8_t*, int);
  654. extern void* rendezvous(void*, void*);
  655. extern Dir* dirstat(char*);
  656. extern Dir* dirfstat(int);
  657. extern int dirwstat(char*, Dir*);
  658. extern int dirfwstat(int, Dir*);
  659. extern int32_t dirread(int, Dir**);
  660. extern void nulldir(Dir*);
  661. extern int32_t dirreadall(int, Dir**);
  662. extern int getpid(void);
  663. extern int getppid(void);
  664. extern void rerrstr(char*, uint);
  665. extern char* sysname(void);
  666. extern void werrstr(char*, ...);
  667. #pragma varargck argpos werrstr 1
  668. /* compiler directives on plan 9 */
  669. #define SET(x) ((x)=0)
  670. #define USED(x) if(x){}else{}
  671. #ifdef __GNUC__
  672. # if __GNUC__ >= 3
  673. # undef USED
  674. # define USED(x) ((void)(x))
  675. # endif
  676. #endif
  677. extern char *argv0;
  678. /* #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ */
  679. #define ARGBEGIN for((argv0?0:(argv0=*argv)),argv++,argc--;\
  680. argv[0] && argv[0][0]=='-' && argv[0][1];\
  681. argc--, argv++) {\
  682. char *_args, *_argt;\
  683. Rune _argc;\
  684. _args = &argv[0][1];\
  685. if(_args[0]=='-' && _args[1]==0){\
  686. argc--; argv++; break;\
  687. }\
  688. _argc = 0;\
  689. while(*_args && (_args += chartorune(&_argc, _args)))\
  690. switch(_argc)
  691. /* #define ARGEND SET(_argt);USED(_argt,_argc,_args);}USED(argv, argc); */
  692. #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
  693. #define ARGF() (_argt=_args, _args="",\
  694. (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
  695. #define EARGF(x) (_argt=_args, _args="",\
  696. (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
  697. #define ARGC() _argc
  698. /* this is used by sbrk and brk, it's a really bad idea to redefine it */
  699. extern char end[];