Rob Pike | 69b74c3 | 2008-06-12 13:26:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | Derived from Inferno include/kern.h and |
| 3 | Plan 9 from User Space include/libc.h |
| 4 | |
| 5 | http://code.google.com/p/inferno-os/source/browse/include/kern.h |
| 6 | http://code.swtch.com/plan9port/src/tip/include/libc.h |
| 7 | |
| 8 | Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. |
| 9 | Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com). All rights reserved. |
| 10 | Portions Copyright © 2001-2007 Russ Cox. All rights reserved. |
| 11 | Portions Copyright © 2009 The Go Authors. All rights reserved. |
| 12 | |
| 13 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | of this software and associated documentation files (the "Software"), to deal |
| 15 | in the Software without restriction, including without limitation the rights |
| 16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | copies of the Software, and to permit persons to whom the Software is |
| 18 | furnished to do so, subject to the following conditions: |
| 19 | |
| 20 | The above copyright notice and this permission notice shall be included in |
| 21 | all copies or substantial portions of the Software. |
| 22 | |
| 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 29 | THE SOFTWARE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Lib9 is miscellany from the Plan 9 C library that doesn't |
| 34 | * fit into libutf or into libfmt, but is still missing from traditional |
| 35 | * Unix C libraries. |
| 36 | */ |
| 37 | #ifndef _LIBC_H_ |
| 38 | #define _LIBC_H_ 1 |
| 39 | #if defined(__cplusplus) |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | #include <utf.h> |
| 44 | #include <fmt.h> |
| 45 | |
| 46 | /* |
| 47 | * Begin trimmed down usual libc.h |
| 48 | */ |
| 49 | |
| 50 | #ifndef nil |
| 51 | #define nil ((void*)0) |
| 52 | #endif |
| 53 | #define nelem(x) (sizeof(x)/sizeof((x)[0])) |
| 54 | |
| 55 | #ifndef offsetof |
| 56 | #define offsetof(s, m) (ulong)(&(((s*)0)->m)) |
| 57 | #endif |
| 58 | |
| 59 | extern char* strecpy(char*, char*, char*); |
| 60 | extern int tokenize(char*, char**, int); |
| 61 | |
| 62 | extern double p9cputime(void); |
| 63 | #ifndef NOPLAN9DEFINES |
| 64 | #define cputime p9cputime |
| 65 | #endif |
| 66 | /* |
| 67 | * one-of-a-kind |
| 68 | */ |
| 69 | enum |
| 70 | { |
| 71 | PNPROC = 1, |
| 72 | PNGROUP = 2 |
| 73 | }; |
| 74 | int isInf(double, int); |
| 75 | |
| 76 | extern int p9atoi(char*); |
| 77 | extern long p9atol(char*); |
| 78 | extern vlong p9atoll(char*); |
| 79 | extern double fmtcharstod(int(*)(void*), void*); |
| 80 | extern char* cleanname(char*); |
| 81 | extern int exitcode(char*); |
| 82 | extern void exits(char*); |
| 83 | extern double frexp(double, int*); |
| 84 | extern char* p9getenv(char*); |
| 85 | extern int p9putenv(char*, char*); |
| 86 | extern int getfields(char*, char**, int, int, char*); |
| 87 | extern int gettokens(char *, char **, int, char *); |
| 88 | extern char* getuser(void); |
| 89 | extern char* p9getwd(char*, int); |
| 90 | extern void p9longjmp(p9jmp_buf, int); |
| 91 | extern char* mktemp(char*); |
| 92 | extern int opentemp(char*); |
| 93 | extern void p9notejmp(void*, p9jmp_buf, int); |
| 94 | extern void perror(const char*); |
| 95 | extern int postnote(int, int, char *); |
| 96 | extern double p9pow10(int); |
| 97 | extern char* searchpath(char*); |
| 98 | #define p9setjmp(b) sigsetjmp((void*)(b), 1) |
| 99 | |
| 100 | extern void sysfatal(char*, ...); |
| 101 | |
| 102 | #ifndef NOPLAN9DEFINES |
| 103 | #define atoi p9atoi |
| 104 | #define atol p9atol |
| 105 | #define atoll p9atoll |
| 106 | #define getenv p9getenv |
| 107 | #define getwd p9getwd |
| 108 | #define longjmp p9longjmp |
| 109 | #undef setjmp |
| 110 | #define setjmp p9setjmp |
| 111 | #define putenv p9putenv |
| 112 | #define notejmp p9notejmp |
| 113 | #define jmp_buf p9jmp_buf |
| 114 | #define pow10 p9pow10 |
| 115 | #define strtod fmtstrtod |
| 116 | #define charstod fmtcharstod |
| 117 | #endif |
| 118 | |
| 119 | /* |
| 120 | * system calls |
| 121 | * |
| 122 | */ |
| 123 | #define STATMAX 65535U /* max length of machine-independent stat structure */ |
| 124 | #define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */ |
| 125 | #define ERRMAX 128 /* max length of error string */ |
| 126 | |
| 127 | #define MORDER 0x0003 /* mask for bits defining order of mounting */ |
| 128 | #define MREPL 0x0000 /* mount replaces object */ |
| 129 | #define MBEFORE 0x0001 /* mount goes before others in union directory */ |
| 130 | #define MAFTER 0x0002 /* mount goes after others in union directory */ |
| 131 | #define MCREATE 0x0004 /* permit creation in mounted directory */ |
| 132 | #define MCACHE 0x0010 /* cache some data */ |
| 133 | #define MMASK 0x0017 /* all bits on */ |
| 134 | |
| 135 | #define OREAD 0 /* open for read */ |
| 136 | #define OWRITE 1 /* write */ |
| 137 | #define ORDWR 2 /* read and write */ |
| 138 | #define OEXEC 3 /* execute, == read but check execute permission */ |
| 139 | #define OTRUNC 16 /* or'ed in (except for exec), truncate file first */ |
| 140 | #define OCEXEC 32 /* or'ed in, close on exec */ |
| 141 | #define ORCLOSE 64 /* or'ed in, remove on close */ |
| 142 | #define ODIRECT 128 /* or'ed in, direct access */ |
| 143 | #define ONONBLOCK 256 /* or'ed in, non-blocking call */ |
| 144 | #define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */ |
| 145 | #define OLOCK 0x2000 /* or'ed in, lock after opening */ |
| 146 | #define OAPPEND 0x4000 /* or'ed in, append only */ |
| 147 | |
| 148 | #define AEXIST 0 /* accessible: exists */ |
| 149 | #define AEXEC 1 /* execute access */ |
| 150 | #define AWRITE 2 /* write access */ |
| 151 | #define AREAD 4 /* read access */ |
| 152 | |
| 153 | /* Segattch */ |
| 154 | #define SG_RONLY 0040 /* read only */ |
| 155 | #define SG_CEXEC 0100 /* detach on exec */ |
| 156 | |
| 157 | #define NCONT 0 /* continue after note */ |
| 158 | #define NDFLT 1 /* terminate after note */ |
| 159 | #define NSAVE 2 /* clear note but hold state */ |
| 160 | #define NRSTR 3 /* restore saved state */ |
| 161 | |
| 162 | /* bits in Qid.type */ |
| 163 | #define QTDIR 0x80 /* type bit for directories */ |
| 164 | #define QTAPPEND 0x40 /* type bit for append only files */ |
| 165 | #define QTEXCL 0x20 /* type bit for exclusive use files */ |
| 166 | #define QTMOUNT 0x10 /* type bit for mounted channel */ |
| 167 | #define QTAUTH 0x08 /* type bit for authentication file */ |
| 168 | #define QTTMP 0x04 /* type bit for non-backed-up file */ |
| 169 | #define QTSYMLINK 0x02 /* type bit for symbolic link */ |
| 170 | #define QTFILE 0x00 /* type bits for plain file */ |
| 171 | |
| 172 | /* bits in Dir.mode */ |
| 173 | #define DMDIR 0x80000000 /* mode bit for directories */ |
| 174 | #define DMAPPEND 0x40000000 /* mode bit for append only files */ |
| 175 | #define DMEXCL 0x20000000 /* mode bit for exclusive use files */ |
| 176 | #define DMMOUNT 0x10000000 /* mode bit for mounted channel */ |
| 177 | #define DMAUTH 0x08000000 /* mode bit for authentication file */ |
| 178 | #define DMTMP 0x04000000 /* mode bit for non-backed-up file */ |
| 179 | #define DMSYMLINK 0x02000000 /* mode bit for symbolic link (Unix, 9P2000.u) */ |
| 180 | #define DMDEVICE 0x00800000 /* mode bit for device file (Unix, 9P2000.u) */ |
| 181 | #define DMNAMEDPIPE 0x00200000 /* mode bit for named pipe (Unix, 9P2000.u) */ |
| 182 | #define DMSOCKET 0x00100000 /* mode bit for socket (Unix, 9P2000.u) */ |
| 183 | #define DMSETUID 0x00080000 /* mode bit for setuid (Unix, 9P2000.u) */ |
| 184 | #define DMSETGID 0x00040000 /* mode bit for setgid (Unix, 9P2000.u) */ |
| 185 | |
| 186 | #define DMREAD 0x4 /* mode bit for read permission */ |
| 187 | #define DMWRITE 0x2 /* mode bit for write permission */ |
| 188 | #define DMEXEC 0x1 /* mode bit for execute permission */ |
| 189 | |
| 190 | #ifdef RFMEM /* FreeBSD, OpenBSD */ |
| 191 | #undef RFFDG |
| 192 | #undef RFNOTEG |
| 193 | #undef RFPROC |
| 194 | #undef RFMEM |
| 195 | #undef RFNOWAIT |
| 196 | #undef RFCFDG |
| 197 | #undef RFNAMEG |
| 198 | #undef RFENVG |
| 199 | #undef RFCENVG |
| 200 | #undef RFCFDG |
| 201 | #undef RFCNAMEG |
| 202 | #endif |
| 203 | |
| 204 | enum |
| 205 | { |
| 206 | RFNAMEG = (1<<0), |
| 207 | RFENVG = (1<<1), |
| 208 | RFFDG = (1<<2), |
| 209 | RFNOTEG = (1<<3), |
| 210 | RFPROC = (1<<4), |
| 211 | RFMEM = (1<<5), |
| 212 | RFNOWAIT = (1<<6), |
| 213 | RFCNAMEG = (1<<10), |
| 214 | RFCENVG = (1<<11), |
| 215 | RFCFDG = (1<<12) |
| 216 | /* RFREND = (1<<13), */ |
| 217 | /* RFNOMNT = (1<<14) */ |
| 218 | }; |
| 219 | |
| 220 | typedef |
| 221 | struct Qid |
| 222 | { |
| 223 | uvlong path; |
| 224 | ulong vers; |
| 225 | uchar type; |
| 226 | } Qid; |
| 227 | |
| 228 | typedef |
| 229 | struct Dir { |
| 230 | /* system-modified data */ |
| 231 | ushort type; /* server type */ |
| 232 | uint dev; /* server subtype */ |
| 233 | /* file data */ |
| 234 | Qid qid; /* unique id from server */ |
| 235 | ulong mode; /* permissions */ |
| 236 | ulong atime; /* last read time */ |
| 237 | ulong mtime; /* last write time */ |
| 238 | vlong length; /* file length */ |
| 239 | char *name; /* last element of path */ |
| 240 | char *uid; /* owner name */ |
| 241 | char *gid; /* group name */ |
| 242 | char *muid; /* last modifier name */ |
| 243 | |
| 244 | /* 9P2000.u extensions */ |
| 245 | uint uidnum; /* numeric uid */ |
| 246 | uint gidnum; /* numeric gid */ |
| 247 | uint muidnum; /* numeric muid */ |
| 248 | char *ext; /* extended info */ |
| 249 | } Dir; |
| 250 | |
| 251 | typedef |
| 252 | struct Waitmsg |
| 253 | { |
| 254 | int pid; /* of loved one */ |
| 255 | ulong time[3]; /* of loved one & descendants */ |
| 256 | char *msg; |
| 257 | } Waitmsg; |
| 258 | |
| 259 | extern void _exits(char*); |
| 260 | |
| 261 | extern void abort(void); |
| 262 | extern long p9alarm(ulong); |
| 263 | extern int await(char*, int); |
| 264 | extern int awaitfor(int, char*, int); |
| 265 | extern int awaitnohang(char*, int); |
| 266 | extern int p9chdir(char*); |
| 267 | extern int close(int); |
| 268 | extern int p9create(char*, int, ulong); |
| 269 | extern int p9dup(int, int); |
| 270 | extern int errstr(char*, uint); |
| 271 | extern int p9exec(char*, char*[]); |
| 272 | extern int p9execl(char*, ...); |
| 273 | extern int p9rfork(int); |
| 274 | extern int noted(int); |
| 275 | extern int notify(void(*)(void*, char*)); |
| 276 | extern int noteenable(char*); |
| 277 | extern int notedisable(char*); |
| 278 | extern int notifyon(char*); |
| 279 | extern int notifyoff(char*); |
| 280 | extern int p9open(char*, int); |
| 281 | extern int fd2path(int, char*, int); |
| 282 | extern int p9pipe(int*); |
| 283 | extern long readn(int, void*, long); |
| 284 | extern int remove(const char*); |
| 285 | extern vlong p9seek(int, vlong, int); |
| 286 | extern int p9sleep(long); |
| 287 | extern Waitmsg* p9wait(void); |
| 288 | extern Waitmsg* p9waitfor(int); |
| 289 | extern Waitmsg* waitnohang(void); |
| 290 | extern int p9waitpid(void); |
| 291 | extern ulong rendezvous(ulong, ulong); |
| 292 | |
| 293 | #ifndef NOPLAN9DEFINES |
| 294 | #define alarm p9alarm |
| 295 | #define dup p9dup |
| 296 | #define exec p9exec |
| 297 | #define execl p9execl |
| 298 | #define seek p9seek |
| 299 | #define sleep p9sleep |
| 300 | #define wait p9wait |
| 301 | #define waitpid p9waitpid |
| 302 | #define rfork p9rfork |
| 303 | #define create p9create |
| 304 | #undef open |
| 305 | #define open p9open |
| 306 | #define pipe p9pipe |
| 307 | #define waitfor p9waitfor |
| 308 | #endif |
| 309 | |
| 310 | extern Dir* dirstat(char*); |
| 311 | extern Dir* dirfstat(int); |
| 312 | extern int dirwstat(char*, Dir*); |
| 313 | extern int dirfwstat(int, Dir*); |
| 314 | extern void nulldir(Dir*); |
| 315 | extern long dirreadall(int, Dir**); |
| 316 | extern void rerrstr(char*, uint); |
| 317 | extern char* sysname(void); |
| 318 | extern void werrstr(char*, ...); |
| 319 | extern char* getns(void); |
| 320 | extern char* get9root(void); |
| 321 | extern char* unsharp(char*); |
| 322 | |
| 323 | /* external names that we don't want to step on */ |
| 324 | #ifndef NOPLAN9DEFINES |
| 325 | #define main p9main |
| 326 | #endif |
| 327 | |
| 328 | /* compiler directives on plan 9 */ |
| 329 | #define SET(x) ((x)=0) |
| 330 | #define USED(x) if(x){}else{} |
| 331 | #ifdef __GNUC__ |
| 332 | # if __GNUC__ >= 3 |
| 333 | # undef USED |
| 334 | # define USED(x) ((void)(x)) |
| 335 | # endif |
| 336 | #endif |
| 337 | |
| 338 | /* command line */ |
| 339 | extern char *argv0; |
| 340 | extern void __fixargv0(void); |
| 341 | #define ARGBEGIN for((argv0?0:(argv0=(__fixargv0(),*argv))),argv++,argc--;\ |
| 342 | argv[0] && argv[0][0]=='-' && argv[0][1];\ |
| 343 | argc--, argv++) {\ |
| 344 | char *_args, *_argt;\ |
| 345 | Rune _argc;\ |
| 346 | _args = &argv[0][1];\ |
| 347 | if(_args[0]=='-' && _args[1]==0){\ |
| 348 | argc--; argv++; break;\ |
| 349 | }\ |
| 350 | _argc = 0;\ |
| 351 | while(*_args && (_args += chartorune(&_argc, _args)))\ |
| 352 | switch(_argc) |
| 353 | #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc); |
| 354 | #define ARGF() (_argt=_args, _args="",\ |
| 355 | (*_argt? _argt: argv[1]? (argc--, *++argv): 0)) |
| 356 | #define EARGF(x) (_argt=_args, _args="",\ |
| 357 | (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) |
| 358 | |
| 359 | #define ARGC() _argc |
| 360 | |
| 361 | #if defined(__cplusplus) |
| 362 | } |
| 363 | #endif |
| 364 | #endif /* _LIB9_H_ */ |