lib9.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Based on FreeBSD lib9.h
  3. * Copyright © 1998, 1999 Lucent Technologies Inc. All rights reserved.
  4. * Revisions Copyright © 1999, 2002 Vita Nuova Limited. All rights reserved.
  5. * Revisions Copyright © 2002, 2003 Corpus Callosum Corporation. All rights reserved.
  6. */
  7. /* define _BSD_SOURCE to use ISO C, POSIX, and 4.4BSD things. */
  8. #ifndef _BSD_SOURCE
  9. #define _BSD_SOURCE
  10. #endif
  11. #define _LARGEFILE64_SOURCE 1
  12. #define _FILE_OFFSET_BITS 64
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. // #include "math.h"
  21. #include <fcntl.h>
  22. #include <setjmp.h>
  23. #include <float.h>
  24. #include <time.h>
  25. #define nil ((void*)0)
  26. // typedef unsigned short ushort;
  27. typedef unsigned char uchar;
  28. typedef unsigned long ulong;
  29. // typedef unsigned int uint;
  30. typedef signed char schar;
  31. typedef long long vlong;
  32. typedef unsigned long long uvlong;
  33. typedef ushort Rune;
  34. typedef unsigned int u32int;
  35. typedef uvlong u64int;
  36. typedef size_t usize;
  37. typedef unsigned int mpdigit; /* for /sys/include/mp.h */
  38. typedef unsigned short u16int;
  39. typedef unsigned char u8int;
  40. typedef unsigned long uintptr;
  41. /* handle conflicts with host os libs */
  42. #define getwd infgetwd
  43. #define scalb infscalb
  44. #define div infdiv
  45. #define panic infpanic
  46. #define rint infrint
  47. #define rcmd infrcmd
  48. #undef isnan
  49. #define pow10 infpow10
  50. #ifndef EMU
  51. typedef struct Proc Proc;
  52. #endif
  53. /*
  54. * math module dtoa
  55. */
  56. #include <machine/endian.h>
  57. #define __BIG_ENDIAN
  58. #define USED(x) if(x){}else{}
  59. #define SET(x)
  60. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  61. #undef offsetof
  62. #undef assert
  63. #define offsetof(s, m) (ulong)(&(((s*)0)->m))
  64. #define assert(x) if(x){}else _assert("x")
  65. /*
  66. * mem and string routines are declared by ANSI/POSIX files above
  67. */
  68. extern char* strecpy(char*, char*, char*);
  69. extern char* strdup(const char*);
  70. extern int cistrncmp(char*, char*, int);
  71. extern int cistrcmp(char*, char*);
  72. extern char* cistrstr(char*, char*);
  73. extern int tokenize(char*, char**, int);
  74. extern vlong strtoll(const char*, char**, int);
  75. #define qsort infqsort
  76. extern void qsort(void*, long, long, int (*)(void*, void*));
  77. enum
  78. {
  79. UTFmax = 3, /* maximum bytes per rune */
  80. Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
  81. Runeself = 0x80, /* rune and UTF sequences are the same (<) */
  82. Runeerror = 0x80 /* decoding error in UTF */
  83. };
  84. /*
  85. * rune routines
  86. */
  87. extern int runetochar(char*, Rune*);
  88. extern int chartorune(Rune*, char*);
  89. extern int runelen(long);
  90. extern int runenlen(Rune*, int);
  91. extern int fullrune(char*, int);
  92. extern int utflen(char*);
  93. extern int utfnlen(char*, long);
  94. extern char* utfrune(char*, long);
  95. extern char* utfrrune(char*, long);
  96. extern char* utfutf(char*, char*);
  97. extern char* utfecpy(char*, char*, char*);
  98. extern Rune* runestrcat(Rune*, Rune*);
  99. extern Rune* runestrchr(Rune*, Rune);
  100. extern int runestrcmp(Rune*, Rune*);
  101. extern Rune* runestrcpy(Rune*, Rune*);
  102. extern Rune* runestrncpy(Rune*, Rune*, long);
  103. extern Rune* runestrecpy(Rune*, Rune*, Rune*);
  104. extern Rune* runestrdup(Rune*);
  105. extern Rune* runestrncat(Rune*, Rune*, long);
  106. extern int runestrncmp(Rune*, Rune*, long);
  107. extern Rune* runestrrchr(Rune*, Rune);
  108. extern long runestrlen(Rune*);
  109. extern Rune* runestrstr(Rune*, Rune*);
  110. extern Rune tolowerrune(Rune);
  111. extern Rune totitlerune(Rune);
  112. extern Rune toupperrune(Rune);
  113. extern int isalpharune(Rune);
  114. extern int islowerrune(Rune);
  115. extern int isspacerune(Rune);
  116. extern int istitlerune(Rune);
  117. extern int isupperrune(Rune);
  118. /*
  119. * malloc
  120. */
  121. extern void* malloc(size_t);
  122. extern void* mallocz(ulong, int);
  123. extern void free(void*);
  124. extern ulong msize(void*);
  125. extern void* calloc(size_t, size_t);
  126. extern void* realloc(void*, size_t);
  127. extern void setmalloctag(void*, ulong);
  128. extern void setrealloctag(void*, ulong);
  129. extern ulong getmalloctag(void*);
  130. extern ulong getrealloctag(void*);
  131. extern void* malloctopoolblock(void*);
  132. /*
  133. * print routines
  134. */
  135. typedef struct Fmt Fmt;
  136. struct Fmt{
  137. uchar runes; /* output buffer is runes or chars? */
  138. void *start; /* of buffer */
  139. void *to; /* current place in the buffer */
  140. void *stop; /* end of the buffer; overwritten if flush fails */
  141. int (*flush)(Fmt *); /* called when to == stop */
  142. void *farg; /* to make flush a closure */
  143. int nfmt; /* num chars formatted so far */
  144. va_list args; /* args passed to dofmt */
  145. int r; /* % format Rune */
  146. int width;
  147. int prec;
  148. ulong flags;
  149. };
  150. enum{
  151. FmtWidth = 1,
  152. FmtLeft = FmtWidth << 1,
  153. FmtPrec = FmtLeft << 1,
  154. FmtSharp = FmtPrec << 1,
  155. FmtSpace = FmtSharp << 1,
  156. FmtSign = FmtSpace << 1,
  157. FmtZero = FmtSign << 1,
  158. FmtUnsigned = FmtZero << 1,
  159. FmtShort = FmtUnsigned << 1,
  160. FmtLong = FmtShort << 1,
  161. FmtVLong = FmtLong << 1,
  162. FmtComma = FmtVLong << 1,
  163. FmtByte = FmtComma << 1,
  164. FmtFlag = FmtByte << 1
  165. };
  166. extern int print(char*, ...);
  167. extern char* seprint(char*, char*, char*, ...);
  168. extern char* vseprint(char*, char*, char*, va_list);
  169. extern int snprint(char*, int, char*, ...);
  170. extern int vsnprint(char*, int, char*, va_list);
  171. extern char* smprint(char*, ...);
  172. extern char* vsmprint(char*, va_list);
  173. extern int sprint(char*, char*, ...);
  174. extern int fprint(int, char*, ...);
  175. extern int vfprint(int, char*, va_list);
  176. extern int runesprint(Rune*, char*, ...);
  177. extern int runesnprint(Rune*, int, char*, ...);
  178. extern int runevsnprint(Rune*, int, char*, va_list);
  179. extern Rune* runeseprint(Rune*, Rune*, char*, ...);
  180. extern Rune* runevseprint(Rune*, Rune*, char*, va_list);
  181. extern Rune* runesmprint(char*, ...);
  182. extern Rune* runevsmprint(char*, va_list);
  183. extern int fmtfdinit(Fmt*, int, char*, int);
  184. extern int fmtfdflush(Fmt*);
  185. extern int fmtstrinit(Fmt*);
  186. extern char* fmtstrflush(Fmt*);
  187. extern int runefmtstrinit(Fmt*);
  188. extern Rune* runefmtstrflush(Fmt*);
  189. extern int fmtinstall(int, int (*)(Fmt*));
  190. extern int dofmt(Fmt*, char*);
  191. extern int dorfmt(Fmt*, Rune*);
  192. extern int fmtprint(Fmt*, char*, ...);
  193. extern int fmtvprint(Fmt*, char*, va_list);
  194. extern int fmtrune(Fmt*, int);
  195. extern int fmtstrcpy(Fmt*, char*);
  196. extern int fmtrunestrcpy(Fmt*, Rune*);
  197. /*
  198. * error string for %r
  199. * supplied on per os basis, not part of fmt library
  200. */
  201. extern int errfmt(Fmt *f);
  202. /*
  203. * quoted strings
  204. */
  205. extern char *unquotestrdup(char*);
  206. extern Rune *unquoterunestrdup(Rune*);
  207. extern char *quotestrdup(char*);
  208. extern Rune *quoterunestrdup(Rune*);
  209. extern int quotestrfmt(Fmt*);
  210. extern int quoterunestrfmt(Fmt*);
  211. extern void quotefmtinstall(void);
  212. extern int (*doquote)(int);
  213. /*
  214. * random number
  215. */
  216. extern int nrand(int);
  217. extern ulong truerand(void);
  218. extern ulong ntruerand(ulong);
  219. /*
  220. * math
  221. */
  222. extern int isNaN(double);
  223. extern int isInf(double, int);
  224. extern double pow(double, double);
  225. /*
  226. * Time-of-day
  227. */
  228. typedef struct Tm Tm;
  229. struct Tm {
  230. int sec;
  231. int min;
  232. int hour;
  233. int mday;
  234. int mon;
  235. int year;
  236. int wday;
  237. int yday;
  238. char zone[4];
  239. int tzoff;
  240. };
  241. extern vlong osnsec(void);
  242. #define nsec osnsec
  243. /*
  244. * one-of-a-kind
  245. */
  246. extern void _assert(char*);
  247. extern double charstod(int(*)(void*), void*);
  248. extern char* cleanname(char*);
  249. extern double frexp(double, int*);
  250. extern ulong getcallerpc(void*);
  251. extern int getfields(char*, char**, int, int, char*);
  252. extern char* getuser(void);
  253. extern char* getwd(char*, int);
  254. extern double ipow10(int);
  255. extern double ldexp(double, int);
  256. extern double modf(double, double*);
  257. extern void perror(const char*);
  258. extern double pow10(int);
  259. extern uvlong strtoull(const char*, char**, int);
  260. extern void sysfatal(char*, ...);
  261. extern int dec64(uchar*, int, char*, int);
  262. extern int enc64(char*, int, uchar*, int);
  263. extern int dec32(uchar*, int, char*, int);
  264. extern int enc32(char*, int, uchar*, int);
  265. extern int dec16(uchar*, int, char*, int);
  266. extern int enc16(char*, int, uchar*, int);
  267. extern int encodefmt(Fmt*);
  268. /*
  269. * synchronization
  270. */
  271. typedef
  272. struct Lock {
  273. int val;
  274. int pid;
  275. } Lock;
  276. extern int _tas(int*);
  277. extern void lock(Lock*);
  278. extern void unlock(Lock*);
  279. extern int canlock(Lock*);
  280. typedef struct QLock QLock;
  281. struct QLock
  282. {
  283. Lock use; /* to access Qlock structure */
  284. Proc *head; /* next process waiting for object */
  285. Proc *tail; /* last process waiting for object */
  286. int locked; /* flag */
  287. };
  288. extern void qlock(QLock*);
  289. extern void qunlock(QLock*);
  290. extern int canqlock(QLock*);
  291. extern void _qlockinit(ulong (*)(ulong, ulong)); /* called only by the thread library */
  292. typedef
  293. struct RWLock
  294. {
  295. Lock l; /* Lock modify lock */
  296. QLock x; /* Mutual exclusion lock */
  297. QLock k; /* Lock for waiting writers */
  298. int readers; /* Count of readers in lock */
  299. } RWLock;
  300. extern int canrlock(RWLock*);
  301. extern int canwlock(RWLock*);
  302. extern void rlock(RWLock*);
  303. extern void runlock(RWLock*);
  304. extern void wlock(RWLock*);
  305. extern void wunlock(RWLock*);
  306. /*
  307. * network dialing
  308. */
  309. #define NETPATHLEN 40
  310. /*
  311. * system calls
  312. *
  313. */
  314. #define STATMAX 65535U /* max length of machine-independent stat structure */
  315. #define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */
  316. #define ERRMAX 128 /* max length of error string */
  317. #define MORDER 0x0003 /* mask for bits defining order of mounting */
  318. #define MREPL 0x0000 /* mount replaces object */
  319. #define MBEFORE 0x0001 /* mount goes before others in union directory */
  320. #define MAFTER 0x0002 /* mount goes after others in union directory */
  321. #define MCREATE 0x0004 /* permit creation in mounted directory */
  322. #define MCACHE 0x0010 /* cache some data */
  323. #define MMASK 0x0017 /* all bits on */
  324. #define OREAD 0 /* open for read */
  325. #define OWRITE 1 /* write */
  326. #define ORDWR 2 /* read and write */
  327. #define OEXEC 3 /* execute, == read but check execute permission */
  328. #define OTRUNC 16 /* or'ed in (except for exec), truncate file first */
  329. #define OCEXEC 32 /* or'ed in, close on exec */
  330. #define ORCLOSE 64 /* or'ed in, remove on close */
  331. #define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */
  332. #define AEXIST 0 /* accessible: exists */
  333. #define AEXEC 1 /* execute access */
  334. #define AWRITE 2 /* write access */
  335. #define AREAD 4 /* read access */
  336. /* bits in Qid.type */
  337. #define QTDIR 0x80 /* type bit for directories */
  338. #define QTAPPEND 0x40 /* type bit for append only files */
  339. #define QTEXCL 0x20 /* type bit for exclusive use files */
  340. #define QTMOUNT 0x10 /* type bit for mounted channel */
  341. #define QTAUTH 0x08 /* type bit for authentication file */
  342. #define QTFILE 0x00 /* plain file */
  343. /* bits in Dir.mode */
  344. #define DMDIR 0x80000000 /* mode bit for directories */
  345. #define DMAPPEND 0x40000000 /* mode bit for append only files */
  346. #define DMEXCL 0x20000000 /* mode bit for exclusive use files */
  347. #define DMMOUNT 0x10000000 /* mode bit for mounted channel */
  348. #define DMAUTH 0x08000000 /* mode bit for authentication file */
  349. #define DMREAD 0x4 /* mode bit for read permission */
  350. #define DMWRITE 0x2 /* mode bit for write permission */
  351. #define DMEXEC 0x1 /* mode bit for execute permission */
  352. typedef
  353. struct Qid
  354. {
  355. uvlong path;
  356. ulong vers;
  357. uchar type;
  358. } Qid;
  359. typedef
  360. struct Dir {
  361. /* system-modified data */
  362. ushort type; /* server type */
  363. uint dev; /* server subtype */
  364. /* file data */
  365. Qid qid; /* unique id from server */
  366. ulong mode; /* permissions */
  367. ulong atime; /* last read time */
  368. ulong mtime; /* last write time */
  369. vlong length; /* file length */
  370. char *name; /* last element of path */
  371. char *uid; /* owner name */
  372. char *gid; /* group name */
  373. char *muid; /* last modifier name */
  374. } Dir;
  375. extern Dir* dirstat(char*);
  376. extern Dir* dirfstat(int);
  377. extern int dirwstat(char*, Dir*);
  378. extern int dirfwstat(int, Dir*);
  379. extern long dirread(int, Dir**);
  380. extern void nulldir(Dir*);
  381. extern long dirreadall(int, Dir**);
  382. typedef
  383. struct Waitmsg
  384. {
  385. int pid; /* of loved one */
  386. ulong time[3]; /* of loved one & descendants */
  387. char *msg;
  388. } Waitmsg;
  389. extern void _exits(char*);
  390. extern void exits(char*);
  391. extern int create(char*, int, int);
  392. extern int errstr(char*, uint);
  393. extern void perror(const char*);
  394. extern long readn(int, void*, long);
  395. extern int remove(const char*);
  396. extern void rerrstr(char*, uint);
  397. extern vlong seek(int, vlong, int);
  398. extern int segflush(void*, ulong);
  399. extern void werrstr(char*, ...);
  400. extern char *argv0;
  401. #define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\
  402. argv[0] && argv[0][0]=='-' && argv[0][1];\
  403. argc--, argv++) {\
  404. char *_args, *_argt;\
  405. Rune _argc;\
  406. _args = &argv[0][1];\
  407. if(_args[0]=='-' && _args[1]==0){\
  408. argc--; argv++; break;\
  409. }\
  410. _argc = 0;\
  411. while(*_args && (_args += chartorune(&_argc, _args)))\
  412. switch(_argc)
  413. #define ARGEND SET(_argt);USED(_argt);USED(_argc); USED(_args);}USED(argv); USED(argc);
  414. #define ARGF() (_argt=_args, _args="",\
  415. (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
  416. #define EARGF(x) (_argt=_args, _args="",\
  417. (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
  418. #define ARGC() _argc
  419. /*
  420. * Extensions for Inferno to basic libc.h
  421. */
  422. #define setbinmode()
  423. /*
  424. * Extensions for emu kernel emulation
  425. */
  426. #ifdef EMU
  427. extern Proc *getup(void);
  428. #define up (getup())
  429. /*
  430. * This structure must agree with FPsave and FPrestore asm routines
  431. */
  432. #include <architecture/ppc/fp_regs.h>
  433. typedef union {
  434. double __dbl;
  435. ppc_fp_scr_t __src;
  436. } FPU;
  437. typedef sigjmp_buf osjmpbuf;
  438. #define ossetjmp(buf) sigsetjmp(buf, 1)
  439. #endif