libc.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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(char*);
  41. extern int32_t strspn(char*, char*);
  42. extern int32_t strcspn(char*, char*);
  43. extern char* strstr(char*, char*);
  44. extern int cistrncmp(char*, char*, int);
  45. extern int cistrcmp(char*, char*);
  46. extern char* cistrstr(char*, 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*, Rune*);
  61. extern int chartorune(Rune*, char*);
  62. extern int runelen(int32_t);
  63. extern int runenlen(Rune*, int);
  64. extern int fullrune(char*, int);
  65. extern int utflen(char*);
  66. extern int utfnlen(char*, int32_t);
  67. extern char* utfrune(char*, int32_t);
  68. extern char* utfrrune(char*, int32_t);
  69. extern char* utfutf(char*, char*);
  70. extern char* utfecpy(char*, char*, char*);
  71. extern Rune* runestrcat(Rune*, Rune*);
  72. extern Rune* runestrchr(Rune*, Rune);
  73. extern int runestrcmp(Rune*, Rune*);
  74. extern Rune* runestrcpy(Rune*, Rune*);
  75. extern Rune* runestrncpy(Rune*, Rune*, int32_t);
  76. extern Rune* runestrecpy(Rune*, Rune*, Rune*);
  77. extern Rune* runestrdup(Rune*);
  78. extern Rune* runestrncat(Rune*, Rune*, int32_t);
  79. extern int runestrncmp(Rune*, Rune*, int32_t);
  80. extern Rune* runestrrchr(Rune*, Rune);
  81. extern int32_t runestrlen(Rune*);
  82. extern Rune* runestrstr(Rune*, 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(char*, ...);
  144. extern char* seprint(char*, char*, char*, ...);
  145. extern char* vseprint(char*, char*, char*, va_list);
  146. extern int snprint(char*, int, char*, ...);
  147. extern int vsnprint(char*, int, char*, va_list);
  148. extern char* smprint(char*, ...);
  149. extern char* vsmprint(char*, va_list);
  150. extern int sprint(char*, char*, ...);
  151. extern int fprint(int, char*, ...);
  152. extern int vfprint(int, char*, va_list);
  153. extern int runesprint(Rune*, char*, ...);
  154. extern int runesnprint(Rune*, int, char*, ...);
  155. extern int runevsnprint(Rune*, int, char*, va_list);
  156. extern Rune* runeseprint(Rune*, Rune*, char*, ...);
  157. extern Rune* runevseprint(Rune*, Rune*, char*, va_list);
  158. extern Rune* runesmprint(char*, ...);
  159. extern Rune* runevsmprint(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*, char*);
  168. extern int dorfmt(Fmt*, Rune*);
  169. extern int fmtprint(Fmt*, char*, ...);
  170. extern int fmtvprint(Fmt*, char*, va_list);
  171. extern int fmtrune(Fmt*, int);
  172. extern int fmtstrcpy(Fmt*, char*);
  173. extern int fmtrunestrcpy(Fmt*, 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(char*);
  183. extern Rune *unquoterunestrdup(Rune*);
  184. extern char *quotestrdup(char*);
  185. extern Rune *quoterunestrdup(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(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(char*);
  278. extern int atoi(char*);
  279. extern int32_t atol(char*);
  280. extern int64_t atoll(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, char*, int);
  286. extern int enc64(char*, int, uint8_t*, int);
  287. extern int dec32(uint8_t*, int, char*, int);
  288. extern int enc32(char*, int, uint8_t*, int);
  289. extern int dec16(uint8_t*, int, char*, int);
  290. extern int enc16(char*, int, uint8_t*, int);
  291. extern int encodefmt(Fmt*);
  292. extern void exits(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 int netcrypt(void*, void*);
  308. extern void notejmp(void*, jmp_buf, int);
  309. extern void perror(const char*);
  310. extern int postnote(int, int, char *);
  311. extern double pow10(int);
  312. extern int putenv(char*, char*);
  313. extern void qsort(void*, int32_t, int32_t,
  314. int (*)(const void*, const void*));
  315. extern int setjmp(jmp_buf) __attribute__((returns_twice));;
  316. extern double strtod(const char*, char**);
  317. extern int32_t strtol(const char*, char**, int);
  318. extern uint32_t strtoul(const char*, char**, int);
  319. extern int64_t strtoll(const char*, char**, int);
  320. extern uint64_t strtoull(const char*, char**, int);
  321. extern void sysfatal(const char*, ...);
  322. extern void syslog(int, const char*, const char*, ...);
  323. extern int32_t time(int32_t*);
  324. extern int tolower(int);
  325. extern int toupper(int);
  326. /*
  327. * profiling
  328. */
  329. enum {
  330. Profoff, /* No profiling */
  331. Profuser, /* Measure user time only (default) */
  332. Profkernel, /* Measure user + kernel time */
  333. Proftime, /* Measure total time */
  334. Profsample, /* Use clock interrupt to sample (default when there is no cycle counter) */
  335. }; /* what */
  336. extern void prof(void (*fn)(void*), void *arg, int entries, int what);
  337. /*
  338. * atomic
  339. */
  340. int32_t ainc(int32_t*);
  341. int32_t adec(int32_t*);
  342. int cas32(uint32_t*, uint32_t, uint32_t);
  343. int casp(void**, void*, void*);
  344. int casl(uint32_t*, uint32_t, uint32_t);
  345. /*
  346. * synchronization
  347. */
  348. typedef
  349. struct Lock {
  350. int32_t key;
  351. int32_t sem;
  352. } Lock;
  353. extern int _tas(int*);
  354. extern int _tas32(int*);
  355. extern void lock(Lock*);
  356. extern void unlock(Lock*);
  357. extern int canlock(Lock*);
  358. typedef struct QLp QLp;
  359. struct QLp
  360. {
  361. int inuse;
  362. QLp *next;
  363. char state;
  364. };
  365. typedef
  366. struct QLock
  367. {
  368. Lock lock;
  369. int locked;
  370. QLp *head;
  371. QLp *tail;
  372. } QLock;
  373. extern void qlock(QLock*);
  374. extern void qunlock(QLock*);
  375. extern int canqlock(QLock*);
  376. extern void _qlockinit(void* (*)(void*, void*)); /* called only by the thread library */
  377. typedef
  378. struct RWLock
  379. {
  380. Lock lock;
  381. int _readers; /* number of readers */
  382. int writer; /* number of writers */
  383. QLp *head; /* list of waiting processes */
  384. QLp *tail;
  385. } RWLock;
  386. extern void rlock(RWLock*);
  387. extern void runlock(RWLock*);
  388. extern int canrlock(RWLock*);
  389. extern void wlock(RWLock*);
  390. extern void wunlock(RWLock*);
  391. extern int canwlock(RWLock*);
  392. typedef
  393. struct Rendez
  394. {
  395. QLock *l;
  396. QLp *head;
  397. QLp *tail;
  398. } Rendez;
  399. extern void rsleep(Rendez*); /* unlocks r->l, sleeps, locks r->l again */
  400. extern int rwakeup(Rendez*);
  401. extern int rwakeupall(Rendez*);
  402. extern void** privalloc(void);
  403. extern void privfree(void**);
  404. /*
  405. * network dialing
  406. */
  407. #define NETPATHLEN 40
  408. extern int accept(int, char*);
  409. extern int announce(char*, char*);
  410. extern int dial(char*, char*, char*, int*);
  411. extern void setnetmtpt(char*, int, char*);
  412. extern int hangup(int);
  413. extern int listen(char*, char*);
  414. extern char* netmkaddr(char*, char*, char*);
  415. extern int reject(int, char*, char*);
  416. /*
  417. * encryption
  418. */
  419. extern int pushssl(int, char*, char*, char*, int*);
  420. extern int pushtls(int, char*, char*, int, char*,
  421. char*);
  422. /*
  423. * network services
  424. */
  425. typedef struct NetConnInfo NetConnInfo;
  426. struct NetConnInfo
  427. {
  428. char *dir; /* connection directory */
  429. char *root; /* network root */
  430. char *spec; /* binding spec */
  431. char *lsys; /* local system */
  432. char *lserv; /* local service */
  433. char *rsys; /* remote system */
  434. char *rserv; /* remote service */
  435. char *laddr; /* local address */
  436. char *raddr; /* remote address */
  437. };
  438. extern NetConnInfo* getnetconninfo(char*, int);
  439. extern void freenetconninfo(NetConnInfo*);
  440. /*
  441. * system calls
  442. *
  443. */
  444. #define STATMAX 65535U /* max length of machine-independent stat structure */
  445. #define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */
  446. #define ERRMAX 128 /* max length of error string */
  447. #define MORDER 0x0003 /* mask for bits defining order of mounting */
  448. #define MREPL 0x0000 /* mount replaces object */
  449. #define MBEFORE 0x0001 /* mount goes before others in union directory */
  450. #define MAFTER 0x0002 /* mount goes after others in union directory */
  451. #define MCREATE 0x0004 /* permit creation in mounted directory */
  452. #define MCACHE 0x0010 /* cache some data */
  453. #define MMASK 0x0017 /* all bits on */
  454. #define OREAD 0 /* open for read */
  455. #define OWRITE 1 /* write */
  456. #define ORDWR 2 /* read and write */
  457. #define OEXEC 3 /* execute, == read but check execute permission */
  458. #define OTRUNC 16 /* or'ed in (except for exec), truncate file first */
  459. #define OCEXEC 32 /* or'ed in, close on exec */
  460. #define ORCLOSE 64 /* or'ed in, remove on close */
  461. #define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */
  462. // #define OBEHIND 0x2000 /* use write behind for writes [for 9n] */
  463. #define AEXIST 0 /* accessible: exists */
  464. #define AEXEC 1 /* execute access */
  465. #define AWRITE 2 /* write access */
  466. #define AREAD 4 /* read access */
  467. /* Segattch */
  468. #define SG_RONLY 0040 /* read only */
  469. #define SG_CEXEC 0100 /* detach on exec */
  470. #define NCONT 0 /* continue after note */
  471. #define NDFLT 1 /* terminate after note */
  472. #define NSAVE 2 /* clear note but hold state */
  473. #define NRSTR 3 /* restore saved state */
  474. /* bits in Qid.type */
  475. #define QTDIR 0x80 /* type bit for directories */
  476. #define QTAPPEND 0x40 /* type bit for append only files */
  477. #define QTEXCL 0x20 /* type bit for exclusive use files */
  478. #define QTMOUNT 0x10 /* type bit for mounted channel */
  479. #define QTAUTH 0x08 /* type bit for authentication file */
  480. #define QTTMP 0x04 /* type bit for not-backed-up file */
  481. #define QTSYMLINK 0x02 /* type bit for symlink */
  482. #define QTFILE 0x00 /* plain file */
  483. /* bits in Dir.mode */
  484. #define DMDIR 0x80000000 /* mode bit for directories */
  485. #define DMAPPEND 0x40000000 /* mode bit for append only files */
  486. #define DMEXCL 0x20000000 /* mode bit for exclusive use files */
  487. #define DMMOUNT 0x10000000 /* mode bit for mounted channel */
  488. #define DMAUTH 0x08000000 /* mode bit for authentication file */
  489. #define DMTMP 0x04000000 /* mode bit for non-backed-up files */
  490. #define DMSYMLINK 0x02000000 /* mode bit for symlnk */
  491. #define DMREAD 0x4 /* mode bit for read permission */
  492. #define DMWRITE 0x2 /* mode bit for write permission */
  493. #define DMEXEC 0x1 /* mode bit for execute permission */
  494. /* rfork */
  495. enum
  496. {
  497. RFNAMEG = (1<<0),
  498. RFENVG = (1<<1),
  499. RFFDG = (1<<2),
  500. RFNOTEG = (1<<3),
  501. RFPROC = (1<<4),
  502. RFMEM = (1<<5),
  503. RFNOWAIT = (1<<6),
  504. RFCNAMEG = (1<<10),
  505. RFCENVG = (1<<11),
  506. RFCFDG = (1<<12),
  507. RFREND = (1<<13),
  508. RFNOMNT = (1<<14)
  509. };
  510. typedef
  511. struct Qid
  512. {
  513. uint64_t path;
  514. uint32_t vers;
  515. uint8_t type;
  516. } Qid;
  517. typedef
  518. struct Dir {
  519. /* system-modified data */
  520. uint16_t type; /* server type */
  521. uint dev; /* server subtype */
  522. /* file data */
  523. Qid qid; /* unique id from server */
  524. uint32_t mode; /* permissions */
  525. uint32_t atime; /* last read time */
  526. uint32_t mtime; /* last write time */
  527. int64_t length; /* file length */
  528. char *name; /* last element of path */
  529. char *uid; /* owner name */
  530. char *gid; /* group name */
  531. char *muid; /* last modifier name */
  532. } Dir;
  533. /* keep /sys/src/ape/lib/ap/plan9/sys9.h in sync with this -rsc */
  534. typedef
  535. struct Waitmsg
  536. {
  537. int pid; /* of loved one */
  538. uint32_t time[3]; /* of loved one & descendants */
  539. char *msg;
  540. } Waitmsg;
  541. typedef
  542. struct IOchunk
  543. {
  544. void *addr;
  545. uint32_t len;
  546. } IOchunk;
  547. extern void _exits(char*);
  548. extern void abort(void);
  549. extern int access(char*, int);
  550. extern int32_t alarm(uint32_t);
  551. extern int await(char*, int);
  552. extern int bind(char*, char*, int);
  553. extern int brk(void*);
  554. extern int chdir(char*);
  555. extern int close(int);
  556. extern int create(char*, int, uint32_t);
  557. extern int dup(int, int);
  558. extern int errstr(char*, uint);
  559. extern int exec(char*, char*[]);
  560. extern int execl(char*, ...);
  561. extern int fork(void);
  562. extern int rfork(int);
  563. extern int fauth(int, char*);
  564. extern int fstat(int, uint8_t*, int);
  565. extern int fwstat(int, uint8_t*, int);
  566. extern int fversion(int, int, char*, int);
  567. extern int mount(int, int, char*, int, char*, int);
  568. extern int unmount(char*, char*);
  569. extern int noted(int);
  570. extern int notify(void(*)(void*, char*));
  571. extern int open(char*, int);
  572. extern int fd2path(int, char*, int);
  573. // extern int fdflush(int);
  574. extern int pipe(int*);
  575. extern int32_t pread(int, void*, int32_t, int64_t);
  576. extern int32_t preadv(int, IOchunk*, int, int64_t);
  577. extern int32_t pwrite(int, void*, int32_t, int64_t);
  578. extern int32_t pwritev(int, IOchunk*, int, int64_t);
  579. extern int32_t r0(void);
  580. extern int32_t read(int, void*, int32_t);
  581. extern int32_t readn(int, void*, int32_t);
  582. extern int32_t readv(int, IOchunk*, int);
  583. extern int remove(char*);
  584. extern void* sbrk(uint32_t);
  585. extern int32_t oseek(int, int32_t, int);
  586. extern int64_t seek(int, int64_t, int);
  587. extern void* segattach(int, char*, void*, uint32_t);
  588. extern void* segbrk(void*, void*);
  589. extern int segdetach(void*);
  590. extern int segflush(void*, uint32_t);
  591. extern int segfree(void*, uint32_t);
  592. extern int semacquire(int32_t*, int);
  593. extern int32_t semrelease(int32_t*, int32_t);
  594. extern int sleep(int32_t);
  595. extern int stat(const char*, uint8_t*, int);
  596. extern int tsemacquire(int32_t*, uint64_t);
  597. extern Waitmsg* wait(void);
  598. extern int waitpid(void);
  599. extern int32_t write(int, void*, int32_t);
  600. extern int32_t writev(int, IOchunk*, int);
  601. extern int wstat(char*, uint8_t*, int);
  602. extern void* rendezvous(void*, void*);
  603. extern Dir* dirstat(char*);
  604. extern Dir* dirfstat(int);
  605. extern int dirwstat(char*, Dir*);
  606. extern int dirfwstat(int, Dir*);
  607. extern int32_t dirread(int, Dir**);
  608. extern void nulldir(Dir*);
  609. extern int32_t dirreadall(int, Dir**);
  610. extern int getpid(void);
  611. extern int getppid(void);
  612. extern void rerrstr(char*, uint);
  613. extern char* sysname(void);
  614. extern void werrstr(const char*, ...);
  615. /* compiler directives on plan 9 */
  616. #define SET(x) ((x)=0)
  617. #define USED(x) if(x){}else{}
  618. #ifdef __GNUC__
  619. # if __GNUC__ >= 3
  620. # undef USED
  621. # define USED(x) ((void)(x))
  622. # endif
  623. #endif
  624. extern char *argv0;
  625. /* #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ */
  626. #define ARGBEGIN for((argv0?0:(argv0=*argv)),argv++,argc--;\
  627. argv[0] && argv[0][0]=='-' && argv[0][1];\
  628. argc--, argv++) {\
  629. char *_args, *_argt;\
  630. Rune _argc;\
  631. _args = &argv[0][1];\
  632. if(_args[0]=='-' && _args[1]==0){\
  633. argc--; argv++; break;\
  634. }\
  635. _argc = 0;\
  636. while(*_args && (_args += chartorune(&_argc, _args)))\
  637. switch(_argc)
  638. /* #define ARGEND SET(_argt);USED(_argt,_argc,_args);}USED(argv, argc); */
  639. #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
  640. #define ARGF() (_argt=_args, _args="",\
  641. (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
  642. #define EARGF(x) (_argt=_args, _args="",\
  643. (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), argc = *(volatile int*)0, (char*)0)))
  644. #define ARGC() _argc
  645. /* this is used by sbrk and brk, it's a really bad idea to redefine it */
  646. extern char end[];
  647. void* reallocarray(void *base, size_t nel, size_t size);
  648. typedef struct PSlice PSlice;
  649. struct PSlice {
  650. void **ptrs;
  651. size_t len;
  652. size_t capacity;
  653. };
  654. void psliceinit(PSlice *slice);
  655. void psliceclear(PSlice *slice);
  656. void *psliceget(PSlice *slice, size_t i);
  657. int psliceput(PSlice *slice, size_t i, void *p);
  658. int pslicedel(PSlice *slice, size_t i);
  659. void psliceappend(PSlice *s, void *p);
  660. size_t pslicelen(PSlice *slice);
  661. void **pslicefinalize(PSlice *slice);
  662. void pslicedestroy(PSlice *slice);
  663. /* configstring functions. These are from riscv but the idea is so good
  664. * it might have other uses. */
  665. void query_mem(const char *config_string, uintptr_t *base, size_t *size);
  666. void query_rtc(const char *config_string, uintptr_t *mtime);
  667. int query_uint(const char *configstring, char *name, uintptr_t *m);