libc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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) (usize)(&(((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, u32);
  16. extern void* memset(void*, int, usize);
  17. extern int memcmp(const void*, const void*, u32);
  18. extern void* memcpy(void*, const void*, usize);
  19. extern void* memmove(void*, const void*, usize);
  20. extern void* memchr(const void*, int, u32);
  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 usize strlcat(char *, const char *, usize);
  31. extern usize strlcpy(char *, const char *, usize);
  32. extern char* strncat(char*, const char*, i32);
  33. extern char* strncpy(char*, const char*, u32);
  34. extern int strncmp(const char*, const char*, i32);
  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*, i32);
  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*, i32);
  74. extern Rune* runestrecpy(Rune*, Rune*, const Rune*);
  75. extern Rune* runestrdup(const Rune*);
  76. extern Rune* runestrncat(Rune*, const Rune*, i32);
  77. extern int runestrncmp(const Rune*, const Rune*, i32);
  78. extern Rune* runestrrchr(const Rune*, Rune);
  79. extern i32 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(usize);
  96. extern void* mallocz(u32, int);
  97. extern void free(void*);
  98. extern u32 msize(void*);
  99. extern void* mallocalign(u32, u32, i32, u32);
  100. extern void* calloc(u32, usize);
  101. extern void* realloc(void*, usize);
  102. void setmalloctag(void*, usize);
  103. void setrealloctag(void*, usize);
  104. usize getmalloctag(void*);
  105. usize getrealloctag(void*);
  106. void* malloctopoolblock(void*);
  107. /*
  108. * print routines
  109. */
  110. typedef struct Fmt Fmt;
  111. struct Fmt{
  112. u8 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. u32 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(i32);
  193. extern int rand(void);
  194. extern int nrand(int);
  195. extern i32 lrand(void);
  196. extern i32 lnrand(i32);
  197. extern double frand(void);
  198. extern u32 truerand(void); /* uses /dev/random */
  199. extern u32 ntruerand(u32); /* uses /dev/random */
  200. /*
  201. * math
  202. */
  203. extern u32 getfcr(void);
  204. extern void setfsr(u32);
  205. extern u32 getfsr(void);
  206. extern void setfcr(u32);
  207. extern double NaN(void);
  208. extern double Inf(int);
  209. extern int isNaN(double);
  210. extern int isInf(double, int);
  211. extern u32 umuldiv(u32, u32, u32);
  212. extern i32 muldiv(i32, i32, i32);
  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(i32);
  254. extern Tm* localtime(i32);
  255. extern char* asctime(Tm*);
  256. extern char* ctime(i32);
  257. extern double cputime(void);
  258. extern i32 times(i32*);
  259. extern i32 tm2sec(Tm*);
  260. extern i64 nsec(void);
  261. extern void cycles(u64*); /* 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 i32 atol(const char*);
  278. extern i64 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(u8*, int, const char*, int);
  284. extern int enc64(char*, int, const u8*, int);
  285. extern int dec32(u8*, int, const char*, int);
  286. extern int enc32(char*, int, const u8*, int);
  287. extern int dec16(u8*, int, const char*, int);
  288. extern int enc16(char*, int, const u8*, int);
  289. extern int encodefmt(Fmt*);
  290. extern void exits(const char*);
  291. extern double frexp(double, int*);
  292. extern void getcallstack(usize *, usize);
  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 i32 labs(i32);
  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*, i32, i32,
  311. int (*)(const void*, const void*));
  312. extern int setjmp(jmp_buf) __attribute__((returns_twice));;
  313. extern double strtod(const char*, char**);
  314. extern i32 strtol(const char*, char**, int);
  315. extern u32 strtoul(const char*, char**, int);
  316. extern i64 strtoll(const char*, char**, int);
  317. extern u64 strtoull(const char*, char**, int);
  318. extern void sysfatal(const char*, ...);
  319. extern void syslog(int, const char*, const char*, ...);
  320. extern i32 time(i32*);
  321. extern int tolower(int);
  322. extern int toupper(int);
  323. /*
  324. * atomic
  325. */
  326. i32 ainc(i32*);
  327. i32 adec(i32*);
  328. int cas32(u32*, u32, u32);
  329. int casp(void**, void*, void*);
  330. int casl(u32*, u32, u32);
  331. /*
  332. * synchronization
  333. */
  334. typedef
  335. struct Lock {
  336. i32 key;
  337. i32 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. u64 path;
  500. u32 vers;
  501. u8 type;
  502. } Qid;
  503. typedef
  504. struct Dir {
  505. /* system-modified data */
  506. u16 type; /* server type */
  507. u32 dev; /* server subtype */
  508. /* file data */
  509. Qid qid; /* unique id from server */
  510. u32 mode; /* permissions */
  511. u32 atime; /* last read time */
  512. u32 mtime; /* last write time */
  513. i64 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. u32 time[3]; /* of loved one & descendants */
  525. char *msg;
  526. } Waitmsg;
  527. typedef
  528. struct IOchunk
  529. {
  530. void *addr;
  531. u32 len;
  532. } IOchunk;
  533. extern void _exits(const char*);
  534. extern void abort(void);
  535. extern int access(const char*, int);
  536. extern i64 alarm(u64);
  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, u32);
  543. extern int dup(int, int);
  544. extern char* errno2str(u32 errno);
  545. extern int errstr(char*, u32);
  546. extern int exec(const char*, char* const[]);
  547. extern int execl(const char*, ...);
  548. extern int fork(void);
  549. extern int rfork(int);
  550. extern int fauth(int, const char*);
  551. extern int fstat(int, u8*, int);
  552. extern int fwstat(int, u8*, int);
  553. extern int fversion(int, int, char*, int);
  554. extern int mount(int, int, const char*, int, const char*, int);
  555. extern int unmount(const char*, const char*);
  556. extern int noted(int);
  557. extern int notify(void(*)(void*, char*));
  558. extern int open(const char*, int);
  559. extern int fd2path(int, char*, int);
  560. // extern int fdflush(int);
  561. extern int pipe(int*);
  562. extern i32 pread(int, void*, i32, i64);
  563. extern i32 read(int, void*, i32);
  564. extern i32 preadv(int, IOchunk*, int, i64);
  565. extern i32 pwrite(int, const void*, i32, i64);
  566. extern i32 write(int, const void*, i32);
  567. extern i32 pwritev(int, IOchunk*, int, i64);
  568. extern i32 r0(void);
  569. extern i32 readn(int, void*, i32);
  570. extern i32 readv(int, IOchunk*, int);
  571. extern int remove(const char*);
  572. extern void* sbrk(u32);
  573. extern i32 oseek(int, i32, int);
  574. extern i64 seek(int, i64, int);
  575. extern void* segattach(int, const char*, void*, u32);
  576. extern void* segbrk(void*, void*);
  577. extern int segdetach(void*);
  578. extern int segflush(void*, u32);
  579. extern int segfree(void*, u32);
  580. extern int semacquire(i32*, int);
  581. extern i32 semrelease(i32*, i32);
  582. extern int sleep(i32);
  583. extern int stat(const char*, u8*, int);
  584. extern int tsemacquire(i32*, u64);
  585. extern Waitmsg* wait(void);
  586. extern int waitpid(void);
  587. extern i32 writev(int, IOchunk*, int);
  588. extern int wstat(const char*, u8*, int);
  589. extern void* rendezvous(void*, void*);
  590. extern Dir* dirstat(const char*);
  591. extern Dir* dirfstat(int);
  592. extern int dirwstat(const char*, Dir*);
  593. extern int dirfwstat(int, Dir*);
  594. extern i32 dirread(int, Dir**);
  595. extern void nulldir(Dir*);
  596. extern i32 dirreadall(int, Dir**);
  597. extern int getpid(void);
  598. extern int getppid(void);
  599. extern void rerrstr(char*, u32);
  600. extern char* sysname(void);
  601. extern void werrstr(const char*, ...);
  602. /* compiler directives on plan 9 */
  603. #define SET(x) ((x)=0)
  604. #define USED(x) ((void)(x))
  605. extern char *argv0;
  606. /* #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ */
  607. #define ARGBEGIN for((argv0?0:(argv0=*argv)),argv++,argc--;\
  608. argv[0] && argv[0][0]=='-' && argv[0][1];\
  609. argc--, argv++) {\
  610. char *_args, *_argt;\
  611. Rune _argc;\
  612. _args = &argv[0][1];\
  613. if(_args[0]=='-' && _args[1]==0){\
  614. argc--; argv++; break;\
  615. }\
  616. _argc = 0;\
  617. while(*_args && (_args += chartorune(&_argc, _args)))\
  618. switch(_argc)
  619. /* #define ARGEND SET(_argt);USED(_argt,_argc,_args);}USED(argv, argc); */
  620. #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
  621. #define ARGF() (_argt=_args, _args="",\
  622. (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
  623. #define EARGF(x) (_argt=_args, _args="",\
  624. (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
  625. #define ARGC() _argc
  626. /* this is used by sbrk and brk, it's a really bad idea to redefine it */
  627. extern char end[];
  628. void* reallocarray(void *base, usize nel, usize size);
  629. typedef struct PSlice PSlice;
  630. struct PSlice {
  631. void **ptrs;
  632. usize len;
  633. usize capacity;
  634. };
  635. void psliceinit(PSlice *slice);
  636. void psliceclear(PSlice *slice);
  637. void *psliceget(PSlice *slice, usize i);
  638. int psliceput(PSlice *slice, usize i, void *p);
  639. int pslicedel(PSlice *slice, usize i);
  640. void psliceappend(PSlice *s, void *p);
  641. usize pslicelen(PSlice *slice);
  642. void **pslicefinalize(PSlice *slice);
  643. void pslicedestroy(PSlice *slice);
  644. /* configstring functions. These are from riscv but the idea is so good
  645. * it might have other uses. */
  646. void query_mem(const char *config_string, usize *base, usize *size);
  647. void query_rtc(const char *config_string, usize *mtime);
  648. int query_uint(const char *configstring, char *name, usize *m);