libc.h 21 KB

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