libc.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * Copyright (C) 2015-2020 Giacomo Tesio <giacomo@tesio.it>
  3. *
  4. * This file is part of the UCB release of Plan 9. It is subject to the license
  5. * terms in the LICENSE file found in the top-level directory of this
  6. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  7. * part of the UCB release of Plan 9, including this file, may be copied,
  8. * modified, propagated, or distributed except according to the terms contained
  9. * in the LICENSE file.
  10. */
  11. #pragma lib "libjehanne.a"
  12. #pragma src "/sys/src/lib/jehanne"
  13. #define JEHANNE_LIBC /* only for native code */
  14. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  15. #define offsetof(s, m) (uintptr_t)(&(((s*)0)->m))
  16. #define assert(x) if(x){}else jehanne__assert(#x)
  17. #define jehanne_getcallerpc() ((uintptr_t)__builtin_return_address(0))
  18. extern void (*_abort)(void);
  19. #define abort() if(_abort){_abort();}else{while(*(int*)0);}
  20. /*
  21. * mem routines
  22. */
  23. extern void* jehanne_memccpy(void*, const void*, int, uint32_t);
  24. extern void* jehanne_memset(void*, int, uint32_t);
  25. extern int jehanne_memcmp(const void*, const void*, uint32_t);
  26. extern void* jehanne_memcpy(void*, const void*, size_t);
  27. extern void* jehanne_memmove(void*, const void*, size_t);
  28. extern void* jehanne_memchr(const void*, int, uint32_t);
  29. /*
  30. * string routines
  31. */
  32. extern char* jehanne_strcat(char*, const char*);
  33. extern char* jehanne_strchr(const char*, int);
  34. extern int jehanne_strcmp(const char*, const char*);
  35. extern char* jehanne_strcpy(char*, const char*);
  36. extern char* jehanne_strecpy(char*, char *, const char*);
  37. extern char* jehanne_strdup(const char*);
  38. extern char* jehanne_strncat(char*, const char*, int32_t);
  39. extern char* jehanne_strncpy(char*, const char*, uint32_t);
  40. extern int jehanne_strncmp(const char*, const char*, int32_t);
  41. extern char* jehanne_strpbrk(const char*, const char*);
  42. extern char* jehanne_strrchr(const char*, int);
  43. extern char* jehanne_strtok(char*, char*);
  44. extern int jehanne_strlen(const char*);
  45. extern int32_t jehanne_strspn(const char*, const char*);
  46. extern int32_t jehanne_strcspn(const char*, const char*);
  47. extern char* jehanne_strstr(const char*, const char*);
  48. extern int jehanne_cistrncmp(const char*, const char*, int);
  49. extern int jehanne_cistrcmp(const char*, const char*);
  50. extern char* jehanne_cistrstr(const char*, const char*);
  51. extern int jehanne_tokenize(char*, char**, int);
  52. enum
  53. {
  54. UTFmax = 4, /* maximum bytes per rune */
  55. Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
  56. Runeself = 0x80, /* rune and UTF sequences are the same (<) */
  57. Runeerror = 0xFFFD, /* decoding error in UTF */
  58. Runemax = 0x10FFFF, /* 21-bit rune */
  59. Runemask = 0x1FFFFF, /* bits used by runes (see grep) */
  60. };
  61. /*
  62. * rune routines
  63. */
  64. extern int jehanne_runetochar(char*, const Rune*);
  65. extern int jehanne_chartorune(Rune*, const char*);
  66. extern int jehanne_runelen(Rune);
  67. extern int jehanne_runenlen(const Rune*, int);
  68. extern int jehanne_fullrune(const char*, int);
  69. extern int jehanne_utflen(const char*);
  70. extern int jehanne_utfnlen(const char*, int32_t);
  71. extern char* jehanne_utfrune(const char*, Rune);
  72. extern char* jehanne_utfrrune(const char*, Rune);
  73. extern char* jehanne_utfutf(const char*, const char*);
  74. extern char* jehanne_utfecpy(char*, char *, const char*);
  75. extern Rune* jehanne_runestrcat(Rune*, const Rune*);
  76. extern Rune* jehanne_runestrchr(const Rune*, Rune);
  77. extern int jehanne_runestrcmp(const Rune*, const Rune*);
  78. extern Rune* jehanne_runestrcpy(Rune*, const Rune*);
  79. extern Rune* jehanne_runestrncpy(Rune*, const Rune*, int32_t);
  80. extern Rune* jehanne_runestrecpy(Rune*, Rune*, const Rune*);
  81. extern Rune* jehanne_runestrdup(const Rune*);
  82. extern Rune* jehanne_runestrncat(Rune*, const Rune*, int32_t);
  83. extern int jehanne_runestrncmp(const Rune*, const Rune*, int32_t);
  84. extern Rune* jehanne_runestrrchr(const Rune*, Rune);
  85. extern int32_t jehanne_runestrlen(const Rune*);
  86. extern Rune* jehanne_runestrstr(const Rune*, const Rune*);
  87. extern Rune jehanne_tolowerrune(Rune);
  88. extern Rune jehanne_totitlerune(Rune);
  89. extern Rune jehanne_toupperrune(Rune);
  90. extern Rune jehanne_tobaserune(Rune);
  91. extern int jehanne_isalpharune(Rune);
  92. extern int jehanne_isbaserune(Rune);
  93. extern int jehanne_isdigitrune(Rune);
  94. extern int jehanne_islowerrune(Rune);
  95. extern int jehanne_isspacerune(Rune);
  96. extern int jehanne_istitlerune(Rune);
  97. extern int jehanne_isupperrune(Rune);
  98. /*
  99. * malloc
  100. */
  101. extern void* jehanne_malloc(size_t);
  102. extern void* jehanne_mallocz(uint32_t, int);
  103. extern void jehanne_free(void*);
  104. extern uint32_t jehanne_msize(void*);
  105. extern void* jehanne_mallocalign(uint32_t, uint32_t, int32_t, uint32_t);
  106. extern void* jehanne_calloc(uint32_t, size_t);
  107. extern void* jehanne_realloc(void*, size_t);
  108. void jehanne_setmalloctag(void*, uintptr_t);
  109. void jehanne_setrealloctag(void*, uintptr_t);
  110. uintptr_t jehanne_getmalloctag(void*);
  111. uintptr_t jehanne_getrealloctag(void*);
  112. void* jehanne_malloctopoolblock(void*);
  113. /*
  114. * print routines
  115. */
  116. typedef struct Fmt Fmt;
  117. struct Fmt{
  118. uint8_t runes; /* output buffer is runes or chars? */
  119. void *start; /* of buffer */
  120. void *to; /* current place in the buffer */
  121. void *stop; /* end of the buffer; overwritten if flush fails */
  122. int (*flush)(Fmt *); /* called when to == stop */
  123. void *farg; /* to make flush a closure */
  124. int nfmt; /* num chars formatted so far */
  125. va_list args; /* args passed to dofmt */
  126. int r; /* % format Rune */
  127. int width;
  128. int prec;
  129. uint32_t flags;
  130. };
  131. enum{
  132. FmtWidth = 1,
  133. FmtLeft = FmtWidth << 1,
  134. FmtPrec = FmtLeft << 1,
  135. FmtSharp = FmtPrec << 1,
  136. FmtSpace = FmtSharp << 1,
  137. FmtSign = FmtSpace << 1,
  138. FmtZero = FmtSign << 1,
  139. FmtUnsigned = FmtZero << 1,
  140. FmtShort = FmtUnsigned << 1,
  141. FmtLong = FmtShort << 1,
  142. FmtVLong = FmtLong << 1,
  143. FmtComma = FmtVLong << 1,
  144. FmtByte = FmtComma << 1,
  145. FmtFlag = FmtByte << 1
  146. };
  147. extern int jehanne_print(const char*, ...);
  148. extern char* jehanne_seprint(char*, char*, const char*, ...);
  149. extern char* jehanne_vseprint(char*, char*, const char*, va_list);
  150. extern int jehanne_snprint(char*, int, const char*, ...);
  151. extern int jehanne_vsnprint(char*, int, const char*, va_list);
  152. extern char* jehanne_smprint(const char*, ...);
  153. extern char* jehanne_vsmprint(const char*, va_list);
  154. extern int jehanne_sprint(char*, const char*, ...);
  155. extern int jehanne_fprint(int, const char*, ...);
  156. extern int jehanne_vfprint(int, const char*, va_list);
  157. extern int jehanne_runesprint(Rune*, const char*, ...);
  158. extern int jehanne_runesnprint(Rune*, int, const char*, ...);
  159. extern int jehanne_runevsnprint(Rune*, int, const char*, va_list);
  160. extern Rune* jehanne_runeseprint(Rune*, Rune*, const char*, ...);
  161. extern Rune* jehanne_runevseprint(Rune*, Rune*, const char*, va_list);
  162. extern Rune* jehanne_runesmprint(const char*, ...);
  163. extern Rune* jehanne_runevsmprint(const char*, va_list);
  164. extern int jehanne_fmtfdinit(Fmt*, int, char*, int);
  165. extern int jehanne_fmtfdflush(Fmt*);
  166. extern int jehanne_fmtstrinit(Fmt*);
  167. extern char* jehanne_fmtstrflush(Fmt*);
  168. extern int jehanne_runefmtstrinit(Fmt*);
  169. extern Rune* jehanne_runefmtstrflush(Fmt*);
  170. extern int jehanne_fmtinstall(int, int (*)(Fmt*));
  171. extern int jehanne_dofmt(Fmt*, const char*);
  172. extern int jehanne_dorfmt(Fmt*, const Rune*);
  173. extern int jehanne_fmtprint(Fmt*, const char*, ...);
  174. extern int jehanne_fmtvprint(Fmt*, const char*, va_list);
  175. extern int jehanne_fmtrune(Fmt*, int);
  176. extern int jehanne_fmtstrcpy(Fmt*, const char*);
  177. extern int jehanne_fmtrunestrcpy(Fmt*, const Rune*);
  178. /*
  179. * error string for %r
  180. * supplied on per os basis, not part of fmt library
  181. */
  182. extern int jehanne_errfmt(Fmt *f);
  183. /*
  184. * quoted strings
  185. */
  186. extern char *jehanne_unquotestrdup(const char*);
  187. extern Rune *jehanne_unquoterunestrdup(const Rune*);
  188. extern char *jehanne_quotestrdup(const char*);
  189. extern Rune *jehanne_quoterunestrdup(const Rune*);
  190. extern int jehanne_quotestrfmt(Fmt*);
  191. extern int jehanne_quoterunestrfmt(Fmt*);
  192. extern void jehanne_quotefmtinstall(void);
  193. extern int (*doquote)(int);
  194. extern int jehanne_needsrcquote(int);
  195. /*
  196. * random number
  197. */
  198. extern void jehanne_srand(int32_t);
  199. extern int jehanne_rand(void);
  200. extern int jehanne_nrand(int);
  201. extern int32_t jehanne_lrand(void);
  202. extern int32_t jehanne_lnrand(int32_t);
  203. extern double jehanne_frand(void);
  204. extern uint32_t jehanne_truerand(void); /* uses /dev/random */
  205. extern uint32_t jehanne_ntruerand(uint32_t); /* uses /dev/random */
  206. /*
  207. * math
  208. */
  209. extern uint32_t jehanne_getfcr(void);
  210. extern void jehanne_setfsr(uint32_t);
  211. extern uint32_t getfsr(void);
  212. extern void jehanne_setfcr(uint32_t);
  213. extern double jehanne_NaN(void);
  214. extern double jehanne_Inf(int);
  215. extern int jehanne_isNaN(double);
  216. extern int jehanne_isInf(double, int);
  217. extern uint32_t jehanne_umuldiv(uint32_t, uint32_t, uint32_t);
  218. extern int32_t jehanne_muldiv(int32_t, int32_t, int32_t);
  219. extern double jehanne_pow(double, double);
  220. extern double jehanne_atan2(double, double);
  221. extern double jehanne_fabs(double);
  222. extern double jehanne_atan(double);
  223. extern double jehanne_log(double);
  224. extern double jehanne_log10(double);
  225. extern double jehanne_exp(double);
  226. extern double jehanne_floor(double);
  227. extern double jehanne_ceil(double);
  228. extern double jehanne_hypot(double, double);
  229. extern double jehanne_sin(double);
  230. extern double jehanne_cos(double);
  231. extern double jehanne_tan(double);
  232. extern double jehanne_asin(double);
  233. extern double jehanne_acos(double);
  234. extern double jehanne_sinh(double);
  235. extern double jehanne_cosh(double);
  236. extern double jehanne_tanh(double);
  237. extern double jehanne_sqrt(double);
  238. extern double jehanne_fmod(double, double);
  239. #define HUGE 3.4028234e38
  240. #define PIO2 1.570796326794896619231e0
  241. #define PI (PIO2+PIO2)
  242. /*
  243. * Time-of-day
  244. */
  245. typedef
  246. struct Tm
  247. {
  248. int sec;
  249. int min;
  250. int hour;
  251. int mday;
  252. int mon;
  253. int year;
  254. int wday;
  255. int yday;
  256. char zone[4];
  257. int tzoff;
  258. } Tm;
  259. extern Tm* jehanne_gmtime(int32_t);
  260. extern Tm* jehanne_localtime(int32_t);
  261. extern char* jehanne_asctime(Tm*);
  262. extern char* jehanne_ctime(int32_t);
  263. extern double jehanne_cputime(void);
  264. extern int32_t jehanne_times(int32_t*);
  265. extern int32_t jehanne_tm2sec(Tm*);
  266. //extern int64_t jehanne_nsec(void);
  267. extern void cycles(uint64_t*); /* 64-bit value of the cycle counter if there is one, 0 if there isn't */
  268. /*
  269. * one-of-a-kind
  270. */
  271. enum
  272. {
  273. PNPROC = 1,
  274. PNGROUP = 2,
  275. };
  276. extern void jehanne__assert(const char*) __attribute__ ((noreturn));
  277. extern int jehanne_abs(int);
  278. extern int jehanne_atexit(void(*)(void));
  279. extern void jehanne_atexitdont(void(*)(void));
  280. extern int jehanne_atnotify(int(*)(void*, char*), int);
  281. extern double jehanne_atof(const char*);
  282. extern int jehanne_atoi(const char*);
  283. extern int32_t jehanne_atol(const char*);
  284. extern int64_t jehanne_atoll(const char*);
  285. extern double jehanne_charstod(int(*)(void*), void*);
  286. extern int jehanne_chdir(const char *dirname);
  287. extern char* jehanne_cleanname(char*);
  288. extern int decrypt(void*, void*, int);
  289. extern int encrypt(void*, void*, int);
  290. extern int jehanne_dec64(uint8_t*, int, const char*, int);
  291. extern int jehanne_enc64(char*, int, const uint8_t*, int);
  292. extern int jehanne_dec32(uint8_t*, int, const char*, int);
  293. extern int jehanne_enc32(char*, int, const uint8_t*, int);
  294. extern int jehanne_dec16(uint8_t*, int, const char*, int);
  295. extern int jehanne_enc16(char*, int, const uint8_t*, int);
  296. extern int jehanne_encodefmt(Fmt*);
  297. extern void jehanne_exits(const char*) __attribute__ ((noreturn));
  298. extern double jehanne_frexp(double, int*);
  299. //extern uintptr_t jehanne_getcallerpc(void);
  300. extern int jehanne_getfields(char*, char**, int, int, const char*);
  301. extern int jehanne_gettokens(char *, char **, int, const char *);
  302. extern char* jehanne_getuser(void);
  303. extern long jehanne_getwd(char*, int);
  304. extern int jehanne_iounit(int);
  305. extern int32_t jehanne_labs(int32_t);
  306. extern double jehanne_ldexp(double, int);
  307. extern void jehanne_longjmp(jmp_buf, int);
  308. extern char* jehanne_mktemp(char*);
  309. extern double jehanne_modf(double, double*);
  310. extern void jehanne_notejmp(void*, jmp_buf, int);
  311. extern unsigned long jehanne_nsec(void);
  312. extern int jehanne_dup(int oldfd, int newfd);
  313. extern int jehanne_ocreate(const char* path, unsigned int omode, unsigned int perm);
  314. extern void jehanne_perror(const char*);
  315. extern int jehanne_pipe(int pipes[2]);
  316. extern int jehanne_postnote(int, int, const char *);
  317. extern double jehanne_pow10(int);
  318. extern void jehanne_qsort(void*, long, int,
  319. int (*)(const void*, const void*));
  320. extern void* jehanne_bsearch(const void* key, const void* base, size_t nmemb, size_t size,
  321. int (*compar)(const void*, const void*));
  322. extern int jehanne_setjmp(jmp_buf);
  323. extern double jehanne_strtod(const char*, const char**);
  324. extern int32_t jehanne_strtol(const char*, char**, int);
  325. extern uint32_t jehanne_strtoul(const char*, char**, int);
  326. extern int64_t jehanne_strtoll(const char*, char**, int);
  327. extern uint64_t jehanne_strtoull(const char*, char**, int);
  328. extern void jehanne_sysfatal(const char*, ...) __attribute__ ((noreturn));
  329. extern void jehanne_syslog(int, const char*, const char*, ...);
  330. extern int32_t jehanne_time(int32_t*);
  331. extern int jehanne_tolower(int);
  332. extern int jehanne_toupper(int);
  333. /*
  334. * atomic
  335. */
  336. int32_t jehanne_ainc(int32_t*);
  337. int32_t jehanne_adec(int32_t*);
  338. #define cas(ptr, oldval, newval) __sync_bool_compare_and_swap(ptr, oldval, newval)
  339. #define casv(ptr, oldval, newval) __sync_val_compare_and_swap(ptr, oldval, newval)
  340. /*
  341. * synchronization
  342. */
  343. typedef
  344. struct Lock {
  345. int32_t key;
  346. int32_t sem;
  347. } Lock;
  348. extern int jehanne_tsemacquire(int* addr, long ms);
  349. extern int jehanne__tas(int*);
  350. extern void jehanne_lock(Lock*);
  351. extern int jehanne_lockt(Lock*, uint32_t);
  352. extern void jehanne_unlock(Lock*);
  353. extern int jehanne_canlock(Lock*);
  354. typedef struct QLp QLp;
  355. struct QLp
  356. {
  357. uint8_t state;
  358. QLp *next;
  359. };
  360. typedef
  361. struct QLock
  362. {
  363. Lock lock;
  364. int locked;
  365. QLp *head;
  366. QLp *tail;
  367. } QLock;
  368. extern void jehanne_qlock(QLock*);
  369. extern int jehanne_qlockt(QLock*, uint32_t);
  370. extern void jehanne_qunlock(QLock*);
  371. extern int jehanne_canqlock(QLock*);
  372. extern void jehanne__qlockinit(void* (*)(void*, void*)); /* called only by the thread library */
  373. typedef
  374. struct RWLock
  375. {
  376. Lock lock;
  377. int _readers; /* number of readers */
  378. int writer; /* number of writers */
  379. QLp *head; /* list of waiting processes */
  380. QLp *tail;
  381. } RWLock;
  382. extern void jehanne_rlock(RWLock*);
  383. extern int jehanne_rlockt(RWLock*, uint32_t);
  384. extern void jehanne_runlock(RWLock*);
  385. extern int jehanne_canrlock(RWLock*);
  386. extern void jehanne_wlock(RWLock*);
  387. extern int jehanne_wlockt(RWLock*, uint32_t);
  388. extern void jehanne_wunlock(RWLock*);
  389. extern int jehanne_canwlock(RWLock*);
  390. typedef
  391. struct Rendez
  392. {
  393. QLock *l;
  394. QLp *head;
  395. QLp *tail;
  396. } Rendez;
  397. extern void jehanne_rsleep(Rendez*); /* unlocks r->l, sleeps, locks r->l again */
  398. extern int jehanne_rsleept(Rendez*, uint32_t); /* unlocks r->l, sleeps (up to ms), locks r->l again (if not timedout) */
  399. extern int jehanne_rwakeup(Rendez*);
  400. extern int jehanne_rwakeupall(Rendez*);
  401. extern void** jehanne_privalloc(void);
  402. /*
  403. * network dialing
  404. */
  405. #define NETPATHLEN 40
  406. extern int jehanne_accept(int, const char*);
  407. extern int jehanne_announce(const char*, char*);
  408. extern int jehanne_dial(const char*, const char*, char*, int*);
  409. extern void jehanne_setnetmtpt(char*, int, const char*);
  410. extern int jehanne_hangup(int);
  411. extern int jehanne_listen(const char*, char*);
  412. extern char* jehanne_netmkaddr(const char*, const char*, const char*);
  413. extern int jehanne_reject(int, const char*, const char*);
  414. /*
  415. * encryption
  416. */
  417. extern int jehanne_pushssl(int, const char*, const char*, const char*, int*);
  418. //extern int jehanne_pushtls(int, const char*, const char*, int, const char*,
  419. // char*);
  420. /*
  421. * network services
  422. */
  423. typedef struct NetConnInfo NetConnInfo;
  424. struct NetConnInfo
  425. {
  426. char *dir; /* connection directory */
  427. char *root; /* network root */
  428. char *spec; /* binding spec */
  429. char *lsys; /* local system */
  430. char *lserv; /* local service */
  431. char *rsys; /* remote system */
  432. char *rserv; /* remote service */
  433. char *laddr; /* local address */
  434. char *raddr; /* remote address */
  435. };
  436. extern NetConnInfo* jehanne_getnetconninfo(const char*, int);
  437. extern void jehanne_freenetconninfo(NetConnInfo*);
  438. /*
  439. * system calls
  440. *
  441. */
  442. #define STATMAX 65535U /* max length of machine-independent stat structure */
  443. #define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */
  444. #define ERRMAX 128 /* max length of error string */
  445. #define MORDER 0x0003 /* mask for bits defining order of mounting */
  446. #define MREPL 0x0000 /* mount replaces object */
  447. #define MBEFORE 0x0001 /* mount goes before others in union directory */
  448. #define MAFTER 0x0002 /* mount goes after others in union directory */
  449. #define MCREATE 0x0004 /* permit creation in mounted directory */
  450. #define MCACHE 0x0010 /* cache some data */
  451. #define MMASK 0x0017 /* all bits on */
  452. /* Open modes: Kernel reserved flags */
  453. #define OSTAT 0x00 /* open for stat/wstat */
  454. #define OREAD 0x01 /* open for read */
  455. #define OWRITE 0x02 /* write */
  456. #define ORDWR (OREAD|OWRITE) /* read and write */
  457. #define OEXEC 0x04 /* execute, == read but check execute permission */
  458. #define OCEXEC 0x08 /* or'ed in, close on exec */
  459. #define ORCLOSE 0x10 /* or'ed in, remove on close */
  460. #define OKMODE 0xff /* least significant byte reserved for kernel use */
  461. /* Open modes: Popular flags among filesystems */
  462. #define OTRUNC 0x0100 /* or'ed in (except for exec), truncate file first */
  463. /* Access modes */
  464. #define AEXIST 0 /* accessible: exists */
  465. #define AREAD 4 /* read ccess */
  466. #define AWRITE 2 /* write access */
  467. #define AEXEC 1 /* execute access */
  468. #define AMASK (AEXIST|AREAD|AWRITE|AEXEC)
  469. /* Segattch */
  470. #define SG_RONLY 0040 /* read only */
  471. #define SG_CEXEC 0100 /* detach on exec */
  472. #define NCONT 0 /* continue after note */
  473. #define NDFLT 1 /* terminate after note */
  474. #define NSAVE 2 /* clear note but hold state */
  475. #define NRSTR 3 /* restore saved state */
  476. /* bits in Qid.type */
  477. #define QTDIR 0x80 /* type bit for directories */
  478. #define QTAPPEND 0x40 /* type bit for append only files */
  479. #define QTEXCL 0x20 /* type bit for exclusive use files */
  480. #define QTMOUNT 0x10 /* type bit for mounted channel */
  481. #define QTAUTH 0x08 /* type bit for authentication file */
  482. #define QTTMP 0x04 /* type bit for not-backed-up file */
  483. #define QTFILE 0x00 /* plain file */
  484. /* bits in Dir.mode */
  485. #define DMDIR 0x80000000 /* mode bit for directories */
  486. #define DMAPPEND 0x40000000 /* mode bit for append only files */
  487. #define DMEXCL 0x20000000 /* mode bit for exclusive use files */
  488. #define DMMOUNT 0x10000000 /* mode bit for mounted channel */
  489. #define DMAUTH 0x08000000 /* mode bit for authentication file */
  490. #define DMTMP 0x04000000 /* mode bit for non-backed-up files */
  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. uint32_t 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. pid_t pid; /* of loved one */
  538. uint32_t time[3]; /* of loved one & descendants */
  539. char msg[];
  540. } Waitmsg;
  541. extern int jehanne_access(const char*, int);
  542. extern int jehanne_awakened(int64_t);
  543. extern int jehanne_brk(void*);
  544. extern int jehanne_execl(const char*, ...);
  545. extern int jehanne_forgivewkp(int64_t);
  546. extern pid_t jehanne_fork(void);
  547. extern int jehanne_pexec(const char* cmd, char* args[]);
  548. extern int32_t jehanne_readn(int, void*, int32_t);
  549. extern void* jehanne_segbrk(uint32_t);
  550. #define sbrk(incr) jehanne_segbrk(incr)
  551. extern void* jehanne_segattach(int, const char*, void*, unsigned long);
  552. extern int jehanne_segdetach(void*);
  553. extern int jehanne_segfree(void*, unsigned long);
  554. extern void jehanne_sleep(unsigned int millisecs);
  555. extern int jehanne_stat(const char*, uint8_t*, int);
  556. extern Waitmsg* jehanne_wait(void);
  557. extern int jehanne_waitpid(void);
  558. extern int jehanne_wstat(const char*, uint8_t*, int);
  559. extern long jehanne_read(int, void*, int32_t);
  560. extern long jehanne_write(int, const void*, int32_t);
  561. extern Dir* jehanne_dirstat(const char*);
  562. extern Dir* jehanne_dirfstat(int);
  563. extern int jehanne_dirwstat(const char*, Dir*);
  564. extern int jehanne_dirfwstat(int, Dir*);
  565. extern int32_t jehanne_dirread(int, Dir**);
  566. extern void jehanne_nulldir(Dir*);
  567. extern int32_t jehanne_dirreadall(int, Dir**);
  568. extern int32_t jehanne_getpid(void);
  569. extern int32_t jehanne_getppid(void);
  570. extern int32_t jehanne_getmainpid(void);
  571. extern void jehanne_rerrstr(char*, uint32_t);
  572. extern char* jehanne_sysname(void);
  573. extern void jehanne_werrstr(const char*, ...);
  574. extern unsigned int jehanne_convM2D(uint8_t*, uint, Dir*, char*);
  575. extern unsigned int jehanne_convD2M(Dir*, uint8_t*, uint);
  576. extern unsigned int jehanne_sizeD2M(Dir*);
  577. extern int jehanne_dirmodefmt(Fmt*);
  578. #ifndef NPRIVATES
  579. # define NPRIVATES 16
  580. #endif
  581. /* compiler directives on plan 9 */
  582. #define SET(x) ((x)=0)
  583. #define USED(x) if(x){}else{}
  584. #ifdef __GNUC__
  585. # if __GNUC__ >= 3
  586. # undef USED
  587. # define USED(x) ((void)(x))
  588. # endif
  589. #endif
  590. extern char *argv0;
  591. /* #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ */
  592. #define ARGBEGIN if(argv0==nil){\
  593. argv0=*argv;\
  594. }\
  595. for(argv++,argc--;\
  596. argv[0] && argv[0][0]=='-' && argv[0][1];\
  597. argc--, argv++) {\
  598. char *_args, *_argt;\
  599. Rune _argc;\
  600. _args = &argv[0][1];\
  601. if(_args[0]=='-' && _args[1]==0){\
  602. argc--; argv++; break;\
  603. }\
  604. _argc = 0;\
  605. while(*_args && (_args += jehanne_chartorune(&_argc, _args)))\
  606. switch(_argc)
  607. /* #define ARGEND SET(_argt);USED(_argt,_argc,_args);}USED(argv, argc); */
  608. #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc);
  609. #define ARGF() (_argt=_args, _args="",\
  610. (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
  611. #define EARGF(x) (_argt=_args, _args="",\
  612. (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), argc = *(int*)0, (char*)0)))
  613. #define ARGC() _argc
  614. /* this is used by segbrk and brk, it's a really bad idea to redefine it */
  615. extern char end[];