lib9.h 12 KB

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