github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/include/libc.h (about) 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* p9getwd(char*, int); 89 extern void p9longjmp(p9jmp_buf, int); 90 extern void p9notejmp(void*, p9jmp_buf, int); 91 extern void perror(const char*); 92 extern int postnote(int, int, char *); 93 extern double p9pow10(int); 94 extern char* p9ctime(long); 95 #define p9setjmp(b) sigsetjmp((void*)(b), 1) 96 97 extern void sysfatal(char*, ...); 98 99 #ifndef NOPLAN9DEFINES 100 #define atoi p9atoi 101 #define atol p9atol 102 #define atoll p9atoll 103 #define getenv p9getenv 104 #define getwd p9getwd 105 #define longjmp p9longjmp 106 #undef setjmp 107 #define setjmp p9setjmp 108 #define putenv p9putenv 109 #define notejmp p9notejmp 110 #define jmp_buf p9jmp_buf 111 #define pow10 p9pow10 112 #undef strtod 113 #define strtod fmtstrtod 114 #define charstod fmtcharstod 115 #define ctime p9ctime 116 #endif 117 118 /* 119 * system calls 120 * 121 */ 122 #define STATMAX 65535U /* max length of machine-independent stat structure */ 123 #define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */ 124 #define ERRMAX 128 /* max length of error string */ 125 126 #define MORDER 0x0003 /* mask for bits defining order of mounting */ 127 #define MREPL 0x0000 /* mount replaces object */ 128 #define MBEFORE 0x0001 /* mount goes before others in union directory */ 129 #define MAFTER 0x0002 /* mount goes after others in union directory */ 130 #define MCREATE 0x0004 /* permit creation in mounted directory */ 131 #define MCACHE 0x0010 /* cache some data */ 132 #define MMASK 0x0017 /* all bits on */ 133 134 #define OREAD 0 /* open for read */ 135 #define OWRITE 1 /* write */ 136 #define ORDWR 2 /* read and write */ 137 #define OEXEC 3 /* execute, == read but check execute permission */ 138 #define OTRUNC 16 /* or'ed in (except for exec), truncate file first */ 139 #define ORCLOSE 64 /* or'ed in, remove on close */ 140 #define ODIRECT 128 /* or'ed in, direct access */ 141 #define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */ 142 #define OAPPEND 0x4000 /* or'ed in, append only */ 143 144 #define AEXIST 0 /* accessible: exists */ 145 #define AEXEC 1 /* execute access */ 146 #define AWRITE 2 /* write access */ 147 #define AREAD 4 /* read access */ 148 149 /* Segattch */ 150 #define SG_RONLY 0040 /* read only */ 151 #define SG_CEXEC 0100 /* detach on exec */ 152 153 #define NCONT 0 /* continue after note */ 154 #define NDFLT 1 /* terminate after note */ 155 #define NSAVE 2 /* clear note but hold state */ 156 #define NRSTR 3 /* restore saved state */ 157 158 /* bits in Qid.type */ 159 #define QTDIR 0x80 /* type bit for directories */ 160 #define QTAPPEND 0x40 /* type bit for append only files */ 161 #define QTEXCL 0x20 /* type bit for exclusive use files */ 162 #define QTMOUNT 0x10 /* type bit for mounted channel */ 163 #define QTAUTH 0x08 /* type bit for authentication file */ 164 #define QTTMP 0x04 /* type bit for non-backed-up file */ 165 #define QTSYMLINK 0x02 /* type bit for symbolic link */ 166 #define QTFILE 0x00 /* type bits for plain file */ 167 168 /* bits in Dir.mode */ 169 #define DMDIR 0x80000000 /* mode bit for directories */ 170 #define DMAPPEND 0x40000000 /* mode bit for append only files */ 171 #define DMEXCL 0x20000000 /* mode bit for exclusive use files */ 172 #define DMMOUNT 0x10000000 /* mode bit for mounted channel */ 173 #define DMAUTH 0x08000000 /* mode bit for authentication file */ 174 #define DMTMP 0x04000000 /* mode bit for non-backed-up file */ 175 #define DMSYMLINK 0x02000000 /* mode bit for symbolic link (Unix, 9P2000.u) */ 176 #define DMDEVICE 0x00800000 /* mode bit for device file (Unix, 9P2000.u) */ 177 #define DMNAMEDPIPE 0x00200000 /* mode bit for named pipe (Unix, 9P2000.u) */ 178 #define DMSOCKET 0x00100000 /* mode bit for socket (Unix, 9P2000.u) */ 179 #define DMSETUID 0x00080000 /* mode bit for setuid (Unix, 9P2000.u) */ 180 #define DMSETGID 0x00040000 /* mode bit for setgid (Unix, 9P2000.u) */ 181 182 #define DMREAD 0x4 /* mode bit for read permission */ 183 #define DMWRITE 0x2 /* mode bit for write permission */ 184 #define DMEXEC 0x1 /* mode bit for execute permission */ 185 186 #ifdef RFMEM /* FreeBSD, OpenBSD, NetBSD */ 187 #undef RFFDG 188 #undef RFNOTEG 189 #undef RFPROC 190 #undef RFMEM 191 #undef RFNOWAIT 192 #undef RFCFDG 193 #undef RFNAMEG 194 #undef RFENVG 195 #undef RFCENVG 196 #undef RFCFDG 197 #undef RFCNAMEG 198 #endif 199 200 enum 201 { 202 RFNAMEG = (1<<0), 203 RFENVG = (1<<1), 204 RFFDG = (1<<2), 205 RFNOTEG = (1<<3), 206 RFPROC = (1<<4), 207 RFMEM = (1<<5), 208 RFNOWAIT = (1<<6), 209 RFCNAMEG = (1<<10), 210 RFCENVG = (1<<11), 211 RFCFDG = (1<<12) 212 /* RFREND = (1<<13), */ 213 /* RFNOMNT = (1<<14) */ 214 }; 215 216 typedef 217 struct Qid 218 { 219 uvlong path; 220 ulong vers; 221 uchar type; 222 } Qid; 223 224 typedef 225 struct Dir { 226 /* system-modified data */ 227 ushort type; /* server type */ 228 uint dev; /* server subtype */ 229 /* file data */ 230 Qid qid; /* unique id from server */ 231 ulong mode; /* permissions */ 232 ulong atime; /* last read time */ 233 ulong mtime; /* last write time */ 234 vlong length; /* file length */ 235 char *name; /* last element of path */ 236 char *uid; /* owner name */ 237 char *gid; /* group name */ 238 char *muid; /* last modifier name */ 239 240 /* 9P2000.u extensions */ 241 uint uidnum; /* numeric uid */ 242 uint gidnum; /* numeric gid */ 243 uint muidnum; /* numeric muid */ 244 char *ext; /* extended info */ 245 } Dir; 246 247 typedef 248 struct Waitmsg 249 { 250 int pid; /* of loved one */ 251 ulong time[3]; /* of loved one & descendants */ 252 char *msg; 253 } Waitmsg; 254 255 extern void _exits(char*); 256 257 extern void abort(void); 258 extern long p9alarm(ulong); 259 extern int await(char*, int); 260 extern int awaitfor(int, char*, int); 261 extern int awaitnohang(char*, int); 262 extern int p9chdir(char*); 263 extern int close(int); 264 extern int p9create(char*, int, ulong); 265 extern int p9dup(int, int); 266 extern int errstr(char*, uint); 267 extern int p9exec(char*, char*[]); 268 extern int p9execl(char*, ...); 269 extern int p9rfork(int); 270 extern int noted(int); 271 extern int notify(void(*)(void*, char*)); 272 extern int noteenable(char*); 273 extern int notedisable(char*); 274 extern int notifyon(char*); 275 extern int notifyoff(char*); 276 extern int p9open(char*, int); 277 extern int fd2path(int, char*, int); 278 extern long readn(int, void*, long); 279 extern int remove(const char*); 280 extern vlong p9seek(int, vlong, int); 281 extern int p9sleep(long); 282 extern Waitmsg* p9wait(void); 283 extern Waitmsg* p9waitfor(int); 284 extern Waitmsg* waitnohang(void); 285 extern int p9waitpid(void); 286 extern ulong rendezvous(ulong, ulong); 287 288 extern char* getgoos(void); 289 extern char* getgoarch(void); 290 extern char* getgoroot(void); 291 extern char* getgoversion(void); 292 extern char* getgoarm(void); 293 extern char* getgo386(void); 294 extern char* getgoextlinkenabled(void); 295 296 extern char* mktempdir(void); 297 extern void removeall(char*); 298 extern int runcmd(char**); 299 300 extern void flagcount(char*, char*, int*); 301 extern void flagint32(char*, char*, int32*); 302 extern void flagint64(char*, char*, int64*); 303 extern void flagstr(char*, char*, char**); 304 extern void flagparse(int*, char***, void (*usage)(void)); 305 extern void flagfn0(char*, char*, void(*fn)(void)); 306 extern void flagfn1(char*, char*, void(*fn)(char*)); 307 extern void flagfn2(char*, char*, void(*fn)(char*, char*)); 308 extern void flagprint(int); 309 310 #ifdef _WIN32 311 312 #if !defined(_WIN64) && !defined(__MINGW64_VERSION_MAJOR) 313 #define execv(prog, argv) execv(prog, (const char* const*)(argv)) 314 #define execvp(prog, argv) execvp(prog, (const char**)(argv)) 315 #endif 316 317 #undef getwd 318 #define getwd(s, ns) getcwd(s, ns) 319 #undef lseek 320 #define lseek(fd, n, base) _lseeki64(fd, n, base) 321 #define mkdir(path, perm) mkdir(path) 322 #else 323 #define O_BINARY 0 324 #endif 325 326 #ifndef NOPLAN9DEFINES 327 #define alarm p9alarm 328 #define dup p9dup 329 #define exec p9exec 330 #define execl p9execl 331 #define seek p9seek 332 #define sleep p9sleep 333 #define wait p9wait 334 #define waitpid p9waitpid 335 #define rfork p9rfork 336 #define create p9create 337 #undef open 338 #define open p9open 339 #define waitfor p9waitfor 340 #endif 341 342 extern Dir* dirstat(char*); 343 extern Dir* dirfstat(int); 344 extern int dirwstat(char*, Dir*); 345 extern int dirfwstat(int, Dir*); 346 extern void nulldir(Dir*); 347 extern long dirreadall(int, Dir**); 348 extern void rerrstr(char*, uint); 349 extern char* sysname(void); 350 extern void werrstr(char*, ...); 351 extern char* getns(void); 352 extern char* get9root(void); 353 extern char* unsharp(char*); 354 355 /* external names that we don't want to step on */ 356 #ifndef NOPLAN9DEFINES 357 #define main p9main 358 #endif 359 360 /* compiler directives on plan 9 */ 361 #define SET(x) ((x)=0) 362 #define USED(x) if(x){}else{} 363 #ifdef __GNUC__ 364 # if __GNUC__ >= 3 365 # undef USED 366 # define USED(x) ((void)(x)) 367 # endif 368 #endif 369 370 /* command line */ 371 extern char *argv0; 372 extern void __fixargv0(void); 373 #define ARGBEGIN for((void)(argv0?0:(argv0=(__fixargv0(),*argv))),argv++,argc--;\ 374 argv[0] && argv[0][0]=='-' && argv[0][1];\ 375 argc--, argv++) {\ 376 char *_args, *_argt;\ 377 Rune _argc;\ 378 _args = &argv[0][1];\ 379 if(_args[0]=='-' && _args[1]==0){\ 380 argc--; argv++; break;\ 381 }\ 382 _argc = 0;\ 383 while(*_args && (_args += chartorune(&_argc, _args)))\ 384 switch(_argc) 385 #define ARGEND SET(_argt);USED(_argt);USED(_argc);USED(_args);}USED(argv);USED(argc); 386 #define ARGF() (_argt=_args, _args="",\ 387 (*_argt? _argt: argv[1]? (argc--, *++argv): 0)) 388 #define EARGF(x) (_argt=_args, _args="",\ 389 (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) 390 391 #define ARGC() _argc 392 393 #if defined(__cplusplus) 394 } 395 #endif 396 #endif /* _LIB9_H_ */