lib9.h 13 KB

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