lib.h 8.6 KB

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