lib.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* TODO: it really ought to be possible to include <libc.h>, not "../port/lib.h". */
  10. /*
  11. * functions (possibly) linked in, complete, from libc.
  12. */
  13. #define nelem(x) (sizeof(x) / sizeof((x)[0]))
  14. #define offsetof(s, m) (u64)(&(((s *)0)->m))
  15. #define assert(x) \
  16. do { \
  17. if(!(x)) \
  18. _assert(#x); \
  19. } while(0)
  20. /*
  21. * mem routines
  22. */
  23. extern void *memccpy(void *, void *, int, u32);
  24. extern void *memset(void *, int, u32);
  25. extern int memcmp(void *, void *, u32);
  26. extern void *memmove(void *, void *, u32);
  27. extern void *memchr(void *, int, u32);
  28. /*
  29. * string routines
  30. */
  31. extern char *strcat(char *, const char *);
  32. extern char *strchr(const char *, int);
  33. extern int strcmp(const char *, const char *);
  34. extern char *strcpy(char *, const char *);
  35. extern char *strecpy(char *, char *, const char *);
  36. extern char *strncat(char *, const char *, i32);
  37. extern usize strlcpy(char *, const char *, i32);
  38. extern char *strncpy(char *, const char *, i32);
  39. extern int strncmp(const char *, const char *, i32);
  40. extern char *strrchr(const char *, int);
  41. extern int strlen(const char *);
  42. extern char *strstr(const char *, const char *);
  43. extern int cistrncmp(const char *, const char *, int);
  44. extern int cistrcmp(const char *, const char *);
  45. extern int tokenize(char *, char **, int);
  46. enum {
  47. UTFmax = 3, /* maximum bytes per rune */
  48. Runesync = 0x80, /* cannot represent part of a UTF sequence */
  49. Runeself = 0x80, /* rune and UTF sequences are the same (<) */
  50. Runeerror = 0xFFFD, /* decoding error in UTF */
  51. };
  52. /*
  53. * rune routines
  54. */
  55. extern int runetochar(char *, Rune *);
  56. extern int chartorune(Rune *, char *);
  57. extern int runelen(i32);
  58. extern int fullrune(char *, int);
  59. extern int utflen(char *);
  60. extern int utfnlen(char *, i32);
  61. extern char *utfrune(char *, i32);
  62. /*
  63. * malloc
  64. */
  65. extern void *malloc(u32);
  66. extern void *mallocz(u32, int);
  67. extern void free(void *);
  68. extern u32 msize(void *);
  69. extern void *mallocalign(u32, u32, i32, u32);
  70. extern void setmalloctag(void *, u32);
  71. extern void setrealloctag(void *, u32);
  72. extern u32 getmalloctag(void *);
  73. extern u32 getrealloctag(void *);
  74. extern void *realloc(void *, u32);
  75. /* from BSD */
  76. void *reallocarray(void *base, usize nel, usize size);
  77. /*
  78. * print routines
  79. */
  80. typedef struct Fmt Fmt;
  81. struct Fmt {
  82. unsigned char runes; /* output buffer is runes or chars? */
  83. void *start; /* of buffer */
  84. void *to; /* current place in the buffer */
  85. void *stop; /* end of the buffer; overwritten if flush fails */
  86. int (*flush)(Fmt *); /* called when to == stop */
  87. void *farg; /* to make flush a closure */
  88. int nfmt; /* num chars formatted so far */
  89. va_list args; /* args passed to dofmt */
  90. int r; /* % format Rune */
  91. int width;
  92. int prec;
  93. u32 flags;
  94. };
  95. enum {
  96. FmtWidth = 1,
  97. FmtLeft = FmtWidth << 1,
  98. FmtPrec = FmtLeft << 1,
  99. FmtSharp = FmtPrec << 1,
  100. FmtSpace = FmtSharp << 1,
  101. FmtSign = FmtSpace << 1,
  102. FmtZero = FmtSign << 1,
  103. FmtUnsigned = FmtZero << 1,
  104. FmtShort = FmtUnsigned << 1,
  105. FmtLong = FmtShort << 1,
  106. FmtVLong = FmtLong << 1,
  107. FmtComma = FmtVLong << 1,
  108. FmtByte = FmtComma << 1,
  109. FmtFlag = FmtByte << 1
  110. };
  111. extern int print(char *, ...);
  112. extern char *seprint(char *, char *, char *, ...);
  113. extern char *vseprint(char *, char *, char *, va_list);
  114. extern int snprint(char *, int, char *, ...);
  115. extern int vsnprint(char *, int, char *, va_list);
  116. extern int sprint(char *, char *, ...);
  117. extern int fmtinstall(int, int (*)(Fmt *));
  118. extern int fmtprint(Fmt *, char *, ...);
  119. extern int fmtstrcpy(Fmt *, char *);
  120. extern char *fmtstrflush(Fmt *);
  121. extern int fmtstrinit(Fmt *);
  122. /*
  123. * quoted strings
  124. */
  125. extern void quotefmtinstall(void);
  126. /*
  127. * Time-of-day
  128. */
  129. extern void cycles(u64 *); /* 64-bit value of the cycle counter if there is one, 0 if there isn't */
  130. /*
  131. * NIX core types
  132. */
  133. enum {
  134. NIXTC = 0,
  135. NIXKC,
  136. NIXAC,
  137. NIXXC,
  138. NIXROLES,
  139. };
  140. /*
  141. * one-of-a-kind
  142. */
  143. extern int abs(int);
  144. extern int atoi(char *);
  145. extern char *cleanname(char *);
  146. extern int dec64(unsigned char *, int, char *, int);
  147. extern int getfields(char *, char **, int, int, char *);
  148. extern int gettokens(char *, char **, int, char *);
  149. extern i32 strtol(char *, char **, int);
  150. extern u32 strtoul(char *, char **, int);
  151. extern i64 strtoll(char *, char **, int);
  152. extern u64 strtoull(char *, char **, int);
  153. extern void qsort(void *, i32, i32,
  154. int (*)(const void *, const void *));
  155. /*
  156. * Syscall data structures
  157. */
  158. #define MORDER 0x0003 /* mask for bits defining order of mounting */
  159. #define MREPL 0x0000 /* mount replaces object */
  160. #define MBEFORE 0x0001 /* mount goes before others in union directory */
  161. #define MAFTER 0x0002 /* mount goes after others in union directory */
  162. #define MCREATE 0x0004 /* permit creation in mounted directory */
  163. #define MCACHE 0x0010 /* cache some data */
  164. #define MMASK 0x0017 /* all bits on */
  165. #define OREAD 0 /* open for read */
  166. #define OWRITE 1 /* write */
  167. #define ORDWR 2 /* read and write */
  168. #define OEXEC 3 /* execute, == read but check execute permission */
  169. #define OTRUNC 16 /* or'ed in (except for exec), truncate file first */
  170. #define OCEXEC 32 /* or'ed in, close on exec */
  171. #define ORCLOSE 64 /* or'ed in, remove on close */
  172. #define OEXCL 0x1000 /* or'ed in, exclusive create */
  173. #define NCONT 0 /* continue after note */
  174. #define NDFLT 1 /* terminate after note */
  175. #define NSAVE 2 /* clear note but hold state */
  176. #define NRSTR 3 /* restore saved state */
  177. typedef struct Qid Qid;
  178. typedef struct Dir Dir;
  179. typedef struct Waitmsg Waitmsg;
  180. #define ERRMAX 128 /* max length of error string */
  181. #define KNAMELEN 28 /* max length of name held in kernel */
  182. /* bits in Qid.type */
  183. #define QTDIR 0x80 /* type bit for directories */
  184. #define QTAPPEND 0x40 /* type bit for append only files */
  185. #define QTEXCL 0x20 /* type bit for exclusive use files */
  186. #define QTMOUNT 0x10 /* type bit for mounted channel */
  187. #define QTAUTH 0x08 /* type bit for authentication file */
  188. #define QTTMP 0x04 /* type bit for not-backed-up file */
  189. #define QTSYMLINK 0x02 /* type bit for symlink */
  190. #define QTFILE 0x00 /* plain file */
  191. /* bits in Dir.mode */
  192. #define DMDIR 0x80000000 /* mode bit for directories */
  193. #define DMAPPEND 0x40000000 /* mode bit for append only files */
  194. #define DMEXCL 0x20000000 /* mode bit for exclusive use files */
  195. #define DMMOUNT 0x10000000 /* mode bit for mounted channel */
  196. #define DMSYMLINK 0x02000000 /* mode bit for symlnk */
  197. #define DMREAD 0x4 /* mode bit for read permission */
  198. #define DMWRITE 0x2 /* mode bit for write permission */
  199. #define DMEXEC 0x1 /* mode bit for execute permission */
  200. struct Qid {
  201. u64 path;
  202. u32 vers;
  203. unsigned char type;
  204. };
  205. struct Dir {
  206. /* system-modified data */
  207. u16 type; /* server type */
  208. u32 dev; /* server subtype */
  209. /* file data */
  210. Qid qid; /* unique id from server */
  211. u32 mode; /* permissions */
  212. u32 atime; /* last read time */
  213. u32 mtime; /* last write time */
  214. i64 length; /* file length: see <u.h> */
  215. char *name; /* last element of path */
  216. char *uid; /* owner name */
  217. char *gid; /* group name */
  218. char *muid; /* last modifier name */
  219. };
  220. struct Waitmsg {
  221. int pid; /* of loved one */
  222. u32 time[3]; /* of loved one and descendants */
  223. char msg[ERRMAX]; /* actually variable-size in user mode */
  224. };
  225. /*
  226. * Zero-copy I/O
  227. */
  228. typedef struct Zio Zio;
  229. struct Zio {
  230. void *data;
  231. u32 size;
  232. };
  233. extern char etext[];
  234. extern char erodata[];
  235. extern char edata[];
  236. extern char end[];
  237. /* debugging. */
  238. void __print_func_entry(const char *func, const char *file);
  239. void __print_func_exit(const char *func, const char *file);
  240. #define print_func_entry() __print_func_entry(__FUNCTION__, __FILE__)
  241. #define print_func_exit() __print_func_exit(__FUNCTION__, __FILE__)
  242. extern int printx_on;
  243. void set_printx(int mode);
  244. /* compiler directives on plan 9 */
  245. #define SET(x) ((x) = 0)
  246. #define USED(x) \
  247. if(x) { \
  248. } else { \
  249. }
  250. #ifdef __GNUC__
  251. #if __GNUC__ >= 3
  252. #undef USED
  253. #define USED(x) ((void)(x))
  254. #endif
  255. #endif
  256. typedef struct PSlice PSlice;
  257. struct PSlice {
  258. void **ptrs;
  259. usize len;
  260. usize capacity;
  261. };
  262. void psliceinit(PSlice *slice);
  263. void psliceclear(PSlice *slice);
  264. void *psliceget(PSlice *slice, usize i);
  265. int psliceput(PSlice *slice, usize i, void *p);
  266. int pslicedel(PSlice *slice, usize i);
  267. void psliceappend(PSlice *s, void *p);
  268. usize pslicelen(PSlice *slice);
  269. void **pslicefinalize(PSlice *slice);
  270. void pslicedestroy(PSlice *slice);