libc.h 22 KB

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