Showing the changes to Linux (21 of 9462 files). Show all
Unified · Split
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
| @@ -0,0 +1,33 @@ | ||
| 1 | +/* | |
| 2 | + * system- and machine-specific declarations for emu: | |
| 3 | + * floating-point save and restore, signal handling primitive, and | |
| 4 | + * implementation of the current-process variable `up'. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +/* | |
| 8 | + * This structure must agree with FPsave and FPrestore asm routines | |
| 9 | + */ | |
| 10 | +typedef struct FPU FPU; | |
| 11 | +struct FPU | |
| 12 | +{ | |
| 13 | + uchar env[28]; | |
| 14 | +}; | |
| 15 | + | |
| 16 | +#define KSTACK (32 * 1024) | |
| 17 | + | |
| 18 | +#ifndef USE_PTHREADS | |
| 19 | +static __inline Proc *getup(void) { | |
| 20 | + Proc *p; | |
| 21 | + __asm__( "movl %%esp, %%eax\n\t" | |
| 22 | + : "=a" (p) | |
| 23 | + ); | |
| 24 | + return *(Proc **)((uintptr)p & ~(KSTACK - 1)); | |
| 25 | +} | |
| 26 | +#else | |
| 27 | +extern Proc* getup(void); | |
| 28 | +#endif | |
| 29 | + | |
| 30 | +#define up (getup()) | |
| 31 | + | |
| 32 | +typedef sigjmp_buf osjmpbuf; | |
| 33 | +#define ossetjmp(buf) sigsetjmp(buf, 1) | |
| @@ -0,0 +1,76 @@ | ||
| 1 | +/* | |
| 2 | + * Linux 386 fpu support | |
| 3 | + * Mimic Plan9 floating point support | |
| 4 | + */ | |
| 5 | + | |
| 6 | +static void | |
| 7 | +setfcr(ulong fcr) | |
| 8 | +{ | |
| 9 | + __asm__( "xorb $0x3f, %%al\n\t" | |
| 10 | + "pushw %%ax\n\t" | |
| 11 | + "fwait\n\t" | |
| 12 | + "fldcw (%%esp)\n\t" | |
| 13 | + "popw %%ax\n\t" | |
| 14 | + : /* no output */ | |
| 15 | + : "al" (fcr) | |
| 16 | + ); | |
| 17 | +} | |
| 18 | + | |
| 19 | +static ulong | |
| 20 | +getfcr(void) | |
| 21 | +{ | |
| 22 | + ulong fcr = 0; | |
| 23 | + | |
| 24 | + __asm__( "pushl %%eax\n\t" | |
| 25 | + "fwait\n\t" | |
| 26 | + "fstcw (%%esp)\n\t" | |
| 27 | + "popl %%eax\n\t" | |
| 28 | + "xorb $0x3f, %%al\n\t" | |
| 29 | + : "=a" (fcr) | |
| 30 | + : "eax" (fcr) | |
| 31 | + ); | |
| 32 | + return fcr; | |
| 33 | +} | |
| 34 | + | |
| 35 | +static ulong | |
| 36 | +getfsr(void) | |
| 37 | +{ | |
| 38 | + ulong fsr = -1; | |
| 39 | + | |
| 40 | + __asm__( "fwait\n\t" | |
| 41 | + "fstsw (%%eax)\n\t" | |
| 42 | + "movl (%%eax), %%eax\n\t" | |
| 43 | + "andl $0xffff, %%eax\n\t" | |
| 44 | + : "=a" (fsr) | |
| 45 | + : "eax" (&fsr) | |
| 46 | + ); | |
| 47 | + return fsr; | |
| 48 | +} | |
| 49 | + | |
| 50 | +static void | |
| 51 | +setfsr(ulong fsr) | |
| 52 | +{ | |
| 53 | + __asm__("fclex\n\t"); | |
| 54 | +} | |
| 55 | + | |
| 56 | +/* FCR */ | |
| 57 | +#define FPINEX (1<<5) | |
| 58 | +#define FPUNFL ((1<<4)|(1<<1)) | |
| 59 | +#define FPOVFL (1<<3) | |
| 60 | +#define FPZDIV (1<<2) | |
| 61 | +#define FPINVAL (1<<0) | |
| 62 | +#define FPRNR (0<<10) | |
| 63 | +#define FPRZ (3<<10) | |
| 64 | +#define FPRPINF (2<<10) | |
| 65 | +#define FPRNINF (1<<10) | |
| 66 | +#define FPRMASK (3<<10) | |
| 67 | +#define FPPEXT (3<<8) | |
| 68 | +#define FPPSGL (0<<8) | |
| 69 | +#define FPPDBL (2<<8) | |
| 70 | +#define FPPMASK (3<<8) | |
| 71 | +/* FSR */ | |
| 72 | +#define FPAINEX FPINEX | |
| 73 | +#define FPAOVFL FPOVFL | |
| 74 | +#define FPAUNFL FPUNFL | |
| 75 | +#define FPAZDIV FPZDIV | |
| 76 | +#define FPAINVAL FPINVAL | |
| @@ -0,0 +1,462 @@ | ||
| 1 | +/* define _BSD_SOURCE to use ISO C, POSIX, and 4.3BSD things. */ | |
| 2 | +#define USE_PTHREADS | |
| 3 | +#ifndef _BSD_SOURCE | |
| 4 | +#define _BSD_SOURCE | |
| 5 | +#endif | |
| 6 | +#define _XOPEN_SOURCE 500 | |
| 7 | +#define _LARGEFILE_SOURCE 1 | |
| 8 | +#define _LARGEFILE64_SOURCE 1 | |
| 9 | +#define _FILE_OFFSET_BITS 64 | |
| 10 | +#ifdef USE_PTHREADS | |
| 11 | +#define _REENTRANT 1 | |
| 12 | +#endif | |
| 13 | +#include <features.h> | |
| 14 | +#include <sys/types.h> | |
| 15 | +#include <stdlib.h> | |
| 16 | +#include <stdarg.h> | |
| 17 | +#define sync __os_sync | |
| 18 | +#include <unistd.h> | |
| 19 | +#undef sync | |
| 20 | +#include <errno.h> | |
| 21 | +#define __NO_STRING_INLINES | |
| 22 | +#include <string.h> | |
| 23 | +#include "math.h" | |
| 24 | +#include <fcntl.h> | |
| 25 | +#include <setjmp.h> | |
| 26 | +#include <float.h> | |
| 27 | +#include <endian.h> | |
| 28 | + | |
| 29 | +#define getwd infgetwd | |
| 30 | + | |
| 31 | +#ifndef EMU | |
| 32 | +typedef struct Proc Proc; | |
| 33 | +#endif | |
| 34 | + | |
| 35 | +/* | |
| 36 | + * math module dtoa | |
| 37 | + * #define __LITTLE_ENDIAN /usr/include/endian.h under linux | |
| 38 | + */ | |
| 39 | + | |
| 40 | +#define nil ((void*)0) | |
| 41 | + | |
| 42 | +typedef unsigned char uchar; | |
| 43 | +typedef signed char schar; | |
| 44 | +typedef unsigned short Rune; | |
| 45 | +typedef long long int vlong; | |
| 46 | +typedef unsigned long long int uvlong; | |
| 47 | +typedef unsigned int u32int; | |
| 48 | +typedef uvlong u64int; | |
| 49 | + | |
| 50 | +typedef unsigned int mpdigit; /* for /sys/include/mp.h */ | |
| 51 | +typedef unsigned short u16int; | |
| 52 | +typedef unsigned char u8int; | |
| 53 | +typedef unsigned long uintptr; | |
| 54 | + | |
| 55 | +#define USED(x) if(x){}else{} | |
| 56 | +#define SET(x) | |
| 57 | + | |
| 58 | +#undef nelem | |
| 59 | +#define nelem(x) (sizeof(x)/sizeof((x)[0])) | |
| 60 | +#undef offsetof | |
| 61 | +#define offsetof(s, m) (ulong)(&(((s*)0)->m)) | |
| 62 | +#undef assert | |
| 63 | +#define assert(x) if(x){}else _assert("x") | |
| 64 | + | |
| 65 | +/* | |
| 66 | + * most mem and string routines are declared by ANSI/POSIX files above | |
| 67 | + */ | |
| 68 | + | |
| 69 | +extern char* strecpy(char*, char*, char*); | |
| 70 | +extern char* strdup(const char*); | |
| 71 | +extern int cistrncmp(char*, char*, int); | |
| 72 | +extern int cistrcmp(char*, char*); | |
| 73 | +extern char* cistrstr(char*, char*); | |
| 74 | +extern int tokenize(char*, char**, int); | |
| 75 | + | |
| 76 | +enum | |
| 77 | +{ | |
| 78 | + UTFmax = 3, /* maximum bytes per rune */ | |
| 79 | + Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */ | |
| 80 | + Runeself = 0x80, /* rune and UTF sequences are the same (<) */ | |
| 81 | + Runeerror = 0x80 /* decoding error in UTF */ | |
| 82 | +}; | |
| 83 | + | |
| 84 | +/* | |
| 85 | + * rune routines | |
| 86 | + */ | |
| 87 | +extern int runetochar(char*, Rune*); | |
| 88 | +extern int chartorune(Rune*, char*); | |
| 89 | +extern int runelen(long); | |
| 90 | +extern int runenlen(Rune*, int); | |
| 91 | +extern int fullrune(char*, int); | |
| 92 | +extern int utflen(char*); | |
| 93 | +extern int utfnlen(char*, long); | |
| 94 | +extern char* utfrune(char*, long); | |
| 95 | +extern char* utfrrune(char*, long); | |
| 96 | +extern char* utfutf(char*, char*); | |
| 97 | +extern char* utfecpy(char*, char*, char*); | |
| 98 | + | |
| 99 | +extern Rune* runestrcat(Rune*, Rune*); | |
| 100 | +extern Rune* runestrchr(Rune*, Rune); | |
| 101 | +extern int runestrcmp(Rune*, Rune*); | |
| 102 | +extern Rune* runestrcpy(Rune*, Rune*); | |
| 103 | +extern Rune* runestrncpy(Rune*, Rune*, long); | |
| 104 | +extern Rune* runestrecpy(Rune*, Rune*, Rune*); | |
| 105 | +extern Rune* runestrdup(Rune*); | |
| 106 | +extern Rune* runestrncat(Rune*, Rune*, long); | |
| 107 | +extern int runestrncmp(Rune*, Rune*, long); | |
| 108 | +extern Rune* runestrrchr(Rune*, Rune); | |
| 109 | +extern long runestrlen(Rune*); | |
| 110 | +extern Rune* runestrstr(Rune*, Rune*); | |
| 111 | + | |
| 112 | +extern Rune tolowerrune(Rune); | |
| 113 | +extern Rune totitlerune(Rune); | |
| 114 | +extern Rune toupperrune(Rune); | |
| 115 | +extern int isalpharune(Rune); | |
| 116 | +extern int islowerrune(Rune); | |
| 117 | +extern int isspacerune(Rune); | |
| 118 | +extern int istitlerune(Rune); | |
| 119 | +extern int isupperrune(Rune); | |
| 120 | + | |
| 121 | +/* | |
| 122 | + * malloc | |
| 123 | + */ | |
| 124 | +extern void* malloc(size_t); | |
| 125 | +extern void* mallocz(ulong, int); | |
| 126 | +extern void free(void*); | |
| 127 | +extern ulong msize(void*); | |
| 128 | +extern void* calloc(size_t, size_t); | |
| 129 | +extern void* realloc(void*, size_t); | |
| 130 | +extern void setmalloctag(void*, ulong); | |
| 131 | +extern void setrealloctag(void*, ulong); | |
| 132 | +extern ulong getmalloctag(void*); | |
| 133 | +extern ulong getrealloctag(void*); | |
| 134 | +extern void* malloctopoolblock(void*); | |
| 135 | + | |
| 136 | +/* | |
| 137 | + * print routines | |
| 138 | + */ | |
| 139 | +typedef struct Fmt Fmt; | |
| 140 | +struct Fmt{ | |
| 141 | + uchar runes; /* output buffer is runes or chars? */ | |
| 142 | + void *start; /* of buffer */ | |
| 143 | + void *to; /* current place in the buffer */ | |
| 144 | + void *stop; /* end of the buffer; overwritten if flush fails */ | |
| 145 | + int (*flush)(Fmt *); /* called when to == stop */ | |
| 146 | + void *farg; /* to make flush a closure */ | |
| 147 | + int nfmt; /* num chars formatted so far */ | |
| 148 | + va_list args; /* args passed to dofmt */ | |
| 149 | + int r; /* % format Rune */ | |
| 150 | + int width; | |
| 151 | + int prec; | |
| 152 | + ulong flags; | |
| 153 | +}; | |
| 154 | + | |
| 155 | +enum{ | |
| 156 | + FmtWidth = 1, | |
| 157 | + FmtLeft = FmtWidth << 1, | |
| 158 | + FmtPrec = FmtLeft << 1, | |
| 159 | + FmtSharp = FmtPrec << 1, | |
| 160 | + FmtSpace = FmtSharp << 1, | |
| 161 | + FmtSign = FmtSpace << 1, | |
| 162 | + FmtZero = FmtSign << 1, | |
| 163 | + FmtUnsigned = FmtZero << 1, | |
| 164 | + FmtShort = FmtUnsigned << 1, | |
| 165 | + FmtLong = FmtShort << 1, | |
| 166 | + FmtVLong = FmtLong << 1, | |
| 167 | + FmtComma = FmtVLong << 1, | |
| 168 | + FmtByte = FmtComma << 1, | |
| 169 | + | |
| 170 | + FmtFlag = FmtByte << 1 | |
| 171 | +}; | |
| 172 | + | |
| 173 | +extern int print(char*, ...); | |
| 174 | +extern char* seprint(char*, char*, char*, ...); | |
| 175 | +extern char* vseprint(char*, char*, char*, va_list); | |
| 176 | +extern int snprint(char*, int, char*, ...); | |
| 177 | +extern int vsnprint(char*, int, char*, va_list); | |
| 178 | +extern char* smprint(char*, ...); | |
| 179 | +extern char* vsmprint(char*, va_list); | |
| 180 | +extern int sprint(char*, char*, ...); | |
| 181 | +extern int fprint(int, char*, ...); | |
| 182 | +extern int vfprint(int, char*, va_list); | |
| 183 | + | |
| 184 | +extern int runesprint(Rune*, char*, ...); | |
| 185 | +extern int runesnprint(Rune*, int, char*, ...); | |
| 186 | +extern int runevsnprint(Rune*, int, char*, va_list); | |
| 187 | +extern Rune* runeseprint(Rune*, Rune*, char*, ...); | |
| 188 | +extern Rune* runevseprint(Rune*, Rune*, char*, va_list); | |
| 189 | +extern Rune* runesmprint(char*, ...); | |
| 190 | +extern Rune* runevsmprint(char*, va_list); | |
| 191 | + | |
| 192 | +extern int fmtfdinit(Fmt*, int, char*, int); | |
| 193 | +extern int fmtfdflush(Fmt*); | |
| 194 | +extern int fmtstrinit(Fmt*); | |
| 195 | +extern char* fmtstrflush(Fmt*); | |
| 196 | +extern int runefmtstrinit(Fmt*); | |
| 197 | +extern Rune* runefmtstrflush(Fmt*); | |
| 198 | + | |
| 199 | +extern int fmtinstall(int, int (*)(Fmt*)); | |
| 200 | +extern int dofmt(Fmt*, char*); | |
| 201 | +extern int dorfmt(Fmt*, Rune*); | |
| 202 | +extern int fmtprint(Fmt*, char*, ...); | |
| 203 | +extern int fmtvprint(Fmt*, char*, va_list); | |
| 204 | +extern int fmtrune(Fmt*, int); | |
| 205 | +extern int fmtstrcpy(Fmt*, char*); | |
| 206 | +extern int fmtrunestrcpy(Fmt*, Rune*); | |
| 207 | +/* | |
| 208 | + * error string for %r | |
| 209 | + * supplied on per os basis, not part of fmt library | |
| 210 | + */ | |
| 211 | +extern int errfmt(Fmt *f); | |
| 212 | + | |
| 213 | +/* | |
| 214 | + * quoted strings | |
| 215 | + */ | |
| 216 | +extern char *unquotestrdup(char*); | |
| 217 | +extern Rune *unquoterunestrdup(Rune*); | |
| 218 | +extern char *quotestrdup(char*); | |
| 219 | +extern Rune *quoterunestrdup(Rune*); | |
| 220 | +extern int quotestrfmt(Fmt*); | |
| 221 | +extern int quoterunestrfmt(Fmt*); | |
| 222 | +extern void quotefmtinstall(void); | |
| 223 | +extern int (*doquote)(int); | |
| 224 | + | |
| 225 | +/* | |
| 226 | + * random number | |
| 227 | + */ | |
| 228 | +extern ulong truerand(void); | |
| 229 | +extern ulong ntruerand(ulong); | |
| 230 | + | |
| 231 | +/* | |
| 232 | + * math | |
| 233 | + */ | |
| 234 | +extern int isNaN(double); | |
| 235 | +extern int isInf(double, int); | |
| 236 | + | |
| 237 | +/* | |
| 238 | + * Time-of-day | |
| 239 | + */ | |
| 240 | + | |
| 241 | +typedef struct Tm Tm; | |
| 242 | +struct Tm { | |
| 243 | + int sec; | |
| 244 | + int min; | |
| 245 | + int hour; | |
| 246 | + int mday; | |
| 247 | + int mon; | |
| 248 | + int year; | |
| 249 | + int wday; | |
| 250 | + int yday; | |
| 251 | + char zone[4]; | |
| 252 | + int tzoff; | |
| 253 | +}; | |
| 254 | +extern vlong osnsec(void); | |
| 255 | +#define nsec osnsec | |
| 256 | + | |
| 257 | +/* | |
| 258 | + * one-of-a-kind | |
| 259 | + */ | |
| 260 | +extern void _assert(char*); | |
| 261 | +extern double charstod(int(*)(void*), void*); | |
| 262 | +extern char* cleanname(char*); | |
| 263 | +extern uintptr getcallerpc(void*); | |
| 264 | +extern int getfields(char*, char**, int, int, char*); | |
| 265 | +extern char* getuser(void); | |
| 266 | +extern char* getwd(char*, int); | |
| 267 | +extern double ipow10(int); | |
| 268 | +#define pow10 infpow10 | |
| 269 | +extern double pow10(int); | |
| 270 | +extern vlong strtoll(const char*, char**, int); | |
| 271 | +#define qsort infqsort | |
| 272 | +extern void qsort(void*, long, long, int (*)(void*, void*)); | |
| 273 | +extern uvlong strtoull(const char*, char**, int); | |
| 274 | +extern void sysfatal(char*, ...); | |
| 275 | +extern int dec64(uchar*, int, char*, int); | |
| 276 | +extern int enc64(char*, int, uchar*, int); | |
| 277 | +extern int dec32(uchar*, int, char*, int); | |
| 278 | +extern int enc32(char*, int, uchar*, int); | |
| 279 | +extern int dec16(uchar*, int, char*, int); | |
| 280 | +extern int enc16(char*, int, uchar*, int); | |
| 281 | +extern int encodefmt(Fmt*); | |
| 282 | + | |
| 283 | +/* | |
| 284 | + * synchronization | |
| 285 | + */ | |
| 286 | +typedef | |
| 287 | +struct Lock { | |
| 288 | + int val; | |
| 289 | + int pid; | |
| 290 | +} Lock; | |
| 291 | + | |
| 292 | +extern int _tas(int*); | |
| 293 | + | |
| 294 | +extern void lock(Lock*); | |
| 295 | +extern void unlock(Lock*); | |
| 296 | +extern int canlock(Lock*); | |
| 297 | + | |
| 298 | +typedef struct QLock QLock; | |
| 299 | +struct QLock | |
| 300 | +{ | |
| 301 | + Lock use; /* to access Qlock structure */ | |
| 302 | + Proc *head; /* next process waiting for object */ | |
| 303 | + Proc *tail; /* last process waiting for object */ | |
| 304 | + int locked; /* flag */ | |
| 305 | +}; | |
| 306 | + | |
| 307 | +extern void qlock(QLock*); | |
| 308 | +extern void qunlock(QLock*); | |
| 309 | +extern int canqlock(QLock*); | |
| 310 | +extern void _qlockinit(ulong (*)(ulong, ulong)); /* called only by the thread library */ | |
| 311 | + | |
| 312 | +typedef | |
| 313 | +struct RWLock | |
| 314 | +{ | |
| 315 | + Lock l; /* Lock modify lock */ | |
| 316 | + QLock x; /* Mutual exclusion lock */ | |
| 317 | + QLock k; /* Lock for waiting writers */ | |
| 318 | + int readers; /* Count of readers in lock */ | |
| 319 | +} RWLock; | |
| 320 | + | |
| 321 | +extern int canrlock(RWLock*); | |
| 322 | +extern int canwlock(RWLock*); | |
| 323 | +extern void rlock(RWLock*); | |
| 324 | +extern void runlock(RWLock*); | |
| 325 | +extern void wlock(RWLock*); | |
| 326 | +extern void wunlock(RWLock*); | |
| 327 | + | |
| 328 | +/* | |
| 329 | + * network dialing | |
| 330 | + */ | |
| 331 | +#define NETPATHLEN 40 | |
| 332 | + | |
| 333 | +/* | |
| 334 | + * system calls | |
| 335 | + * | |
| 336 | + */ | |
| 337 | +#define STATMAX 65535U /* max length of machine-independent stat structure */ | |
| 338 | +#define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */ | |
| 339 | +#define ERRMAX 128 /* max length of error string */ | |
| 340 | + | |
| 341 | +#define MORDER 0x0003 /* mask for bits defining order of mounting */ | |
| 342 | +#define MREPL 0x0000 /* mount replaces object */ | |
| 343 | +#define MBEFORE 0x0001 /* mount goes before others in union directory */ | |
| 344 | +#define MAFTER 0x0002 /* mount goes after others in union directory */ | |
| 345 | +#define MCREATE 0x0004 /* permit creation in mounted directory */ | |
| 346 | +#define MCACHE 0x0010 /* cache some data */ | |
| 347 | +#define MMASK 0x0017 /* all bits on */ | |
| 348 | + | |
| 349 | +#define OREAD 0 /* open for read */ | |
| 350 | +#define OWRITE 1 /* write */ | |
| 351 | +#define ORDWR 2 /* read and write */ | |
| 352 | +#define OEXEC 3 /* execute, == read but check execute permission */ | |
| 353 | +#define OTRUNC 16 /* or'ed in (except for exec), truncate file first */ | |
| 354 | +#define OCEXEC 32 /* or'ed in, close on exec */ | |
| 355 | +#define ORCLOSE 64 /* or'ed in, remove on close */ | |
| 356 | +#define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */ | |
| 357 | + | |
| 358 | +#define AEXIST 0 /* accessible: exists */ | |
| 359 | +#define AEXEC 1 /* execute access */ | |
| 360 | +#define AWRITE 2 /* write access */ | |
| 361 | +#define AREAD 4 /* read access */ | |
| 362 | + | |
| 363 | +/* bits in Qid.type */ | |
| 364 | +#define QTDIR 0x80 /* type bit for directories */ | |
| 365 | +#define QTAPPEND 0x40 /* type bit for append only files */ | |
| 366 | +#define QTEXCL 0x20 /* type bit for exclusive use files */ | |
| 367 | +#define QTMOUNT 0x10 /* type bit for mounted channel */ | |
| 368 | +#define QTAUTH 0x08 /* type bit for authentication file */ | |
| 369 | +#define QTFILE 0x00 /* plain file */ | |
| 370 | + | |
| 371 | +/* bits in Dir.mode */ | |
| 372 | +#define DMDIR 0x80000000 /* mode bit for directories */ | |
| 373 | +#define DMAPPEND 0x40000000 /* mode bit for append only files */ | |
| 374 | +#define DMEXCL 0x20000000 /* mode bit for exclusive use files */ | |
| 375 | +#define DMMOUNT 0x10000000 /* mode bit for mounted channel */ | |
| 376 | +#define DMAUTH 0x08000000 /* mode bit for authentication file */ | |
| 377 | +#define DMREAD 0x4 /* mode bit for read permission */ | |
| 378 | +#define DMWRITE 0x2 /* mode bit for write permission */ | |
| 379 | +#define DMEXEC 0x1 /* mode bit for execute permission */ | |
| 380 | + | |
| 381 | +typedef | |
| 382 | +struct Qid | |
| 383 | +{ | |
| 384 | + uvlong path; | |
| 385 | + ulong vers; | |
| 386 | + uchar type; | |
| 387 | +} Qid; | |
| 388 | + | |
| 389 | +typedef | |
| 390 | +struct Dir { | |
| 391 | + /* system-modified data */ | |
| 392 | + ushort type; /* server type */ | |
| 393 | + uint dev; /* server subtype */ | |
| 394 | + /* file data */ | |
| 395 | + Qid qid; /* unique id from server */ | |
| 396 | + ulong mode; /* permissions */ | |
| 397 | + ulong atime; /* last read time */ | |
| 398 | + ulong mtime; /* last write time */ | |
| 399 | + vlong length; /* file length */ | |
| 400 | + char *name; /* last element of path */ | |
| 401 | + char *uid; /* owner name */ | |
| 402 | + char *gid; /* group name */ | |
| 403 | + char *muid; /* last modifier name */ | |
| 404 | +} Dir; | |
| 405 | + | |
| 406 | +extern Dir* dirstat(char*); | |
| 407 | +extern Dir* dirfstat(int); | |
| 408 | +extern int dirwstat(char*, Dir*); | |
| 409 | +extern int dirfwstat(int, Dir*); | |
| 410 | +extern long dirread(int, Dir**); | |
| 411 | +extern void nulldir(Dir*); | |
| 412 | +extern long dirreadall(int, Dir**); | |
| 413 | + | |
| 414 | +typedef | |
| 415 | +struct Waitmsg | |
| 416 | +{ | |
| 417 | + int pid; /* of loved one */ | |
| 418 | + ulong time[3]; /* of loved one & descendants */ | |
| 419 | + char *msg; | |
| 420 | +} Waitmsg; | |
| 421 | + | |
| 422 | +extern void _exits(char*); | |
| 423 | + | |
| 424 | +extern void exits(char*); | |
| 425 | +extern int create(char*, int, int); | |
| 426 | +extern int errstr(char*, uint); | |
| 427 | + | |
| 428 | +extern void perror(const char*); | |
| 429 | +extern long readn(int, void*, long); | |
| 430 | +extern int remove(const char*); | |
| 431 | +extern void rerrstr(char*, uint); | |
| 432 | +extern vlong seek(int, vlong, int); | |
| 433 | +extern int segflush(void*, ulong); | |
| 434 | +extern void werrstr(char*, ...); | |
| 435 | + | |
| 436 | +extern char *argv0; | |
| 437 | +#define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ | |
| 438 | + argv[0] && argv[0][0]=='-' && argv[0][1];\ | |
| 439 | + argc--, argv++) {\ | |
| 440 | + char *_args, *_argt;\ | |
| 441 | + Rune _argc;\ | |
| 442 | + _args = &argv[0][1];\ | |
| 443 | + if(_args[0]=='-' && _args[1]==0){\ | |
| 444 | + argc--; argv++; break;\ | |
| 445 | + }\ | |
| 446 | + _argc = 0;\ | |
| 447 | + while(*_args && (_args += chartorune(&_argc, _args)))\ | |
| 448 | + switch(_argc) | |
| 449 | +#define ARGEND SET(_argt);USED(_argt);USED(_argc); USED(_args);}USED(argv); USED(argc); | |
| 450 | +#define ARGF() (_argt=_args, _args="",\ | |
| 451 | + (*_argt? _argt: argv[1]? (argc--, *++argv): 0)) | |
| 452 | +#define EARGF(x) (_argt=_args, _args="",\ | |
| 453 | + (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) | |
| 454 | + | |
| 455 | +#define ARGC() _argc | |
| 456 | + | |
| 457 | +/* | |
| 458 | + * Extensions for Inferno to basic libc.h | |
| 459 | + */ | |
| 460 | + | |
| 461 | +#define setbinmode() | |
| 462 | + | |
| @@ -0,0 +1,34 @@ | ||
| 1 | +/* | |
| 2 | + * system- and machine-specific declarations for emu: | |
| 3 | + * floating-point save and restore, signal handling primitive, and | |
| 4 | + * implementation of the current-process variable `up'. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +/* | |
| 8 | + * This structure must agree with FPsave and FPrestore asm routines | |
| 9 | + */ | |
| 10 | +typedef struct FPU FPU; | |
| 11 | +struct FPU | |
| 12 | +{ | |
| 13 | + uchar env[28]; | |
| 14 | +}; | |
| 15 | + | |
| 16 | + | |
| 17 | +#ifndef USE_PTHREADS | |
| 18 | +#define KSTACK (16 * 1024) /* must be power of two */ | |
| 19 | +static __inline Proc *getup(void) { | |
| 20 | + Proc *p; | |
| 21 | + __asm__( "mov %0, %%sp;" | |
| 22 | + : "=r" (p) | |
| 23 | + ); | |
| 24 | + return *(Proc **)((uintptr)p & ~(KSTACK - 1)); | |
| 25 | +} | |
| 26 | +#else | |
| 27 | +#define KSTACK (32 * 1024) /* need not be power of two */ | |
| 28 | +extern Proc* getup(void); | |
| 29 | +#endif | |
| 30 | + | |
| 31 | +#define up (getup()) | |
| 32 | + | |
| 33 | +typedef sigjmp_buf osjmpbuf; | |
| 34 | +#define ossetjmp(buf) sigsetjmp(buf, 1) | |
| @@ -0,0 +1,52 @@ | ||
| 1 | +/* | |
| 2 | + * Linux arm fpu support | |
| 3 | + * Mimic Plan9 floating point support | |
| 4 | + */ | |
| 5 | + | |
| 6 | +#include <fenv.h> | |
| 7 | + | |
| 8 | +static void | |
| 9 | +setfcr(ulong fcr) | |
| 10 | +{ | |
| 11 | +} | |
| 12 | + | |
| 13 | +static ulong | |
| 14 | +getfcr(void) | |
| 15 | +{ | |
| 16 | + ulong fcr = 0; | |
| 17 | + return fcr; | |
| 18 | +} | |
| 19 | + | |
| 20 | +static ulong | |
| 21 | +getfsr(void) | |
| 22 | +{ | |
| 23 | + ulong fsr = -1; | |
| 24 | + return fsr; | |
| 25 | +} | |
| 26 | + | |
| 27 | +static void | |
| 28 | +setfsr(ulong fsr) | |
| 29 | +{ | |
| 30 | +} | |
| 31 | + | |
| 32 | +/* FCR */ | |
| 33 | +#define FPINEX (1<<5) | |
| 34 | +#define FPUNFL ((1<<4)|(1<<1)) | |
| 35 | +#define FPOVFL (1<<3) | |
| 36 | +#define FPZDIV (1<<2) | |
| 37 | +#define FPINVAL (1<<0) | |
| 38 | +#define FPRNR (0<<10) | |
| 39 | +#define FPRZ (3<<10) | |
| 40 | +#define FPRPINF (2<<10) | |
| 41 | +#define FPRNINF (1<<10) | |
| 42 | +#define FPRMASK (3<<10) | |
| 43 | +#define FPPEXT (3<<8) | |
| 44 | +#define FPPSGL (0<<8) | |
| 45 | +#define FPPDBL (2<<8) | |
| 46 | +#define FPPMASK (3<<8) | |
| 47 | +/* FSR */ | |
| 48 | +#define FPAINEX FPINEX | |
| 49 | +#define FPAOVFL FPOVFL | |
| 50 | +#define FPAUNFL FPUNFL | |
| 51 | +#define FPAZDIV FPZDIV | |
| 52 | +#define FPAINVAL FPINVAL | |
| @@ -0,0 +1,470 @@ | ||
| 1 | +/* define _BSD_SOURCE to use ISO C, POSIX, and 4.3BSD things. */ | |
| 2 | +#define USE_PTHREADS | |
| 3 | +#ifndef _BSD_SOURCE | |
| 4 | +#define _BSD_SOURCE | |
| 5 | +#endif | |
| 6 | +#define _XOPEN_SOURCE 500 | |
| 7 | +#define _LARGEFILE_SOURCE 1 | |
| 8 | +#define _LARGEFILE64_SOURCE 1 | |
| 9 | +#define _FILE_OFFSET_BITS 64 | |
| 10 | +#ifdef USE_PTHREADS | |
| 11 | +#define _REENTRANT 1 | |
| 12 | +#endif | |
| 13 | +#include <features.h> | |
| 14 | +#include <sys/types.h> | |
| 15 | +#include <stdlib.h> | |
| 16 | +#include <stdarg.h> | |
| 17 | +#define sync __os_sync | |
| 18 | +#include <unistd.h> | |
| 19 | +#undef sync | |
| 20 | +#include <errno.h> | |
| 21 | +#define __NO_STRING_INLINES | |
| 22 | +#include <string.h> | |
| 23 | +#include "math.h" | |
| 24 | +#include <fcntl.h> | |
| 25 | +#include <setjmp.h> | |
| 26 | +#include <float.h> | |
| 27 | +#include <endian.h> | |
| 28 | + | |
| 29 | +#define getwd infgetwd | |
| 30 | + | |
| 31 | +#ifndef EMU | |
| 32 | +typedef struct Proc Proc; | |
| 33 | +#endif | |
| 34 | + | |
| 35 | +/* | |
| 36 | + * math module dtoa | |
| 37 | + * #define __LITTLE_ENDIAN /usr/include/endian.h under linux | |
| 38 | + */ | |
| 39 | + | |
| 40 | +#define nil ((void*)0) | |
| 41 | + | |
| 42 | +typedef unsigned char uchar; | |
| 43 | +typedef signed char schar; | |
| 44 | +typedef unsigned short Rune; | |
| 45 | +typedef long long int vlong; | |
| 46 | +typedef unsigned long long int uvlong; | |
| 47 | +typedef unsigned int u32int; | |
| 48 | +typedef uvlong u64int; | |
| 49 | + | |
| 50 | +typedef unsigned int mpdigit; /* for /sys/include/mp.h */ | |
| 51 | +typedef unsigned short u16int; | |
| 52 | +typedef unsigned char u8int; | |
| 53 | +typedef unsigned long uintptr; | |
| 54 | + | |
| 55 | +#define USED(x) if(x){}else{} | |
| 56 | +#define SET(x) | |
| 57 | + | |
| 58 | +#undef nelem | |
| 59 | +#define nelem(x) (sizeof(x)/sizeof((x)[0])) | |
| 60 | +#undef offsetof | |
| 61 | +#define offsetof(s, m) (ulong)(&(((s*)0)->m)) | |
| 62 | +#undef assert | |
| 63 | +#define assert(x) if(x){}else _assert("x") | |
| 64 | + | |
| 65 | +/* | |
| 66 | + * most mem and string routines are declared by ANSI/POSIX files above | |
| 67 | + */ | |
| 68 | + | |
| 69 | +extern char* strecpy(char*, char*, char*); | |
| 70 | +extern char* strdup(const char*); | |
| 71 | +extern int cistrncmp(char*, char*, int); | |
| 72 | +extern int cistrcmp(char*, char*); | |
| 73 | +extern char* cistrstr(char*, char*); | |
| 74 | +extern int tokenize(char*, char**, int); | |
| 75 | + | |
| 76 | +enum | |
| 77 | +{ | |
| 78 | + UTFmax = 3, /* maximum bytes per rune */ | |
| 79 | + Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */ | |
| 80 | + Runeself = 0x80, /* rune and UTF sequences are the same (<) */ | |
| 81 | + Runeerror = 0x80 /* decoding error in UTF */ | |
| 82 | +}; | |
| 83 | + | |
| 84 | +/* | |
| 85 | + * rune routines | |
| 86 | + */ | |
| 87 | +extern int runetochar(char*, Rune*); | |
| 88 | +extern int chartorune(Rune*, char*); | |
| 89 | +extern int runelen(long); | |
| 90 | +extern int runenlen(Rune*, int); | |
| 91 | +extern int fullrune(char*, int); | |
| 92 | +extern int utflen(char*); | |
| 93 | +extern int utfnlen(char*, long); | |
| 94 | +extern char* utfrune(char*, long); | |
| 95 | +extern char* utfrrune(char*, long); | |
| 96 | +extern char* utfutf(char*, char*); | |
| 97 | +extern char* utfecpy(char*, char*, char*); | |
| 98 | + | |
| 99 | +extern Rune* runestrcat(Rune*, Rune*); | |
| 100 | +extern Rune* runestrchr(Rune*, Rune); | |
| 101 | +extern int runestrcmp(Rune*, Rune*); | |
| 102 | +extern Rune* runestrcpy(Rune*, Rune*); | |
| 103 | +extern Rune* runestrncpy(Rune*, Rune*, long); | |
| 104 | +extern Rune* runestrecpy(Rune*, Rune*, Rune*); | |
| 105 | +extern Rune* runestrdup(Rune*); | |
| 106 | +extern Rune* runestrncat(Rune*, Rune*, long); | |
| 107 | +extern int runestrncmp(Rune*, Rune*, long); | |
| 108 | +extern Rune* runestrrchr(Rune*, Rune); | |
| 109 | +extern long runestrlen(Rune*); | |
| 110 | +extern Rune* runestrstr(Rune*, Rune*); | |
| 111 | + | |
| 112 | +extern Rune tolowerrune(Rune); | |
| 113 | +extern Rune totitlerune(Rune); | |
| 114 | +extern Rune toupperrune(Rune); | |
| 115 | +extern int isalpharune(Rune); | |
| 116 | +extern int islowerrune(Rune); | |
| 117 | +extern int isspacerune(Rune); | |
| 118 | +extern int istitlerune(Rune); | |
| 119 | +extern int isupperrune(Rune); | |
| 120 | + | |
| 121 | +/* | |
| 122 | + * malloc | |
| 123 | + */ | |
| 124 | +extern void* malloc(size_t); | |
| 125 | +extern void* mallocz(ulong, int); | |
| 126 | +extern void free(void*); | |
| 127 | +extern ulong msize(void*); | |
| 128 | +extern void* calloc(size_t, size_t); | |
| 129 | +extern void* realloc(void*, size_t); | |
| 130 | +extern void setmalloctag(void*, ulong); | |
| 131 | +extern void setrealloctag(void*, ulong); | |
| 132 | +extern ulong getmalloctag(void*); | |
| 133 | +extern ulong getrealloctag(void*); | |
| 134 | +extern void* malloctopoolblock(void*); | |
| 135 | + | |
| 136 | +/* | |
| 137 | + * print routines | |
| 138 | + */ | |
| 139 | +typedef struct Fmt Fmt; | |
| 140 | +struct Fmt{ | |
| 141 | + uchar runes; /* output buffer is runes or chars? */ | |
| 142 | + void *start; /* of buffer */ | |
| 143 | + void *to; /* current place in the buffer */ | |
| 144 | + void *stop; /* end of the buffer; overwritten if flush fails */ | |
| 145 | + int (*flush)(Fmt *); /* called when to == stop */ | |
| 146 | + void *farg; /* to make flush a closure */ | |
| 147 | + int nfmt; /* num chars formatted so far */ | |
| 148 | + va_list args; /* args passed to dofmt */ | |
| 149 | + int r; /* % format Rune */ | |
| 150 | + int width; | |
| 151 | + int prec; | |
| 152 | + ulong flags; | |
| 153 | +}; | |
| 154 | + | |
| 155 | +enum{ | |
| 156 | + FmtWidth = 1, | |
| 157 | + FmtLeft = FmtWidth << 1, | |
| 158 | + FmtPrec = FmtLeft << 1, | |
| 159 | + FmtSharp = FmtPrec << 1, | |
| 160 | + FmtSpace = FmtSharp << 1, | |
| 161 | + FmtSign = FmtSpace << 1, | |
| 162 | + FmtZero = FmtSign << 1, | |
| 163 | + FmtUnsigned = FmtZero << 1, | |
| 164 | + FmtShort = FmtUnsigned << 1, | |
| 165 | + FmtLong = FmtShort << 1, | |
| 166 | + FmtVLong = FmtLong << 1, | |
| 167 | + FmtComma = FmtVLong << 1, | |
| 168 | + FmtByte = FmtComma << 1, | |
| 169 | + | |
| 170 | + FmtFlag = FmtByte << 1 | |
| 171 | +}; | |
| 172 | + | |
| 173 | +extern int print(char*, ...); | |
| 174 | +extern char* seprint(char*, char*, char*, ...); | |
| 175 | +extern char* vseprint(char*, char*, char*, va_list); | |
| 176 | +extern int snprint(char*, int, char*, ...); | |
| 177 | +extern int vsnprint(char*, int, char*, va_list); | |
| 178 | +extern char* smprint(char*, ...); | |
| 179 | +extern char* vsmprint(char*, va_list); | |
| 180 | +extern int sprint(char*, char*, ...); | |
| 181 | +extern int fprint(int, char*, ...); | |
| 182 | +extern int vfprint(int, char*, va_list); | |
| 183 | + | |
| 184 | +extern int runesprint(Rune*, char*, ...); | |
| 185 | +extern int runesnprint(Rune*, int, char*, ...); | |
| 186 | +extern int runevsnprint(Rune*, int, char*, va_list); | |
| 187 | +extern Rune* runeseprint(Rune*, Rune*, char*, ...); | |
| 188 | +extern Rune* runevseprint(Rune*, Rune*, char*, va_list); | |
| 189 | +extern Rune* runesmprint(char*, ...); | |
| 190 | +extern Rune* runevsmprint(char*, va_list); | |
| 191 | + | |
| 192 | +extern int fmtfdinit(Fmt*, int, char*, int); | |
| 193 | +extern int fmtfdflush(Fmt*); | |
| 194 | +extern int fmtstrinit(Fmt*); | |
| 195 | +extern char* fmtstrflush(Fmt*); | |
| 196 | +extern int runefmtstrinit(Fmt*); | |
| 197 | +extern Rune* runefmtstrflush(Fmt*); | |
| 198 | + | |
| 199 | +extern int fmtinstall(int, int (*)(Fmt*)); | |
| 200 | +extern int dofmt(Fmt*, char*); | |
| 201 | +extern int dorfmt(Fmt*, Rune*); | |
| 202 | +extern int fmtprint(Fmt*, char*, ...); | |
| 203 | +extern int fmtvprint(Fmt*, char*, va_list); | |
| 204 | +extern int fmtrune(Fmt*, int); | |
| 205 | +extern int fmtstrcpy(Fmt*, char*); | |
| 206 | +extern int fmtrunestrcpy(Fmt*, Rune*); | |
| 207 | +/* | |
| 208 | + * error string for %r | |
| 209 | + * supplied on per os basis, not part of fmt library | |
| 210 | + */ | |
| 211 | +extern int errfmt(Fmt *f); | |
| 212 | + | |
| 213 | +/* | |
| 214 | + * quoted strings | |
| 215 | + */ | |
| 216 | +extern char *unquotestrdup(char*); | |
| 217 | +extern Rune *unquoterunestrdup(Rune*); | |
| 218 | +extern char *quotestrdup(char*); | |
| 219 | +extern Rune *quoterunestrdup(Rune*); | |
| 220 | +extern int quotestrfmt(Fmt*); | |
| 221 | +extern int quoterunestrfmt(Fmt*); | |
| 222 | +extern void quotefmtinstall(void); | |
| 223 | +extern int (*doquote)(int); | |
| 224 | + | |
| 225 | +/* | |
| 226 | + * random number | |
| 227 | + */ | |
| 228 | +extern ulong truerand(void); | |
| 229 | +extern ulong ntruerand(ulong); | |
| 230 | + | |
| 231 | +/* | |
| 232 | + * math | |
| 233 | + */ | |
| 234 | +extern int isNaN(double); | |
| 235 | +extern int isInf(double, int); | |
| 236 | + | |
| 237 | +/* | |
| 238 | + * Time-of-day | |
| 239 | + */ | |
| 240 | + | |
| 241 | +typedef struct Tm Tm; | |
| 242 | +struct Tm { | |
| 243 | + int sec; | |
| 244 | + int min; | |
| 245 | + int hour; | |
| 246 | + int mday; | |
| 247 | + int mon; | |
| 248 | + int year; | |
| 249 | + int wday; | |
| 250 | + int yday; | |
| 251 | + char zone[4]; | |
| 252 | + int tzoff; | |
| 253 | +}; | |
| 254 | +extern vlong osnsec(void); | |
| 255 | +#define nsec osnsec | |
| 256 | + | |
| 257 | +/* | |
| 258 | + * one-of-a-kind | |
| 259 | + */ | |
| 260 | +extern void _assert(char*); | |
| 261 | +extern double charstod(int(*)(void*), void*); | |
| 262 | +extern char* cleanname(char*); | |
| 263 | +//extern uintptr getcallerpc(void*); | |
| 264 | +extern int getfields(char*, char**, int, int, char*); | |
| 265 | +extern char* getuser(void); | |
| 266 | +extern char* getwd(char*, int); | |
| 267 | +extern double ipow10(int); | |
| 268 | +#define pow10 infpow10 | |
| 269 | +extern double pow10(int); | |
| 270 | +extern vlong strtoll(const char*, char**, int); | |
| 271 | +#define qsort infqsort | |
| 272 | +extern void qsort(void*, long, long, int (*)(void*, void*)); | |
| 273 | +extern uvlong strtoull(const char*, char**, int); | |
| 274 | +extern void sysfatal(char*, ...); | |
| 275 | +extern int dec64(uchar*, int, char*, int); | |
| 276 | +extern int enc64(char*, int, uchar*, int); | |
| 277 | +extern int dec32(uchar*, int, char*, int); | |
| 278 | +extern int enc32(char*, int, uchar*, int); | |
| 279 | +extern int dec16(uchar*, int, char*, int); | |
| 280 | +extern int enc16(char*, int, uchar*, int); | |
| 281 | +extern int encodefmt(Fmt*); | |
| 282 | + | |
| 283 | +/* | |
| 284 | + * synchronization | |
| 285 | + */ | |
| 286 | +typedef | |
| 287 | +struct Lock { | |
| 288 | + int val; | |
| 289 | + int pid; | |
| 290 | +} Lock; | |
| 291 | + | |
| 292 | +extern int _tas(int*); | |
| 293 | + | |
| 294 | +extern void lock(Lock*); | |
| 295 | +extern void unlock(Lock*); | |
| 296 | +extern int canlock(Lock*); | |
| 297 | + | |
| 298 | +typedef struct QLock QLock; | |
| 299 | +struct QLock | |
| 300 | +{ | |
| 301 | + Lock use; /* to access Qlock structure */ | |
| 302 | + Proc *head; /* next process waiting for object */ | |
| 303 | + Proc *tail; /* last process waiting for object */ | |
| 304 | + int locked; /* flag */ | |
| 305 | +}; | |
| 306 | + | |
| 307 | +extern void qlock(QLock*); | |
| 308 | +extern void qunlock(QLock*); | |
| 309 | +extern int canqlock(QLock*); | |
| 310 | +extern void _qlockinit(ulong (*)(ulong, ulong)); /* called only by the thread library */ | |
| 311 | + | |
| 312 | +typedef | |
| 313 | +struct RWLock | |
| 314 | +{ | |
| 315 | + Lock l; /* Lock modify lock */ | |
| 316 | + QLock x; /* Mutual exclusion lock */ | |
| 317 | + QLock k; /* Lock for waiting writers */ | |
| 318 | + int readers; /* Count of readers in lock */ | |
| 319 | +} RWLock; | |
| 320 | + | |
| 321 | +extern int canrlock(RWLock*); | |
| 322 | +extern int canwlock(RWLock*); | |
| 323 | +extern void rlock(RWLock*); | |
| 324 | +extern void runlock(RWLock*); | |
| 325 | +extern void wlock(RWLock*); | |
| 326 | +extern void wunlock(RWLock*); | |
| 327 | + | |
| 328 | +/* | |
| 329 | + * network dialing | |
| 330 | + */ | |
| 331 | +#define NETPATHLEN 40 | |
| 332 | + | |
| 333 | +/* | |
| 334 | + * system calls | |
| 335 | + * | |
| 336 | + */ | |
| 337 | +#define STATMAX 65535U /* max length of machine-independent stat structure */ | |
| 338 | +#define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */ | |
| 339 | +#define ERRMAX 128 /* max length of error string */ | |
| 340 | + | |
| 341 | +#define MORDER 0x0003 /* mask for bits defining order of mounting */ | |
| 342 | +#define MREPL 0x0000 /* mount replaces object */ | |
| 343 | +#define MBEFORE 0x0001 /* mount goes before others in union directory */ | |
| 344 | +#define MAFTER 0x0002 /* mount goes after others in union directory */ | |
| 345 | +#define MCREATE 0x0004 /* permit creation in mounted directory */ | |
| 346 | +#define MCACHE 0x0010 /* cache some data */ | |
| 347 | +#define MMASK 0x0017 /* all bits on */ | |
| 348 | + | |
| 349 | +#define OREAD 0 /* open for read */ | |
| 350 | +#define OWRITE 1 /* write */ | |
| 351 | +#define ORDWR 2 /* read and write */ | |
| 352 | +#define OEXEC 3 /* execute, == read but check execute permission */ | |
| 353 | +#define OTRUNC 16 /* or'ed in (except for exec), truncate file first */ | |
| 354 | +#define OCEXEC 32 /* or'ed in, close on exec */ | |
| 355 | +#define ORCLOSE 64 /* or'ed in, remove on close */ | |
| 356 | +#define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */ | |
| 357 | + | |
| 358 | +#define AEXIST 0 /* accessible: exists */ | |
| 359 | +#define AEXEC 1 /* execute access */ | |
| 360 | +#define AWRITE 2 /* write access */ | |
| 361 | +#define AREAD 4 /* read access */ | |
| 362 | + | |
| 363 | +/* bits in Qid.type */ | |
| 364 | +#define QTDIR 0x80 /* type bit for directories */ | |
| 365 | +#define QTAPPEND 0x40 /* type bit for append only files */ | |
| 366 | +#define QTEXCL 0x20 /* type bit for exclusive use files */ | |
| 367 | +#define QTMOUNT 0x10 /* type bit for mounted channel */ | |
| 368 | +#define QTAUTH 0x08 /* type bit for authentication file */ | |
| 369 | +#define QTFILE 0x00 /* plain file */ | |
| 370 | + | |
| 371 | +/* bits in Dir.mode */ | |
| 372 | +#define DMDIR 0x80000000 /* mode bit for directories */ | |
| 373 | +#define DMAPPEND 0x40000000 /* mode bit for append only files */ | |
| 374 | +#define DMEXCL 0x20000000 /* mode bit for exclusive use files */ | |
| 375 | +#define DMMOUNT 0x10000000 /* mode bit for mounted channel */ | |
| 376 | +#define DMAUTH 0x08000000 /* mode bit for authentication file */ | |
| 377 | +#define DMREAD 0x4 /* mode bit for read permission */ | |
| 378 | +#define DMWRITE 0x2 /* mode bit for write permission */ | |
| 379 | +#define DMEXEC 0x1 /* mode bit for execute permission */ | |
| 380 | + | |
| 381 | +typedef | |
| 382 | +struct Qid | |
| 383 | +{ | |
| 384 | + uvlong path; | |
| 385 | + ulong vers; | |
| 386 | + uchar type; | |
| 387 | +} Qid; | |
| 388 | + | |
| 389 | +typedef | |
| 390 | +struct Dir { | |
| 391 | + /* system-modified data */ | |
| 392 | + ushort type; /* server type */ | |
| 393 | + uint dev; /* server subtype */ | |
| 394 | + /* file data */ | |
| 395 | + Qid qid; /* unique id from server */ | |
| 396 | + ulong mode; /* permissions */ | |
| 397 | + ulong atime; /* last read time */ | |
| 398 | + ulong mtime; /* last write time */ | |
| 399 | + vlong length; /* file length */ | |
| 400 | + char *name; /* last element of path */ | |
| 401 | + char *uid; /* owner name */ | |
| 402 | + char *gid; /* group name */ | |
| 403 | + char *muid; /* last modifier name */ | |
| 404 | +} Dir; | |
| 405 | + | |
| 406 | +extern Dir* dirstat(char*); | |
| 407 | +extern Dir* dirfstat(int); | |
| 408 | +extern int dirwstat(char*, Dir*); | |
| 409 | +extern int dirfwstat(int, Dir*); | |
| 410 | +extern long dirread(int, Dir**); | |
| 411 | +extern void nulldir(Dir*); | |
| 412 | +extern long dirreadall(int, Dir**); | |
| 413 | + | |
| 414 | +typedef | |
| 415 | +struct Waitmsg | |
| 416 | +{ | |
| 417 | + int pid; /* of loved one */ | |
| 418 | + ulong time[3]; /* of loved one & descendants */ | |
| 419 | + char *msg; | |
| 420 | +} Waitmsg; | |
| 421 | + | |
| 422 | +extern void _exits(char*); | |
| 423 | + | |
| 424 | +extern void exits(char*); | |
| 425 | +extern int create(char*, int, int); | |
| 426 | +extern int errstr(char*, uint); | |
| 427 | + | |
| 428 | +extern void perror(const char*); | |
| 429 | +extern long readn(int, void*, long); | |
| 430 | +extern int remove(const char*); | |
| 431 | +extern void rerrstr(char*, uint); | |
| 432 | +extern vlong seek(int, vlong, int); | |
| 433 | +extern int segflush(void*, ulong); | |
| 434 | +extern void werrstr(char*, ...); | |
| 435 | + | |
| 436 | +extern char *argv0; | |
| 437 | +#define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ | |
| 438 | + argv[0] && argv[0][0]=='-' && argv[0][1];\ | |
| 439 | + argc--, argv++) {\ | |
| 440 | + char *_args, *_argt;\ | |
| 441 | + Rune _argc;\ | |
| 442 | + _args = &argv[0][1];\ | |
| 443 | + if(_args[0]=='-' && _args[1]==0){\ | |
| 444 | + argc--; argv++; break;\ | |
| 445 | + }\ | |
| 446 | + _argc = 0;\ | |
| 447 | + while(*_args && (_args += chartorune(&_argc, _args)))\ | |
| 448 | + switch(_argc) | |
| 449 | +#define ARGEND SET(_argt);USED(_argt);USED(_argc); USED(_args);}USED(argv); USED(argc); | |
| 450 | +#define ARGF() (_argt=_args, _args="",\ | |
| 451 | + (*_argt? _argt: argv[1]? (argc--, *++argv): 0)) | |
| 452 | +#define EARGF(x) (_argt=_args, _args="",\ | |
| 453 | + (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) | |
| 454 | + | |
| 455 | +#define ARGC() _argc | |
| 456 | + | |
| 457 | +/* | |
| 458 | + * Extensions for Inferno to basic libc.h | |
| 459 | + */ | |
| 460 | + | |
| 461 | +#define setbinmode() | |
| 462 | + | |
| 463 | +/* need the inline because the link register is not saved in a known location */ | |
| 464 | +static __inline uintptr getcallerpc(void* dummy) { | |
| 465 | + ulong lr; | |
| 466 | + __asm__( "mov %0, %%lr;" | |
| 467 | + : "=r" (lr) | |
| 468 | + ); | |
| 469 | + return lr; | |
| 470 | +} | |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
| @@ -0,0 +1,37 @@ | ||
| 1 | +/* | |
| 2 | + * system- and machine-specific declarations for emu: | |
| 3 | + * floating-point save and restore, signal handling primitive, and | |
| 4 | + * implementation of the current-process variable `up'. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +/* | |
| 8 | + * This structure must agree with FPsave and FPrestore asm routines | |
| 9 | + */ | |
| 10 | +typedef struct FPU FPU; | |
| 11 | +struct FPU | |
| 12 | +{ | |
| 13 | + uchar env[18*8]; | |
| 14 | +}; | |
| 15 | + | |
| 16 | +/* | |
| 17 | + * Later versions of Linux seemed to need large stack for gethostbyname() | |
| 18 | + * so we had this at 128k, which is excessive. More recently, we've | |
| 19 | + * reduced it again after testing stack usage by gethostbyname. | |
| 20 | + */ | |
| 21 | +#define KSTACK (16 * 1024) | |
| 22 | + | |
| 23 | +#ifndef USE_PTHREADS | |
| 24 | +static __inline Proc *getup(void) { | |
| 25 | + Proc *p; | |
| 26 | + __asm__( "mr %0, 1" : "=r" (p)); | |
| 27 | + return *(Proc **)((unsigned long)p & ~(KSTACK - 1)); | |
| 28 | +} | |
| 29 | +#else | |
| 30 | +extern Proc* getup(void); | |
| 31 | +#endif | |
| 32 | + | |
| 33 | +#define up (getup()) | |
| 34 | + | |
| 35 | +typedef sigjmp_buf osjmpbuf; | |
| 36 | +#define ossetjmp(buf) sigsetjmp(buf, 1) | |
| 37 | + | |
| @@ -0,0 +1,83 @@ | ||
| 1 | +/* | |
| 2 | + * Linux PPC | |
| 3 | + * Mimic Plan9 floating point support | |
| 4 | + */ | |
| 5 | + | |
| 6 | +static __inline__ ulong | |
| 7 | +getfcr(void) | |
| 8 | +{ | |
| 9 | + float fpscr; | |
| 10 | + | |
| 11 | + __asm__ __volatile__ ("mffs %0" : "=&f" (fpscr):); | |
| 12 | + | |
| 13 | + return (ulong)fpscr; | |
| 14 | +} | |
| 15 | + | |
| 16 | +static __inline__ ulong | |
| 17 | +getfsr(void) | |
| 18 | +{ | |
| 19 | + float fpscr; | |
| 20 | + | |
| 21 | + __asm__ __volatile__ ("mffs %0" : "=&f" (fpscr):); | |
| 22 | + | |
| 23 | + return (ulong) fpscr; | |
| 24 | +} | |
| 25 | + | |
| 26 | +static __inline__ void | |
| 27 | +setfsr(ulong fsr) | |
| 28 | +{ | |
| 29 | + float t = (float) fsr; | |
| 30 | + | |
| 31 | + __asm__ __volatile__("mtfsf 0xff, %0" : : "f" (t) ); | |
| 32 | +} | |
| 33 | + | |
| 34 | +static __inline__ void | |
| 35 | +setfcr(ulong fcr) | |
| 36 | +{ | |
| 37 | + float t = (float) fcr; | |
| 38 | + | |
| 39 | + __asm__ __volatile__("mtfsf 0xff, %0" : : "f" (t) ); | |
| 40 | +} | |
| 41 | + | |
| 42 | +/* FPSCR */ | |
| 43 | +#define FPSFX (1<<31) /* exception summary (sticky) */ | |
| 44 | +#define FPSEX (1<<30) /* enabled exception summary */ | |
| 45 | +#define FPSVX (1<<29) /* invalid operation exception summary */ | |
| 46 | +#define FPSOX (1<<28) /* overflow exception OX (sticky) */ | |
| 47 | +#define FPSUX (1<<27) /* underflow exception UX (sticky) */ | |
| 48 | +#define FPSZX (1<<26) /* zero divide exception ZX (sticky) */ | |
| 49 | +#define FPSXX (1<<25) /* inexact exception XX (sticky) */ | |
| 50 | +#define FPSVXSNAN (1<<24) /* invalid operation exception for SNaN (sticky) */ | |
| 51 | +#define FPSVXISI (1<<23) /* invalid operation exception for ∞-∞ (sticky) */ | |
| 52 | +#define FPSVXIDI (1<<22) /* invalid operation exception for ∞/∞ (sticky) */ | |
| 53 | +#define FPSVXZDZ (1<<21) /* invalid operation exception for 0/0 (sticky) */ | |
| 54 | +#define FPSVXIMZ (1<<20) /* invalid operation exception for ∞*0 (sticky) */ | |
| 55 | +#define FPSVXVC (1<<19) /* invalid operation exception for invalid compare (sticky) */ | |
| 56 | +#define FPSFR (1<<18) /* fraction rounded */ | |
| 57 | +#define FPSFI (1<<17) /* fraction inexact */ | |
| 58 | +#define FPSFPRF (1<<16) /* floating point result class */ | |
| 59 | +#define FPSFPCC (0xF<<12) /* <, >, =, unordered */ | |
| 60 | +#define FPVXCVI (1<<8) /* enable exception for invalid integer convert (sticky) */ | |
| 61 | + | |
| 62 | +/* FCR */ | |
| 63 | +#define FPVE (1<<7) /* invalid operation exception enable */ | |
| 64 | +#define FPOVFL (1<<6) /* enable overflow exceptions */ | |
| 65 | +#define FPUNFL (1<<5) /* enable underflow */ | |
| 66 | +#define FPZDIV (1<<4) /* enable zero divide */ | |
| 67 | +#define FPINEX (1<<3) /* enable inexact exceptions */ | |
| 68 | +#define FPRMASK (3<<0) /* rounding mode */ | |
| 69 | +#define FPRNR (0<<0) | |
| 70 | +#define FPRZ (1<<0) | |
| 71 | +#define FPRPINF (2<<0) | |
| 72 | +#define FPRNINF (3<<0) | |
| 73 | +#define FPPEXT 0 | |
| 74 | +#define FPPSGL 0 | |
| 75 | +#define FPPDBL 0 | |
| 76 | +#define FPPMASK 0 | |
| 77 | +#define FPINVAL FPVE | |
| 78 | +/* FSR */ | |
| 79 | +#define FPAOVFL FPSOX | |
| 80 | +#define FPAINEX FPSXX | |
| 81 | +#define FPAUNFL FPSUX | |
| 82 | +#define FPAZDIV FPSZX | |
| 83 | +#define FPAINVAL FPSVX | |
| @@ -0,0 +1,463 @@ | ||
| 1 | +/* define _BSD_SOURCE to use ISO C, POSIX, and 4.3BSD things. */ | |
| 2 | +#define USE_PTHREADS | |
| 3 | +#ifndef _BSD_SOURCE | |
| 4 | +#define _BSD_SOURCE | |
| 5 | +#endif | |
| 6 | +#define _XOPEN_SOURCE 500 | |
| 7 | +#define _LARGEFILE_SOURCE 1 | |
| 8 | +#define _LARGEFILE64_SOURCE 1 | |
| 9 | +#define _FILE_OFFSET_BITS 64 | |
| 10 | +#ifdef USE_PTHREADS | |
| 11 | +#define _REENTRANT 1 | |
| 12 | +#endif | |
| 13 | +#include <features.h> | |
| 14 | +#include <sys/types.h> | |
| 15 | +#include <stdlib.h> | |
| 16 | +#include <stdarg.h> | |
| 17 | +#define sync __os_sync | |
| 18 | +#include <unistd.h> | |
| 19 | +#undef sync | |
| 20 | +#include <errno.h> | |
| 21 | +#define __NO_STRING_INLINES | |
| 22 | +#include <string.h> | |
| 23 | +#include "math.h" | |
| 24 | +#include <fcntl.h> | |
| 25 | +#include <setjmp.h> | |
| 26 | +#include <float.h> | |
| 27 | +#include <endian.h> | |
| 28 | + | |
| 29 | +#define getwd infgetwd | |
| 30 | + | |
| 31 | +#ifndef EMU | |
| 32 | +typedef struct Proc Proc; | |
| 33 | +#endif | |
| 34 | + | |
| 35 | +/* | |
| 36 | + * math module dtoa | |
| 37 | + * #define __LITTLE_ENDIAN /usr/include/endian.h under linux | |
| 38 | + */ | |
| 39 | +#undef __LITTLE_ENDIAN | |
| 40 | + | |
| 41 | +#define nil ((void*)0) | |
| 42 | + | |
| 43 | +typedef unsigned char uchar; | |
| 44 | +typedef signed char schar; | |
| 45 | +typedef unsigned short Rune; | |
| 46 | +typedef long long int vlong; | |
| 47 | +typedef unsigned long long int uvlong; | |
| 48 | +typedef unsigned int u32int; | |
| 49 | +typedef uvlong u64int; | |
| 50 | + | |
| 51 | +typedef unsigned int mpdigit; /* for /sys/include/mp.h */ | |
| 52 | +typedef unsigned short u16int; | |
| 53 | +typedef unsigned char u8int; | |
| 54 | +typedef unsigned long uintptr; | |
| 55 | + | |
| 56 | +#define USED(x) if(x){}else{} | |
| 57 | +#define SET(x) | |
| 58 | + | |
| 59 | +#undef nelem | |
| 60 | +#define nelem(x) (sizeof(x)/sizeof((x)[0])) | |
| 61 | +#undef offsetof | |
| 62 | +#define offsetof(s, m) (ulong)(&(((s*)0)->m)) | |
| 63 | +#undef assert | |
| 64 | +#define assert(x) if(x){}else _assert("x") | |
| 65 | + | |
| 66 | +/* | |
| 67 | + * most mem and string routines are declared by ANSI/POSIX files above | |
| 68 | + */ | |
| 69 | + | |
| 70 | +extern char* strecpy(char*, char*, char*); | |
| 71 | +extern char* strdup(const char*); | |
| 72 | +extern int cistrncmp(char*, char*, int); | |
| 73 | +extern int cistrcmp(char*, char*); | |
| 74 | +extern char* cistrstr(char*, char*); | |
| 75 | +extern int tokenize(char*, char**, int); | |
| 76 | + | |
| 77 | +enum | |
| 78 | +{ | |
| 79 | + UTFmax = 3, /* maximum bytes per rune */ | |
| 80 | + Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */ | |
| 81 | + Runeself = 0x80, /* rune and UTF sequences are the same (<) */ | |
| 82 | + Runeerror = 0x80 /* decoding error in UTF */ | |
| 83 | +}; | |
| 84 | + | |
| 85 | +/* | |
| 86 | + * rune routines | |
| 87 | + */ | |
| 88 | +extern int runetochar(char*, Rune*); | |
| 89 | +extern int chartorune(Rune*, char*); | |
| 90 | +extern int runelen(long); | |
| 91 | +extern int runenlen(Rune*, int); | |
| 92 | +extern int fullrune(char*, int); | |
| 93 | +extern int utflen(char*); | |
| 94 | +extern int utfnlen(char*, long); | |
| 95 | +extern char* utfrune(char*, long); | |
| 96 | +extern char* utfrrune(char*, long); | |
| 97 | +extern char* utfutf(char*, char*); | |
| 98 | +extern char* utfecpy(char*, char*, char*); | |
| 99 | + | |
| 100 | +extern Rune* runestrcat(Rune*, Rune*); | |
| 101 | +extern Rune* runestrchr(Rune*, Rune); | |
| 102 | +extern int runestrcmp(Rune*, Rune*); | |
| 103 | +extern Rune* runestrcpy(Rune*, Rune*); | |
| 104 | +extern Rune* runestrncpy(Rune*, Rune*, long); | |
| 105 | +extern Rune* runestrecpy(Rune*, Rune*, Rune*); | |
| 106 | +extern Rune* runestrdup(Rune*); | |
| 107 | +extern Rune* runestrncat(Rune*, Rune*, long); | |
| 108 | +extern int runestrncmp(Rune*, Rune*, long); | |
| 109 | +extern Rune* runestrrchr(Rune*, Rune); | |
| 110 | +extern long runestrlen(Rune*); | |
| 111 | +extern Rune* runestrstr(Rune*, Rune*); | |
| 112 | + | |
| 113 | +extern Rune tolowerrune(Rune); | |
| 114 | +extern Rune totitlerune(Rune); | |
| 115 | +extern Rune toupperrune(Rune); | |
| 116 | +extern int isalpharune(Rune); | |
| 117 | +extern int islowerrune(Rune); | |
| 118 | +extern int isspacerune(Rune); | |
| 119 | +extern int istitlerune(Rune); | |
| 120 | +extern int isupperrune(Rune); | |
| 121 | + | |
| 122 | +/* | |
| 123 | + * malloc | |
| 124 | + */ | |
| 125 | +extern void* malloc(size_t); | |
| 126 | +extern void* mallocz(ulong, int); | |
| 127 | +extern void free(void*); | |
| 128 | +extern ulong msize(void*); | |
| 129 | +extern void* calloc(size_t, size_t); | |
| 130 | +extern void* realloc(void*, size_t); | |
| 131 | +extern void setmalloctag(void*, ulong); | |
| 132 | +extern void setrealloctag(void*, ulong); | |
| 133 | +extern ulong getmalloctag(void*); | |
| 134 | +extern ulong getrealloctag(void*); | |
| 135 | +extern void* malloctopoolblock(void*); | |
| 136 | + | |
| 137 | +/* | |
| 138 | + * print routines | |
| 139 | + */ | |
| 140 | +typedef struct Fmt Fmt; | |
| 141 | +struct Fmt{ | |
| 142 | + uchar runes; /* output buffer is runes or chars? */ | |
| 143 | + void *start; /* of buffer */ | |
| 144 | + void *to; /* current place in the buffer */ | |
| 145 | + void *stop; /* end of the buffer; overwritten if flush fails */ | |
| 146 | + int (*flush)(Fmt *); /* called when to == stop */ | |
| 147 | + void *farg; /* to make flush a closure */ | |
| 148 | + int nfmt; /* num chars formatted so far */ | |
| 149 | + va_list args; /* args passed to dofmt */ | |
| 150 | + int r; /* % format Rune */ | |
| 151 | + int width; | |
| 152 | + int prec; | |
| 153 | + ulong flags; | |
| 154 | +}; | |
| 155 | + | |
| 156 | +enum{ | |
| 157 | + FmtWidth = 1, | |
| 158 | + FmtLeft = FmtWidth << 1, | |
| 159 | + FmtPrec = FmtLeft << 1, | |
| 160 | + FmtSharp = FmtPrec << 1, | |
| 161 | + FmtSpace = FmtSharp << 1, | |
| 162 | + FmtSign = FmtSpace << 1, | |
| 163 | + FmtZero = FmtSign << 1, | |
| 164 | + FmtUnsigned = FmtZero << 1, | |
| 165 | + FmtShort = FmtUnsigned << 1, | |
| 166 | + FmtLong = FmtShort << 1, | |
| 167 | + FmtVLong = FmtLong << 1, | |
| 168 | + FmtComma = FmtVLong << 1, | |
| 169 | + FmtByte = FmtComma << 1, | |
| 170 | + | |
| 171 | + FmtFlag = FmtByte << 1 | |
| 172 | +}; | |
| 173 | + | |
| 174 | +extern int print(char*, ...); | |
| 175 | +extern char* seprint(char*, char*, char*, ...); | |
| 176 | +extern char* vseprint(char*, char*, char*, va_list); | |
| 177 | +extern int snprint(char*, int, char*, ...); | |
| 178 | +extern int vsnprint(char*, int, char*, va_list); | |
| 179 | +extern char* smprint(char*, ...); | |
| 180 | +extern char* vsmprint(char*, va_list); | |
| 181 | +extern int sprint(char*, char*, ...); | |
| 182 | +extern int fprint(int, char*, ...); | |
| 183 | +extern int vfprint(int, char*, va_list); | |
| 184 | + | |
| 185 | +extern int runesprint(Rune*, char*, ...); | |
| 186 | +extern int runesnprint(Rune*, int, char*, ...); | |
| 187 | +extern int runevsnprint(Rune*, int, char*, va_list); | |
| 188 | +extern Rune* runeseprint(Rune*, Rune*, char*, ...); | |
| 189 | +extern Rune* runevseprint(Rune*, Rune*, char*, va_list); | |
| 190 | +extern Rune* runesmprint(char*, ...); | |
| 191 | +extern Rune* runevsmprint(char*, va_list); | |
| 192 | + | |
| 193 | +extern int fmtfdinit(Fmt*, int, char*, int); | |
| 194 | +extern int fmtfdflush(Fmt*); | |
| 195 | +extern int fmtstrinit(Fmt*); | |
| 196 | +extern char* fmtstrflush(Fmt*); | |
| 197 | +extern int runefmtstrinit(Fmt*); | |
| 198 | +extern Rune* runefmtstrflush(Fmt*); | |
| 199 | + | |
| 200 | +extern int fmtinstall(int, int (*)(Fmt*)); | |
| 201 | +extern int dofmt(Fmt*, char*); | |
| 202 | +extern int dorfmt(Fmt*, Rune*); | |
| 203 | +extern int fmtprint(Fmt*, char*, ...); | |
| 204 | +extern int fmtvprint(Fmt*, char*, va_list); | |
| 205 | +extern int fmtrune(Fmt*, int); | |
| 206 | +extern int fmtstrcpy(Fmt*, char*); | |
| 207 | +extern int fmtrunestrcpy(Fmt*, Rune*); | |
| 208 | +/* | |
| 209 | + * error string for %r | |
| 210 | + * supplied on per os basis, not part of fmt library | |
| 211 | + */ | |
| 212 | +extern int errfmt(Fmt *f); | |
| 213 | + | |
| 214 | +/* | |
| 215 | + * quoted strings | |
| 216 | + */ | |
| 217 | +extern char *unquotestrdup(char*); | |
| 218 | +extern Rune *unquoterunestrdup(Rune*); | |
| 219 | +extern char *quotestrdup(char*); | |
| 220 | +extern Rune *quoterunestrdup(Rune*); | |
| 221 | +extern int quotestrfmt(Fmt*); | |
| 222 | +extern int quoterunestrfmt(Fmt*); | |
| 223 | +extern void quotefmtinstall(void); | |
| 224 | +extern int (*doquote)(int); | |
| 225 | + | |
| 226 | +/* | |
| 227 | + * random number | |
| 228 | + */ | |
| 229 | +extern ulong truerand(void); | |
| 230 | +extern ulong ntruerand(ulong); | |
| 231 | + | |
| 232 | +/* | |
| 233 | + * math | |
| 234 | + */ | |
| 235 | +extern int isNaN(double); | |
| 236 | +extern int isInf(double, int); | |
| 237 | + | |
| 238 | +/* | |
| 239 | + * Time-of-day | |
| 240 | + */ | |
| 241 | + | |
| 242 | +typedef struct Tm Tm; | |
| 243 | +struct Tm { | |
| 244 | + int sec; | |
| 245 | + int min; | |
| 246 | + int hour; | |
| 247 | + int mday; | |
| 248 | + int mon; | |
| 249 | + int year; | |
| 250 | + int wday; | |
| 251 | + int yday; | |
| 252 | + char zone[4]; | |
| 253 | + int tzoff; | |
| 254 | +}; | |
| 255 | +extern vlong osnsec(void); | |
| 256 | +#define nsec osnsec | |
| 257 | + | |
| 258 | +/* | |
| 259 | + * one-of-a-kind | |
| 260 | + */ | |
| 261 | +extern void _assert(char*); | |
| 262 | +extern double charstod(int(*)(void*), void*); | |
| 263 | +extern char* cleanname(char*); | |
| 264 | +extern uintptr getcallerpc(void*); | |
| 265 | +extern int getfields(char*, char**, int, int, char*); | |
| 266 | +extern char* getuser(void); | |
| 267 | +extern char* getwd(char*, int); | |
| 268 | +extern double ipow10(int); | |
| 269 | +#define pow10 infpow10 | |
| 270 | +extern double pow10(int); | |
| 271 | +extern vlong strtoll(const char*, char**, int); | |
| 272 | +#define qsort infqsort | |
| 273 | +extern void qsort(void*, long, long, int (*)(void*, void*)); | |
| 274 | +extern uvlong strtoull(const char*, char**, int); | |
| 275 | +extern void sysfatal(char*, ...); | |
| 276 | +extern int dec64(uchar*, int, char*, int); | |
| 277 | +extern int enc64(char*, int, uchar*, int); | |
| 278 | +extern int dec32(uchar*, int, char*, int); | |
| 279 | +extern int enc32(char*, int, uchar*, int); | |
| 280 | +extern int dec16(uchar*, int, char*, int); | |
| 281 | +extern int enc16(char*, int, uchar*, int); | |
| 282 | +extern int encodefmt(Fmt*); | |
| 283 | + | |
| 284 | +/* | |
| 285 | + * synchronization | |
| 286 | + */ | |
| 287 | +typedef | |
| 288 | +struct Lock { | |
| 289 | + int val; | |
| 290 | + int pid; | |
| 291 | +} Lock; | |
| 292 | + | |
| 293 | +extern int _tas(int*); | |
| 294 | + | |
| 295 | +extern void lock(Lock*); | |
| 296 | +extern void unlock(Lock*); | |
| 297 | +extern int canlock(Lock*); | |
| 298 | + | |
| 299 | +typedef struct QLock QLock; | |
| 300 | +struct QLock | |
| 301 | +{ | |
| 302 | + Lock use; /* to access Qlock structure */ | |
| 303 | + Proc *head; /* next process waiting for object */ | |
| 304 | + Proc *tail; /* last process waiting for object */ | |
| 305 | + int locked; /* flag */ | |
| 306 | +}; | |
| 307 | + | |
| 308 | +extern void qlock(QLock*); | |
| 309 | +extern void qunlock(QLock*); | |
| 310 | +extern int canqlock(QLock*); | |
| 311 | +extern void _qlockinit(ulong (*)(ulong, ulong)); /* called only by the thread library */ | |
| 312 | + | |
| 313 | +typedef | |
| 314 | +struct RWLock | |
| 315 | +{ | |
| 316 | + Lock l; /* Lock modify lock */ | |
| 317 | + QLock x; /* Mutual exclusion lock */ | |
| 318 | + QLock k; /* Lock for waiting writers */ | |
| 319 | + int readers; /* Count of readers in lock */ | |
| 320 | +} RWLock; | |
| 321 | + | |
| 322 | +extern int canrlock(RWLock*); | |
| 323 | +extern int canwlock(RWLock*); | |
| 324 | +extern void rlock(RWLock*); | |
| 325 | +extern void runlock(RWLock*); | |
| 326 | +extern void wlock(RWLock*); | |
| 327 | +extern void wunlock(RWLock*); | |
| 328 | + | |
| 329 | +/* | |
| 330 | + * network dialing | |
| 331 | + */ | |
| 332 | +#define NETPATHLEN 40 | |
| 333 | + | |
| 334 | +/* | |
| 335 | + * system calls | |
| 336 | + * | |
| 337 | + */ | |
| 338 | +#define STATMAX 65535U /* max length of machine-independent stat structure */ | |
| 339 | +#define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */ | |
| 340 | +#define ERRMAX 128 /* max length of error string */ | |
| 341 | + | |
| 342 | +#define MORDER 0x0003 /* mask for bits defining order of mounting */ | |
| 343 | +#define MREPL 0x0000 /* mount replaces object */ | |
| 344 | +#define MBEFORE 0x0001 /* mount goes before others in union directory */ | |
| 345 | +#define MAFTER 0x0002 /* mount goes after others in union directory */ | |
| 346 | +#define MCREATE 0x0004 /* permit creation in mounted directory */ | |
| 347 | +#define MCACHE 0x0010 /* cache some data */ | |
| 348 | +#define MMASK 0x0017 /* all bits on */ | |
| 349 | + | |
| 350 | +#define OREAD 0 /* open for read */ | |
| 351 | +#define OWRITE 1 /* write */ | |
| 352 | +#define ORDWR 2 /* read and write */ | |
| 353 | +#define OEXEC 3 /* execute, == read but check execute permission */ | |
| 354 | +#define OTRUNC 16 /* or'ed in (except for exec), truncate file first */ | |
| 355 | +#define OCEXEC 32 /* or'ed in, close on exec */ | |
| 356 | +#define ORCLOSE 64 /* or'ed in, remove on close */ | |
| 357 | +#define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */ | |
| 358 | + | |
| 359 | +#define AEXIST 0 /* accessible: exists */ | |
| 360 | +#define AEXEC 1 /* execute access */ | |
| 361 | +#define AWRITE 2 /* write access */ | |
| 362 | +#define AREAD 4 /* read access */ | |
| 363 | + | |
| 364 | +/* bits in Qid.type */ | |
| 365 | +#define QTDIR 0x80 /* type bit for directories */ | |
| 366 | +#define QTAPPEND 0x40 /* type bit for append only files */ | |
| 367 | +#define QTEXCL 0x20 /* type bit for exclusive use files */ | |
| 368 | +#define QTMOUNT 0x10 /* type bit for mounted channel */ | |
| 369 | +#define QTAUTH 0x08 /* type bit for authentication file */ | |
| 370 | +#define QTFILE 0x00 /* plain file */ | |
| 371 | + | |
| 372 | +/* bits in Dir.mode */ | |
| 373 | +#define DMDIR 0x80000000 /* mode bit for directories */ | |
| 374 | +#define DMAPPEND 0x40000000 /* mode bit for append only files */ | |
| 375 | +#define DMEXCL 0x20000000 /* mode bit for exclusive use files */ | |
| 376 | +#define DMMOUNT 0x10000000 /* mode bit for mounted channel */ | |
| 377 | +#define DMAUTH 0x08000000 /* mode bit for authentication file */ | |
| 378 | +#define DMREAD 0x4 /* mode bit for read permission */ | |
| 379 | +#define DMWRITE 0x2 /* mode bit for write permission */ | |
| 380 | +#define DMEXEC 0x1 /* mode bit for execute permission */ | |
| 381 | + | |
| 382 | +typedef | |
| 383 | +struct Qid | |
| 384 | +{ | |
| 385 | + uvlong path; | |
| 386 | + ulong vers; | |
| 387 | + uchar type; | |
| 388 | +} Qid; | |
| 389 | + | |
| 390 | +typedef | |
| 391 | +struct Dir { | |
| 392 | + /* system-modified data */ | |
| 393 | + ushort type; /* server type */ | |
| 394 | + uint dev; /* server subtype */ | |
| 395 | + /* file data */ | |
| 396 | + Qid qid; /* unique id from server */ | |
| 397 | + ulong mode; /* permissions */ | |
| 398 | + ulong atime; /* last read time */ | |
| 399 | + ulong mtime; /* last write time */ | |
| 400 | + vlong length; /* file length */ | |
| 401 | + char *name; /* last element of path */ | |
| 402 | + char *uid; /* owner name */ | |
| 403 | + char *gid; /* group name */ | |
| 404 | + char *muid; /* last modifier name */ | |
| 405 | +} Dir; | |
| 406 | + | |
| 407 | +extern Dir* dirstat(char*); | |
| 408 | +extern Dir* dirfstat(int); | |
| 409 | +extern int dirwstat(char*, Dir*); | |
| 410 | +extern int dirfwstat(int, Dir*); | |
| 411 | +extern long dirread(int, Dir**); | |
| 412 | +extern void nulldir(Dir*); | |
| 413 | +extern long dirreadall(int, Dir**); | |
| 414 | + | |
| 415 | +typedef | |
| 416 | +struct Waitmsg | |
| 417 | +{ | |
| 418 | + int pid; /* of loved one */ | |
| 419 | + ulong time[3]; /* of loved one & descendants */ | |
| 420 | + char *msg; | |
| 421 | +} Waitmsg; | |
| 422 | + | |
| 423 | +extern void _exits(char*); | |
| 424 | + | |
| 425 | +extern void exits(char*); | |
| 426 | +extern int create(char*, int, int); | |
| 427 | +extern int errstr(char*, uint); | |
| 428 | + | |
| 429 | +extern void perror(const char*); | |
| 430 | +extern long readn(int, void*, long); | |
| 431 | +extern int remove(const char*); | |
| 432 | +extern void rerrstr(char*, uint); | |
| 433 | +extern vlong seek(int, vlong, int); | |
| 434 | +extern int segflush(void*, ulong); | |
| 435 | +extern void werrstr(char*, ...); | |
| 436 | + | |
| 437 | +extern char *argv0; | |
| 438 | +#define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ | |
| 439 | + argv[0] && argv[0][0]=='-' && argv[0][1];\ | |
| 440 | + argc--, argv++) {\ | |
| 441 | + char *_args, *_argt;\ | |
| 442 | + Rune _argc;\ | |
| 443 | + _args = &argv[0][1];\ | |
| 444 | + if(_args[0]=='-' && _args[1]==0){\ | |
| 445 | + argc--; argv++; break;\ | |
| 446 | + }\ | |
| 447 | + _argc = 0;\ | |
| 448 | + while(*_args && (_args += chartorune(&_argc, _args)))\ | |
| 449 | + switch(_argc) | |
| 450 | +#define ARGEND SET(_argt);USED(_argt);USED(_argc); USED(_args);}USED(argv); USED(argc); | |
| 451 | +#define ARGF() (_argt=_args, _args="",\ | |
| 452 | + (*_argt? _argt: argv[1]? (argc--, *++argv): 0)) | |
| 453 | +#define EARGF(x) (_argt=_args, _args="",\ | |
| 454 | + (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) | |
| 455 | + | |
| 456 | +#define ARGC() _argc | |
| 457 | + | |
| 458 | +/* | |
| 459 | + * Extensions for Inferno to basic libc.h | |
| 460 | + */ | |
| 461 | + | |
| 462 | +#define setbinmode() | |
| 463 | + | |
| @@ -0,0 +1,39 @@ | ||
| 1 | +/* | |
| 2 | + * system- and machine-specific declarations for emu: | |
| 3 | + * floating-point save and restore, signal handling primitive, and | |
| 4 | + * implementation of the current-process variable `up'. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +/* | |
| 8 | + * This structure must agree with FPsave and FPrestore asm routines | |
| 9 | + */ | |
| 10 | +typedef struct FPU FPU; | |
| 11 | +struct FPU | |
| 12 | +{ | |
| 13 | + uchar env[28]; | |
| 14 | +}; | |
| 15 | + | |
| 16 | +/* | |
| 17 | + * Later versions of Linux seemed to need large stack for gethostbyname() | |
| 18 | + * so we had this at 128k, which is excessive. More recently, we've | |
| 19 | + * reduced it again after testing stack usage by gethostbyname. | |
| 20 | + */ | |
| 21 | +#define KSTACK (16 * 1024) | |
| 22 | + | |
| 23 | +#ifndef USE_PTHREADS | |
| 24 | +static __inline Proc *getup(void) { | |
| 25 | + Proc *p; | |
| 26 | + __asm__( "move %0, $29\n\t" | |
| 27 | + : "=r" (p) | |
| 28 | + ); | |
| 29 | + return *(Proc **)((unsigned long)p & ~(KSTACK - 1)); | |
| 30 | +} | |
| 31 | +#else | |
| 32 | +extern Proc* getup(void); | |
| 33 | +#endif | |
| 34 | + | |
| 35 | +#define up (getup()) | |
| 36 | + | |
| 37 | +typedef sigjmp_buf osjmpbuf; | |
| 38 | +#define ossetjmp(buf) sigsetjmp(buf, 1) | |
| 39 | + | |
| @@ -0,0 +1,65 @@ | ||
| 1 | +/* FCR */ | |
| 2 | +#define FCRBITS 0x00000F83 | |
| 3 | +#define FPINEX (1<<7) | |
| 4 | +#define FPUNFL (1<<8) | |
| 5 | +#define FPOVFL (1<<9) | |
| 6 | +#define FPZDIV (1<<10) | |
| 7 | +#define FPINVAL (1<<11) | |
| 8 | +#define FPRNR (0<<0) | |
| 9 | +#define FPRZ (1<<0) | |
| 10 | +#define FPRPINF (2<<0) | |
| 11 | +#define FPRNINF (3<<0) | |
| 12 | +#define FPRMASK (3<<0) | |
| 13 | +#define FPPEXT 0 | |
| 14 | +#define FPPSGL 0 | |
| 15 | +#define FPPDBL 0 | |
| 16 | +#define FPPMASK 0 | |
| 17 | +/* FSR */ | |
| 18 | +#define FSRBITS 0x0003F07C | |
| 19 | +#define FPAINEX (1<<2) | |
| 20 | +#define FPAOVFL (1<<4) | |
| 21 | +#define FPAUNFL (1<<3) | |
| 22 | +#define FPAZDIV (1<<5) | |
| 23 | +#define FPAINVAL (1<<6) | |
| 24 | + | |
| 25 | +/* | |
| 26 | + * Linux mips fpu support | |
| 27 | + * Mimic Plan9 floating point support | |
| 28 | + */ | |
| 29 | + | |
| 30 | +static void | |
| 31 | +setfcr(ulong fcr) | |
| 32 | +{ | |
| 33 | + __asm__("ctc1 %0,$31\n" | |
| 34 | + : :"r" (fcr) | |
| 35 | + ); | |
| 36 | +} | |
| 37 | + | |
| 38 | +static ulong | |
| 39 | +getfcr(void) | |
| 40 | +{ | |
| 41 | + ulong fcr = 0; | |
| 42 | + __asm__("cfc1 %0,$31\n" | |
| 43 | + : "=r" (fcr) | |
| 44 | + ); | |
| 45 | + fcr &= FCRBITS; | |
| 46 | + return fcr; | |
| 47 | +} | |
| 48 | + | |
| 49 | +static ulong | |
| 50 | +getfsr(void) | |
| 51 | +{ | |
| 52 | + ulong fsr = 0; | |
| 53 | + __asm__("cfc1 %0,$31\n" | |
| 54 | + : "=r" (fsr) | |
| 55 | + ); | |
| 56 | + fsr &= FSRBITS; | |
| 57 | + return fsr; | |
| 58 | +} | |
| 59 | + | |
| 60 | +static void | |
| 61 | +setfsr(ulong fsr) | |
| 62 | +{ | |
| 63 | + fsr |= getfcr(); | |
| 64 | + setfcr(getfcr()|fsr); | |
| 65 | +} | |
| @@ -0,0 +1,464 @@ | ||
| 1 | +/* define _BSD_SOURCE to use ISO C, POSIX, and 4.3BSD things. */ | |
| 2 | +#define USE_PTHREADS | |
| 3 | +#ifndef _BSD_SOURCE | |
| 4 | +#define _BSD_SOURCE | |
| 5 | +#endif | |
| 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 | + | |
| 25 | +#define getwd infgetwd | |
| 26 | + | |
| 27 | +#ifndef EMU | |
| 28 | +typedef struct Proc Proc; | |
| 29 | +#endif | |
| 30 | + | |
| 31 | +/* | |
| 32 | + * math module dtoa | |
| 33 | + * #define __LITTLE_ENDIAN according to target under linux | |
| 34 | + */ | |
| 35 | + | |
| 36 | +#ifdef __MIPSEB__ | |
| 37 | +#undef __LITTLE_ENDIAN | |
| 38 | +#else | |
| 39 | +#define __LITTLE_ENDIAN 1234 | |
| 40 | +#endif | |
| 41 | + | |
| 42 | +#define nil ((void*)0) | |
| 43 | + | |
| 44 | +typedef unsigned char uchar; | |
| 45 | +typedef signed char schar; | |
| 46 | +typedef unsigned short Rune; | |
| 47 | +typedef long long int vlong; | |
| 48 | +typedef unsigned long long int uvlong; | |
| 49 | +typedef unsigned int u32int; | |
| 50 | +typedef uvlong u64int; | |
| 51 | + | |
| 52 | +typedef unsigned int mpdigit; /* for /sys/include/mp.h */ | |
| 53 | +typedef unsigned short u16int; | |
| 54 | +typedef unsigned char u8int; | |
| 55 | +typedef unsigned long uintptr; | |
| 56 | + | |
| 57 | +#define USED(x) if(x){}else{} | |
| 58 | +#define SET(x) | |
| 59 | + | |
| 60 | +#undef nelem | |
| 61 | +#define nelem(x) (sizeof(x)/sizeof((x)[0])) | |
| 62 | +#undef offsetof | |
| 63 | +#define offsetof(s, m) (ulong)(&(((s*)0)->m)) | |
| 64 | +#undef assert | |
| 65 | +#define assert(x) if(x){}else _assert("x") | |
| 66 | + | |
| 67 | +/* | |
| 68 | + * most mem and string routines are declared by ANSI/POSIX files above | |
| 69 | + */ | |
| 70 | + | |
| 71 | +extern char* strecpy(char*, char*, char*); | |
| 72 | +extern char* strdup(const char*); | |
| 73 | +extern int cistrncmp(char*, char*, int); | |
| 74 | +extern int cistrcmp(char*, char*); | |
| 75 | +extern char* cistrstr(char*, char*); | |
| 76 | +extern int tokenize(char*, char**, int); | |
| 77 | + | |
| 78 | +enum | |
| 79 | +{ | |
| 80 | + UTFmax = 3, /* maximum bytes per rune */ | |
| 81 | + Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */ | |
| 82 | + Runeself = 0x80, /* rune and UTF sequences are the same (<) */ | |
| 83 | + Runeerror = 0x80 /* decoding error in UTF */ | |
| 84 | +}; | |
| 85 | + | |
| 86 | +/* | |
| 87 | + * rune routines | |
| 88 | + */ | |
| 89 | +extern int runetochar(char*, Rune*); | |
| 90 | +extern int chartorune(Rune*, char*); | |
| 91 | +extern int runelen(long); | |
| 92 | +extern int runenlen(Rune*, int); | |
| 93 | +extern int fullrune(char*, int); | |
| 94 | +extern int utflen(char*); | |
| 95 | +extern int utfnlen(char*, long); | |
| 96 | +extern char* utfrune(char*, long); | |
| 97 | +extern char* utfrrune(char*, long); | |
| 98 | +extern char* utfutf(char*, char*); | |
| 99 | +extern char* utfecpy(char*, char*, char*); | |
| 100 | + | |
| 101 | +extern Rune* runestrcat(Rune*, Rune*); | |
| 102 | +extern Rune* runestrchr(Rune*, Rune); | |
| 103 | +extern int runestrcmp(Rune*, Rune*); | |
| 104 | +extern Rune* runestrcpy(Rune*, Rune*); | |
| 105 | +extern Rune* runestrncpy(Rune*, Rune*, long); | |
| 106 | +extern Rune* runestrecpy(Rune*, Rune*, Rune*); | |
| 107 | +extern Rune* runestrdup(Rune*); | |
| 108 | +extern Rune* runestrncat(Rune*, Rune*, long); | |
| 109 | +extern int runestrncmp(Rune*, Rune*, long); | |
| 110 | +extern Rune* runestrrchr(Rune*, Rune); | |
| 111 | +extern long runestrlen(Rune*); | |
| 112 | +extern Rune* runestrstr(Rune*, Rune*); | |
| 113 | + | |
| 114 | +extern Rune tolowerrune(Rune); | |
| 115 | +extern Rune totitlerune(Rune); | |
| 116 | +extern Rune toupperrune(Rune); | |
| 117 | +extern int isalpharune(Rune); | |
| 118 | +extern int islowerrune(Rune); | |
| 119 | +extern int isspacerune(Rune); | |
| 120 | +extern int istitlerune(Rune); | |
| 121 | +extern int isupperrune(Rune); | |
| 122 | + | |
| 123 | +/* | |
| 124 | + * malloc | |
| 125 | + */ | |
| 126 | +extern void* malloc(size_t); | |
| 127 | +extern void* mallocz(ulong, int); | |
| 128 | +extern void free(void*); | |
| 129 | +extern ulong msize(void*); | |
| 130 | +extern void* calloc(size_t, size_t); | |
| 131 | +extern void* realloc(void*, size_t); | |
| 132 | +extern void setmalloctag(void*, ulong); | |
| 133 | +extern void setrealloctag(void*, ulong); | |
| 134 | +extern ulong getmalloctag(void*); | |
| 135 | +extern ulong getrealloctag(void*); | |
| 136 | +extern void* malloctopoolblock(void*); | |
| 137 | + | |
| 138 | +/* | |
| 139 | + * print routines | |
| 140 | + */ | |
| 141 | +typedef struct Fmt Fmt; | |
| 142 | +struct Fmt{ | |
| 143 | + uchar runes; /* output buffer is runes or chars? */ | |
| 144 | + void *start; /* of buffer */ | |
| 145 | + void *to; /* current place in the buffer */ | |
| 146 | + void *stop; /* end of the buffer; overwritten if flush fails */ | |
| 147 | + int (*flush)(Fmt *); /* called when to == stop */ | |
| 148 | + void *farg; /* to make flush a closure */ | |
| 149 | + int nfmt; /* num chars formatted so far */ | |
| 150 | + va_list args; /* args passed to dofmt */ | |
| 151 | + int r; /* % format Rune */ | |
| 152 | + int width; | |
| 153 | + int prec; | |
| 154 | + ulong flags; | |
| 155 | +}; | |
| 156 | + | |
| 157 | +enum{ | |
| 158 | + FmtWidth = 1, | |
| 159 | + FmtLeft = FmtWidth << 1, | |
| 160 | + FmtPrec = FmtLeft << 1, | |
| 161 | + FmtSharp = FmtPrec << 1, | |
| 162 | + FmtSpace = FmtSharp << 1, | |
| 163 | + FmtSign = FmtSpace << 1, | |
| 164 | + FmtZero = FmtSign << 1, | |
| 165 | + FmtUnsigned = FmtZero << 1, | |
| 166 | + FmtShort = FmtUnsigned << 1, | |
| 167 | + FmtLong = FmtShort << 1, | |
| 168 | + FmtVLong = FmtLong << 1, | |
| 169 | + FmtComma = FmtVLong << 1, | |
| 170 | + FmtByte = FmtComma << 1, | |
| 171 | + | |
| 172 | + FmtFlag = FmtByte << 1 | |
| 173 | +}; | |
| 174 | + | |
| 175 | +extern int print(char*, ...); | |
| 176 | +extern char* seprint(char*, char*, char*, ...); | |
| 177 | +extern char* vseprint(char*, char*, char*, va_list); | |
| 178 | +extern int snprint(char*, int, char*, ...); | |
| 179 | +extern int vsnprint(char*, int, char*, va_list); | |
| 180 | +extern char* smprint(char*, ...); | |
| 181 | +extern char* vsmprint(char*, va_list); | |
| 182 | +extern int sprint(char*, char*, ...); | |
| 183 | +extern int fprint(int, char*, ...); | |
| 184 | +extern int vfprint(int, char*, va_list); | |
| 185 | + | |
| 186 | +extern int runesprint(Rune*, char*, ...); | |
| 187 | +extern int runesnprint(Rune*, int, char*, ...); | |
| 188 | +extern int runevsnprint(Rune*, int, char*, va_list); | |
| 189 | +extern Rune* runeseprint(Rune*, Rune*, char*, ...); | |
| 190 | +extern Rune* runevseprint(Rune*, Rune*, char*, va_list); | |
| 191 | +extern Rune* runesmprint(char*, ...); | |
| 192 | +extern Rune* runevsmprint(char*, va_list); | |
| 193 | + | |
| 194 | +extern int fmtfdinit(Fmt*, int, char*, int); | |
| 195 | +extern int fmtfdflush(Fmt*); | |
| 196 | +extern int fmtstrinit(Fmt*); | |
| 197 | +extern char* fmtstrflush(Fmt*); | |
| 198 | +extern int runefmtstrinit(Fmt*); | |
| 199 | +extern Rune* runefmtstrflush(Fmt*); | |
| 200 | + | |
| 201 | +extern int fmtinstall(int, int (*)(Fmt*)); | |
| 202 | +extern int dofmt(Fmt*, char*); | |
| 203 | +extern int dorfmt(Fmt*, Rune*); | |
| 204 | +extern int fmtprint(Fmt*, char*, ...); | |
| 205 | +extern int fmtvprint(Fmt*, char*, va_list); | |
| 206 | +extern int fmtrune(Fmt*, int); | |
| 207 | +extern int fmtstrcpy(Fmt*, char*); | |
| 208 | +extern int fmtrunestrcpy(Fmt*, Rune*); | |
| 209 | +/* | |
| 210 | + * error string for %r | |
| 211 | + * supplied on per os basis, not part of fmt library | |
| 212 | + */ | |
| 213 | +extern int errfmt(Fmt *f); | |
| 214 | + | |
| 215 | +/* | |
| 216 | + * quoted strings | |
| 217 | + */ | |
| 218 | +extern char *unquotestrdup(char*); | |
| 219 | +extern Rune *unquoterunestrdup(Rune*); | |
| 220 | +extern char *quotestrdup(char*); | |
| 221 | +extern Rune *quoterunestrdup(Rune*); | |
| 222 | +extern int quotestrfmt(Fmt*); | |
| 223 | +extern int quoterunestrfmt(Fmt*); | |
| 224 | +extern void quotefmtinstall(void); | |
| 225 | +extern int (*doquote)(int); | |
| 226 | + | |
| 227 | +/* | |
| 228 | + * random number | |
| 229 | + */ | |
| 230 | +extern ulong truerand(void); | |
| 231 | +extern ulong ntruerand(ulong); | |
| 232 | + | |
| 233 | +/* | |
| 234 | + * math | |
| 235 | + */ | |
| 236 | +extern int isNaN(double); | |
| 237 | +extern int isInf(double, int); | |
| 238 | + | |
| 239 | +/* | |
| 240 | + * Time-of-day | |
| 241 | + */ | |
| 242 | + | |
| 243 | +typedef struct Tm Tm; | |
| 244 | +struct Tm { | |
| 245 | + int sec; | |
| 246 | + int min; | |
| 247 | + int hour; | |
| 248 | + int mday; | |
| 249 | + int mon; | |
| 250 | + int year; | |
| 251 | + int wday; | |
| 252 | + int yday; | |
| 253 | + char zone[4]; | |
| 254 | + int tzoff; | |
| 255 | +}; | |
| 256 | +extern vlong osnsec(void); | |
| 257 | +#define nsec osnsec | |
| 258 | + | |
| 259 | +/* | |
| 260 | + * one-of-a-kind | |
| 261 | + */ | |
| 262 | +extern void _assert(char*); | |
| 263 | +extern double charstod(int(*)(void*), void*); | |
| 264 | +extern char* cleanname(char*); | |
| 265 | +extern uintptr getcallerpc(void*); | |
| 266 | +extern int getfields(char*, char**, int, int, char*); | |
| 267 | +extern char* getuser(void); | |
| 268 | +extern char* getwd(char*, int); | |
| 269 | +extern double ipow10(int); | |
| 270 | +#define pow10 infpow10 | |
| 271 | +extern double pow10(int); | |
| 272 | +extern vlong strtoll(const char*, char**, int); | |
| 273 | +#define qsort infqsort | |
| 274 | +extern void qsort(void*, long, long, int (*)(void*, void*)); | |
| 275 | +extern uvlong strtoull(const char*, char**, int); | |
| 276 | +extern void sysfatal(char*, ...); | |
| 277 | +extern int dec64(uchar*, int, char*, int); | |
| 278 | +extern int enc64(char*, int, uchar*, int); | |
| 279 | +extern int dec32(uchar*, int, char*, int); | |
| 280 | +extern int enc32(char*, int, uchar*, int); | |
| 281 | +extern int dec16(uchar*, int, char*, int); | |
| 282 | +extern int enc16(char*, int, uchar*, int); | |
| 283 | +extern int encodefmt(Fmt*); | |
| 284 | + | |
| 285 | +/* | |
| 286 | + * synchronization | |
| 287 | + */ | |
| 288 | +typedef | |
| 289 | +struct Lock { | |
| 290 | + ulong val; | |
| 291 | + int pid; | |
| 292 | +} Lock; | |
| 293 | + | |
| 294 | +extern ulong _tas(ulong*); | |
| 295 | + | |
| 296 | +extern void lock(Lock*); | |
| 297 | +extern void unlock(Lock*); | |
| 298 | +extern int canlock(Lock*); | |
| 299 | + | |
| 300 | +typedef struct QLock QLock; | |
| 301 | +struct QLock | |
| 302 | +{ | |
| 303 | + Lock use; /* to access Qlock structure */ | |
| 304 | + Proc *head; /* next process waiting for object */ | |
| 305 | + Proc *tail; /* last process waiting for object */ | |
| 306 | + int locked; /* flag */ | |
| 307 | +}; | |
| 308 | + | |
| 309 | +extern void qlock(QLock*); | |
| 310 | +extern void qunlock(QLock*); | |
| 311 | +extern int canqlock(QLock*); | |
| 312 | +extern void _qlockinit(ulong (*)(ulong, ulong)); /* called only by the thread library */ | |
| 313 | + | |
| 314 | +typedef | |
| 315 | +struct RWLock | |
| 316 | +{ | |
| 317 | + Lock l; /* Lock modify lock */ | |
| 318 | + QLock x; /* Mutual exclusion lock */ | |
| 319 | + QLock k; /* Lock for waiting writers */ | |
| 320 | + int readers; /* Count of readers in lock */ | |
| 321 | +} RWLock; | |
| 322 | + | |
| 323 | +extern int canrlock(RWLock*); | |
| 324 | +extern int canwlock(RWLock*); | |
| 325 | +extern void rlock(RWLock*); | |
| 326 | +extern void runlock(RWLock*); | |
| 327 | +extern void wlock(RWLock*); | |
| 328 | +extern void wunlock(RWLock*); | |
| 329 | + | |
| 330 | +/* | |
| 331 | + * network dialing | |
| 332 | + */ | |
| 333 | +#define NETPATHLEN 40 | |
| 334 | + | |
| 335 | +/* | |
| 336 | + * system calls | |
| 337 | + * | |
| 338 | + */ | |
| 339 | +#define STATMAX 65535U /* max length of machine-independent stat structure */ | |
| 340 | +#define DIRMAX (sizeof(Dir)+STATMAX) /* max length of Dir structure */ | |
| 341 | +#define ERRMAX 128 /* max length of error string */ | |
| 342 | + | |
| 343 | +#define MORDER 0x0003 /* mask for bits defining order of mounting */ | |
| 344 | +#define MREPL 0x0000 /* mount replaces object */ | |
| 345 | +#define MBEFORE 0x0001 /* mount goes before others in union directory */ | |
| 346 | +#define MAFTER 0x0002 /* mount goes after others in union directory */ | |
| 347 | +#define MCREATE 0x0004 /* permit creation in mounted directory */ | |
| 348 | +#define MCACHE 0x0010 /* cache some data */ | |
| 349 | +#define MMASK 0x0017 /* all bits on */ | |
| 350 | + | |
| 351 | +#define OREAD 0 /* open for read */ | |
| 352 | +#define OWRITE 1 /* write */ | |
| 353 | +#define ORDWR 2 /* read and write */ | |
| 354 | +#define OEXEC 3 /* execute, == read but check execute permission */ | |
| 355 | +#define OTRUNC 16 /* or'ed in (except for exec), truncate file first */ | |
| 356 | +#define OCEXEC 32 /* or'ed in, close on exec */ | |
| 357 | +#define ORCLOSE 64 /* or'ed in, remove on close */ | |
| 358 | +#define OEXCL 0x1000 /* or'ed in, exclusive use (create only) */ | |
| 359 | + | |
| 360 | +#define AEXIST 0 /* accessible: exists */ | |
| 361 | +#define AEXEC 1 /* execute access */ | |
| 362 | +#define AWRITE 2 /* write access */ | |
| 363 | +#define AREAD 4 /* read access */ | |
| 364 | + | |
| 365 | +/* bits in Qid.type */ | |
| 366 | +#define QTDIR 0x80 /* type bit for directories */ | |
| 367 | +#define QTAPPEND 0x40 /* type bit for append only files */ | |
| 368 | +#define QTEXCL 0x20 /* type bit for exclusive use files */ | |
| 369 | +#define QTMOUNT 0x10 /* type bit for mounted channel */ | |
| 370 | +#define QTAUTH 0x08 /* type bit for authentication file */ | |
| 371 | +#define QTFILE 0x00 /* plain file */ | |
| 372 | + | |
| 373 | +/* bits in Dir.mode */ | |
| 374 | +#define DMDIR 0x80000000 /* mode bit for directories */ | |
| 375 | +#define DMAPPEND 0x40000000 /* mode bit for append only files */ | |
| 376 | +#define DMEXCL 0x20000000 /* mode bit for exclusive use files */ | |
| 377 | +#define DMMOUNT 0x10000000 /* mode bit for mounted channel */ | |
| 378 | +#define DMAUTH 0x08000000 /* mode bit for authentication file */ | |
| 379 | +#define DMREAD 0x4 /* mode bit for read permission */ | |
| 380 | +#define DMWRITE 0x2 /* mode bit for write permission */ | |
| 381 | +#define DMEXEC 0x1 /* mode bit for execute permission */ | |
| 382 | + | |
| 383 | +typedef | |
| 384 | +struct Qid | |
| 385 | +{ | |
| 386 | + uvlong path; | |
| 387 | + ulong vers; | |
| 388 | + uchar type; | |
| 389 | +} Qid; | |
| 390 | + | |
| 391 | +typedef | |
| 392 | +struct Dir { | |
| 393 | + /* system-modified data */ | |
| 394 | + ushort type; /* server type */ | |
| 395 | + uint dev; /* server subtype */ | |
| 396 | + /* file data */ | |
| 397 | + Qid qid; /* unique id from server */ | |
| 398 | + ulong mode; /* permissions */ | |
| 399 | + ulong atime; /* last read time */ | |
| 400 | + ulong mtime; /* last write time */ | |
| 401 | + vlong length; /* file length */ | |
| 402 | + char *name; /* last element of path */ | |
| 403 | + char *uid; /* owner name */ | |
| 404 | + char *gid; /* group name */ | |
| 405 | + char *muid; /* last modifier name */ | |
| 406 | +} Dir; | |
| 407 | + | |
| 408 | +extern Dir* dirstat(char*); | |
| 409 | +extern Dir* dirfstat(int); | |
| 410 | +extern int dirwstat(char*, Dir*); | |
| 411 | +extern int dirfwstat(int, Dir*); | |
| 412 | +extern long dirread(int, Dir**); | |
| 413 | +extern void nulldir(Dir*); | |
| 414 | +extern long dirreadall(int, Dir**); | |
| 415 | + | |
| 416 | +typedef | |
| 417 | +struct Waitmsg | |
| 418 | +{ | |
| 419 | + int pid; /* of loved one */ | |
| 420 | + ulong time[3]; /* of loved one & descendants */ | |
| 421 | + char *msg; | |
| 422 | +} Waitmsg; | |
| 423 | + | |
| 424 | +extern void _exits(char*); | |
| 425 | + | |
| 426 | +extern void exits(char*); | |
| 427 | +extern int create(char*, int, int); | |
| 428 | +extern int errstr(char*, uint); | |
| 429 | + | |
| 430 | +extern void perror(const char*); | |
| 431 | +extern long readn(int, void*, long); | |
| 432 | +extern int remove(const char*); | |
| 433 | +extern void rerrstr(char*, uint); | |
| 434 | +extern vlong seek(int, vlong, int); | |
| 435 | +extern int segflush(void*, ulong); | |
| 436 | +extern void werrstr(char*, ...); | |
| 437 | + | |
| 438 | +extern char *argv0; | |
| 439 | +#define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ | |
| 440 | + argv[0] && argv[0][0]=='-' && argv[0][1];\ | |
| 441 | + argc--, argv++) {\ | |
| 442 | + char *_args, *_argt;\ | |
| 443 | + Rune _argc;\ | |
| 444 | + _args = &argv[0][1];\ | |
| 445 | + if(_args[0]=='-' && _args[1]==0){\ | |
| 446 | + argc--; argv++; break;\ | |
| 447 | + }\ | |
| 448 | + _argc = 0;\ | |
| 449 | + while(*_args && (_args += chartorune(&_argc, _args)))\ | |
| 450 | + switch(_argc) | |
| 451 | +#define ARGEND SET(_argt);USED(_argt);USED(_argc); USED(_args);}USED(argv); USED(argc); | |
| 452 | +#define ARGF() (_argt=_args, _args="",\ | |
| 453 | + (*_argt? _argt: argv[1]? (argc--, *++argv): 0)) | |
| 454 | +#define EARGF(x) (_argt=_args, _args="",\ | |
| 455 | + (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0))) | |
| 456 | + | |
| 457 | +#define ARGC() _argc | |
| 458 | + | |
| 459 | +/* | |
| 460 | + * Extensions for Inferno to basic libc.h | |
| 461 | + */ | |
| 462 | + | |
| 463 | +#define setbinmode() | |
| 464 | + | |