libc.h 21 KB

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