lib9.h 12 KB

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