lib9.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* define _BSD_SOURCE to use ISO C, POSIX, and 4.3BSD things. */
  2. #ifndef _BSD_SOURCE
  3. #define _BSD_SOURCE
  4. #endif
  5. #define _XOPEN_SOURCE 500
  6. #define _LARGEFILE_SOURCE 1
  7. #define _LARGEFILE64_SOURCE 1
  8. #define _FILE_OFFSET_BITS 64
  9. #include <features.h>
  10. #include <sys/types.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #define sync __os_sync
  14. #include <unistd.h>
  15. #undef sync
  16. #include <errno.h>
  17. #define __NO_STRING_INLINES
  18. #include <string.h>
  19. #include "math.h"
  20. #include <fcntl.h>
  21. #include <setjmp.h>
  22. #include <float.h>
  23. #include <endian.h>
  24. #define getwd infgetwd
  25. #ifndef EMU
  26. typedef struct Proc Proc;
  27. #endif
  28. /*
  29. * math module dtoa
  30. * #define __LITTLE_ENDIAN /usr/include/endian.h under linux
  31. */
  32. #undef __LITTLE_ENDIAN
  33. #define nil ((void*)0)
  34. typedef unsigned char uchar;
  35. typedef signed char schar;
  36. typedef unsigned short Rune;
  37. typedef long long int vlong;
  38. typedef unsigned long long int uvlong;
  39. typedef unsigned int u32int;
  40. typedef uvlong u64int;
  41. typedef size_t usize;
  42. typedef unsigned int mpdigit; /* for /sys/include/mp.h */
  43. typedef unsigned short u16int;
  44. typedef unsigned char u8int;
  45. typedef unsigned long uintptr;
  46. #define USED(x) if(x){}else{}
  47. #define SET(x)
  48. #undef nelem
  49. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  50. #undef offsetof
  51. #define offsetof(s, m) (ulong)(&(((s*)0)->m))
  52. #undef assert
  53. #define assert(x) if(x){}else _assert("x")
  54. /*
  55. * most mem and string routines are declared by ANSI/POSIX files above
  56. */
  57. extern char* strecpy(char*, char*, char*);
  58. extern char* strdup(const char*);
  59. extern int cistrncmp(char*, char*, int);
  60. extern int cistrcmp(char*, char*);
  61. extern char* cistrstr(char*, char*);
  62. extern int tokenize(char*, char**, int);
  63. enum
  64. {
  65. UTFmax = 3, /* maximum bytes per rune */
  66. Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
  67. Runeself = 0x80, /* rune and UTF sequences are the same (<) */
  68. Runeerror = 0x80 /* decoding error in UTF */
  69. };
  70. /*
  71. * rune routines
  72. */
  73. extern int runetochar(char*, Rune*);
  74. extern int chartorune(Rune*, char*);
  75. extern int runelen(long);
  76. extern int runenlen(Rune*, int);
  77. extern int fullrune(char*, int);
  78. extern int utflen(char*);
  79. extern int utfnlen(char*, long);
  80. extern char* utfrune(char*, long);
  81. extern char* utfrrune(char*, long);
  82. extern char* utfutf(char*, char*);
  83. extern char* utfecpy(char*, char*, char*);
  84. extern Rune* runestrcat(Rune*, Rune*);
  85. extern Rune* runestrchr(Rune*, Rune);
  86. extern int runestrcmp(Rune*, Rune*);
  87. extern Rune* runestrcpy(Rune*, Rune*);
  88. extern Rune* runestrncpy(Rune*, Rune*, long);
  89. extern Rune* runestrecpy(Rune*, Rune*, Rune*);
  90. extern Rune* runestrdup(Rune*);
  91. extern Rune* runestrncat(Rune*, Rune*, long);
  92. extern int runestrncmp(Rune*, Rune*, long);
  93. extern Rune* runestrrchr(Rune*, Rune);
  94. extern long runestrlen(Rune*);
  95. extern Rune* runestrstr(Rune*, Rune*);
  96. extern Rune tolowerrune(Rune);
  97. extern Rune totitlerune(Rune);
  98. extern Rune toupperrune(Rune);
  99. extern int isalpharune(Rune);
  100. extern int islowerrune(Rune);
  101. extern int isspacerune(Rune);
  102. extern int istitlerune(Rune);
  103. extern int isupperrune(Rune);
  104. /*
  105. * malloc
  106. */
  107. extern void* malloc(size_t);
  108. extern void* mallocz(ulong, int);
  109. extern void free(void*);
  110. extern ulong msize(void*);
  111. extern void* calloc(size_t, size_t);
  112. extern void* realloc(void*, size_t);
  113. extern void setmalloctag(void*, ulong);
  114. extern void setrealloctag(void*, ulong);
  115. extern ulong getmalloctag(void*);
  116. extern ulong getrealloctag(void*);
  117. extern void* malloctopoolblock(void*);
  118. /*
  119. * print routines
  120. */
  121. typedef struct Fmt Fmt;
  122. struct Fmt{
  123. uchar runes; /* output buffer is runes or chars? */
  124. void *start; /* of buffer */
  125. void *to; /* current place in the buffer */
  126. void *stop; /* end of the buffer; overwritten if flush fails */
  127. int (*flush)(Fmt *); /* called when to == stop */
  128. void *farg; /* to make flush a closure */
  129. int nfmt; /* num chars formatted so far */
  130. va_list args; /* args passed to dofmt */
  131. int r; /* % format Rune */
  132. int width;
  133. int prec;
  134. ulong flags;
  135. };
  136. enum{
  137. FmtWidth = 1,
  138. FmtLeft = FmtWidth << 1,
  139. FmtPrec = FmtLeft << 1,
  140. FmtSharp = FmtPrec << 1,
  141. FmtSpace = FmtSharp << 1,
  142. FmtSign = FmtSpace << 1,
  143. FmtZero = FmtSign << 1,
  144. FmtUnsigned = FmtZero << 1,
  145. FmtShort = FmtUnsigned << 1,
  146. FmtLong = FmtShort << 1,
  147. FmtVLong = FmtLong << 1,
  148. FmtComma = FmtVLong << 1,
  149. FmtByte = FmtComma << 1,
  150. FmtFlag = FmtByte << 1
  151. };
  152. extern int print(char*, ...);
  153. extern char* seprint(char*, char*, char*, ...);
  154. extern char* vseprint(char*, char*, char*, va_list);
  155. extern int snprint(char*, int, char*, ...);
  156. extern int vsnprint(char*, int, char*, va_list);
  157. extern char* smprint(char*, ...);
  158. extern char* vsmprint(char*, va_list);
  159. extern int sprint(char*, char*, ...);
  160. extern int fprint(int, char*, ...);
  161. extern int vfprint(int, char*, va_list);
  162. extern int runesprint(Rune*, char*, ...);
  163. extern int runesnprint(Rune*, int, char*, ...);
  164. extern int runevsnprint(Rune*, int, char*, va_list);
  165. extern Rune* runeseprint(Rune*, Rune*, char*, ...);
  166. extern Rune* runevseprint(Rune*, Rune*, char*, va_list);
  167. extern Rune* runesmprint(char*, ...);
  168. extern Rune* runevsmprint(char*, va_list);
  169. extern int fmtfdinit(Fmt*, int, char*, int);
  170. extern int fmtfdflush(Fmt*);
  171. extern int fmtstrinit(Fmt*);
  172. extern char* fmtstrflush(Fmt*);
  173. extern int runefmtstrinit(Fmt*);
  174. extern Rune* runefmtstrflush(Fmt*);
  175. extern int fmtinstall(int, int (*)(Fmt*));
  176. extern int dofmt(Fmt*, char*);
  177. extern int dorfmt(Fmt*, Rune*);
  178. extern int fmtprint(Fmt*, char*, ...);
  179. extern int fmtvprint(Fmt*, char*, va_list);
  180. extern int fmtrune(Fmt*, int);
  181. extern int fmtstrcpy(Fmt*, char*);
  182. extern int fmtrunestrcpy(Fmt*, Rune*);
  183. /*
  184. * error string for %r
  185. * supplied on per os basis, not part of fmt library
  186. */
  187. extern int errfmt(Fmt *f);
  188. /*
  189. * quoted strings
  190. */
  191. extern char *unquotestrdup(char*);
  192. extern Rune *unquoterunestrdup(Rune*);
  193. extern char *quotestrdup(char*);
  194. extern Rune *quoterunestrdup(Rune*);
  195. extern int quotestrfmt(Fmt*);
  196. extern int quoterunestrfmt(Fmt*);
  197. extern void quotefmtinstall(void);
  198. extern int (*doquote)(int);
  199. /*
  200. * random number
  201. */
  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. /*
  210. * Time-of-day
  211. */
  212. typedef struct Tm Tm;
  213. struct Tm {
  214. int sec;
  215. int min;
  216. int hour;
  217. int mday;
  218. int mon;
  219. int year;
  220. int wday;
  221. int yday;
  222. char zone[4];
  223. int tzoff;
  224. };
  225. extern vlong osnsec(void);
  226. #define nsec osnsec
  227. /*
  228. * one-of-a-kind
  229. */
  230. extern void _assert(char*);
  231. extern double charstod(int(*)(void*), void*);
  232. extern char* cleanname(char*);
  233. extern ulong getcallerpc(void*);
  234. extern int getfields(char*, char**, int, int, char*);
  235. extern char* getuser(void);
  236. extern char* getwd(char*, int);
  237. extern double ipow10(int);
  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. /*
  412. * This structure must agree with FPsave and FPrestore asm routines
  413. */
  414. typedef struct FPU FPU;
  415. struct FPU
  416. {
  417. uchar env[18*8];
  418. };
  419. /*
  420. * Later versions of Linux seemed to need large stack for gethostbyname()
  421. * so we had this at 128k, which is excessive. More recently, we've
  422. * reduced it again after testing stack usage by gethostbyname.
  423. */
  424. #define KSTACK (16 * 1024)
  425. static __inline Proc *getup(void) {
  426. Proc *p;
  427. __asm__( "mr %0, 1" : "=r" (p));
  428. return *(Proc **)((unsigned long)p & ~(KSTACK - 1));
  429. };
  430. #define up (getup())
  431. typedef sigjmp_buf osjmpbuf;
  432. #define ossetjmp(buf) sigsetjmp(buf, 1)
  433. #endif