diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/lib/v |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/lib/v')
-rwxr-xr-x | sys/src/ape/lib/v/error.c | 14 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/getfields.c | 136 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/max.c | 7 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/min.c | 7 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/mkfile | 21 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/nap.c | 9 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/nrand.c | 24 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/plan9/getpass.c | 42 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/plan9/tty.c | 37 | ||||
-rwxr-xr-x | sys/src/ape/lib/v/rand.c | 64 |
10 files changed, 361 insertions, 0 deletions
diff --git a/sys/src/ape/lib/v/error.c b/sys/src/ape/lib/v/error.c new file mode 100755 index 000000000..6dccc463e --- /dev/null +++ b/sys/src/ape/lib/v/error.c @@ -0,0 +1,14 @@ +#define _POSIX_SOURCE +#define _RESEARCH_SOURCE +#include <error.h> +#include <stdio.h> +#include <libv.h> + +char *_progname; + +void +_perror(char *s) +{ + fprintf(stderr, "%s: ", _progname); + perror(s); +} diff --git a/sys/src/ape/lib/v/getfields.c b/sys/src/ape/lib/v/getfields.c new file mode 100755 index 000000000..da59216d9 --- /dev/null +++ b/sys/src/ape/lib/v/getfields.c @@ -0,0 +1,136 @@ +#define _RESEARCH_SOURCE +#include <libv.h> +#include <string.h> + +static char is_sep[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 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, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; +static char last_sep[256]; + +char * +setfields(char *arg) +{ + register unsigned char *s; + register i; + + for(i = 1, s = (unsigned char *)last_sep; i < 256; i++) + if(is_sep[i]) + *s++ = i; + *s = 0; + memset(is_sep, 0, sizeof is_sep); + memset(is_field, 1, sizeof is_field); + for(s = (unsigned char *)arg; *s;){ + is_sep[*s] = 1; + is_field[*s++] = 0; + } + is_field[0] = 0; + return(last_sep); +} + +int +getfields(char *ss, char **sp, int nptrs) +{ + register unsigned char *s = (unsigned char *)ss; + register unsigned char **p = (unsigned char **)sp; + register unsigned c; + + for(;;){ + if(--nptrs < 0) break; + *p++ = s; + while(is_field[c = *s++]) + ; + if(c == 0) break; + s[-1] = 0; + } + if(nptrs > 0) + *p = 0; + else if(--s >= (unsigned char *)ss) + *s = c; + return(p - (unsigned char **)sp); +} + +int +getmfields(char *ss, char **sp, int nptrs) +{ + register unsigned char *s = (unsigned char *)ss; + register unsigned char **p = (unsigned char **)sp; + register unsigned c; + + if(nptrs <= 0) + return(0); + goto flushdelim; + for(;;){ + *p++ = s; + if(--nptrs == 0) break; + while(is_field[c = *s++]) + ; + /* + * s is now pointing 1 past the delimiter of the last field + * c is the delimiter + */ + if(c == 0) break; + s[-1] = 0; + flushdelim: + while(is_sep[c = *s++]) + ; + /* + * s is now pointing 1 past the beginning of the next field + * c is the first letter of the field + */ + if(c == 0) break; + s--; + /* + * s is now pointing to the beginning of the next field + * c is the first letter of the field + */ + } + if(nptrs > 0) + *p = 0; + return(p - (unsigned char **)sp); +} + +#ifdef MAIN +#include <fio.h> + +main() +{ + char *fields[256]; + char *s; + int n, i; + char buf[1024]; + + print("go:\n"); + while(s = Frdline(0)){ + strcpy(buf, s); + Fprint(1, "getf:"); + n = getfields(s, fields, 4); + for(i = 0; i < n; i++) + Fprint(1, " >%s<", fields[i]); + Fputc(1, '\n'); + Fprint(1, "getmf:"); + n = getmfields(buf, fields, 4); + for(i = 0; i < n; i++) + Fprint(1, " >%s<", fields[i]); + Fputc(1, '\n'); + Fflush(1); + } + exit(0); +} +#endif diff --git a/sys/src/ape/lib/v/max.c b/sys/src/ape/lib/v/max.c new file mode 100755 index 000000000..c8a105567 --- /dev/null +++ b/sys/src/ape/lib/v/max.c @@ -0,0 +1,7 @@ +#define _RESEARCH_SOURCE +#include <libv.h> + +max(int a, int b) +{ + return (a>b? a: b); +} diff --git a/sys/src/ape/lib/v/min.c b/sys/src/ape/lib/v/min.c new file mode 100755 index 000000000..f0502af0c --- /dev/null +++ b/sys/src/ape/lib/v/min.c @@ -0,0 +1,7 @@ +#define _RESEARCH_SOURCE +#include <libv.h> + +min(int a, int b) +{ + return (a<b? a: b); +} diff --git a/sys/src/ape/lib/v/mkfile b/sys/src/ape/lib/v/mkfile new file mode 100755 index 000000000..fa3c8217f --- /dev/null +++ b/sys/src/ape/lib/v/mkfile @@ -0,0 +1,21 @@ +APE=/sys/src/ape +<$APE/config + +LIB=/$objtype/lib/ape/libv.a + +OFILES=getpass.$O\ + tty.$O\ + rand.$O\ + nrand.$O\ + getfields.$O\ + min.$O\ + max.$O\ + error.$O\ + nap.$O + +</sys/src/cmd/mksyslib + +CFLAGS=-c + +%.$O: $FAMILY/%.c + $CC $CFLAGS $prereq diff --git a/sys/src/ape/lib/v/nap.c b/sys/src/ape/lib/v/nap.c new file mode 100755 index 000000000..490857d27 --- /dev/null +++ b/sys/src/ape/lib/v/nap.c @@ -0,0 +1,9 @@ +nap(int n) +{ + register i; + + while(n-- > 0){ + for(i = 0; i < 1000*1000*10; i++) + ; + } +} diff --git a/sys/src/ape/lib/v/nrand.c b/sys/src/ape/lib/v/nrand.c new file mode 100755 index 000000000..29e5aee0f --- /dev/null +++ b/sys/src/ape/lib/v/nrand.c @@ -0,0 +1,24 @@ +#include <stdlib.h> + +#define MASK 0x7FFFFFFFL +#define FRACT (1.0 / (MASK + 1.0)) + +extern long lrand(void); + +double +frand(void) +{ + + return lrand() * FRACT; +} + +nrand(int n) +{ + long slop, v; + + slop = MASK % n; + do + v = lrand(); + while(v <= slop); + return v % n; +} diff --git a/sys/src/ape/lib/v/plan9/getpass.c b/sys/src/ape/lib/v/plan9/getpass.c new file mode 100755 index 000000000..f8f6a8e89 --- /dev/null +++ b/sys/src/ape/lib/v/plan9/getpass.c @@ -0,0 +1,42 @@ +#define _POSIX_SOURCE +#define _RESEARCH_SOURCE +#include <stdio.h> +#include <signal.h> +#include <libv.h> + +char * +getpass(char *prompt) +{ + int c; + char *p; + FILE *fi; + static char pbuf[9]; + void (*sig)(int); + + if ((fi = fopen("/dev/cons", "r")) == NULL) + fi = stdin; + else + setbuf(fi, NULL); + sig = signal(SIGINT, SIG_IGN); + tty_echooff(fileno(fi)); + fprintf(stderr, "%s", prompt); + fflush(stderr); + + for (p = pbuf; (c = getc(fi)) != '\n' && c != EOF; ) + if (c == ('u' & 037)) + p = pbuf; + else if (c == '\b') { + if (p > pbuf) + p--; + } else if (p < &pbuf[8]) + *p++ = c; + *p = '\0'; + + fprintf(stderr, "\n"); + fflush(stderr); + tty_echoon(fileno(fi)); + signal(SIGINT, sig); + if (fi != stdin) + fclose(fi); + return(pbuf); +} diff --git a/sys/src/ape/lib/v/plan9/tty.c b/sys/src/ape/lib/v/plan9/tty.c new file mode 100755 index 000000000..dae26d48f --- /dev/null +++ b/sys/src/ape/lib/v/plan9/tty.c @@ -0,0 +1,37 @@ +/* + * turn raw (no echo, etc.) on and off. + * ptyfs is gone, so don't even try tcsetattr, etc. + */ +#define _POSIX_SOURCE +#define _RESEARCH_SOURCE + +#include <sys/types.h> +#include <unistd.h> +#include <fcntl.h> +#include <libv.h> + +static int ctlfd = -1; + +/* fd is ignored */ + +tty_echooff(int fd) +{ + if(ctlfd >= 0) + return 0; + ctlfd = open("/dev/consctl", O_WRONLY); + if(ctlfd < 0) + return -1; + write(ctlfd, "rawon", 5); + return 0; +} + +tty_echoon(int fd) +{ + if(ctlfd >= 0){ + write(ctlfd, "rawoff", 6); + close(ctlfd); + ctlfd = -1; + return 0; + } + return -1; +} diff --git a/sys/src/ape/lib/v/rand.c b/sys/src/ape/lib/v/rand.c new file mode 100755 index 000000000..4996b0c26 --- /dev/null +++ b/sys/src/ape/lib/v/rand.c @@ -0,0 +1,64 @@ +#define _RESEARCH_SOURCE +#include <stdlib.h> +#include <libv.h> +/* + random number generator from cacm 31 10, oct 88 + for 32 bit integers (called long here) +*/ + +#ifdef MAIN +#define A 16807 +#define M 2147483647 +#define Q 127773 +#define R 2836 +#else +#define A 48271 +#define M 2147483647 +#define Q 44488 +#define R 3399 +#endif + +static long seed = 1; + +void +srand(unsigned int newseed) +{ + seed = newseed; +} + +long +lrand(void) +{ + long lo, hi, test; + + hi = seed/Q; + lo = seed%Q; + test = A*lo - R*hi; + if(test > 0) + seed = test; + else + seed = test+M; + return(seed); +} + +int +rand(void) +{ + return lrand()%(RAND_MAX+1); +} + +#ifdef MAIN + +main() +{ + int i; + + for(i = 0; i < 10000; i++) + rand(); + if(seed == 1043618065) + printf(" rand: pass\n"); + else + printf("*****rand: fail; seed=%u, should be 1043618065\n", seed); + exit(0); +} +#endif |