diff options
author | rsc <devnull@localhost> | 2005-10-29 16:26:44 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2005-10-29 16:26:44 +0000 |
commit | 5cdb17983ae6e6367ad7a940cb219eab247a9304 (patch) | |
tree | 8ca1ef49af2a96e7daebe624d91fdf679814a057 /src/cmd/upas/filterkit/readaddrs.c | |
parent | cd3745196389579fb78b9b01ef1daefb5a57aa71 (diff) | |
download | plan9port-5cdb17983ae6e6367ad7a940cb219eab247a9304.tar.gz plan9port-5cdb17983ae6e6367ad7a940cb219eab247a9304.tar.bz2 plan9port-5cdb17983ae6e6367ad7a940cb219eab247a9304.zip |
Thanks to John Cummings.
Diffstat (limited to 'src/cmd/upas/filterkit/readaddrs.c')
-rw-r--r-- | src/cmd/upas/filterkit/readaddrs.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/cmd/upas/filterkit/readaddrs.c b/src/cmd/upas/filterkit/readaddrs.c new file mode 100644 index 00000000..9aadc1ab --- /dev/null +++ b/src/cmd/upas/filterkit/readaddrs.c @@ -0,0 +1,98 @@ +#include <u.h> +#include <libc.h> +#include "dat.h" + +void* +emalloc(int size) +{ + void *a; + + a = mallocz(size, 1); + if(a == nil) + sysfatal("%r"); + return a; +} + +char* +estrdup(char *s) +{ + s = strdup(s); + if(s == nil) + sysfatal("%r"); + return s; +} + +/* + * like tokenize but obey "" quoting + */ +int +tokenize822(char *str, char **args, int max) +{ + int na; + int intok = 0, inquote = 0; + + if(max <= 0) + return 0; + for(na=0; ;str++) + switch(*str) { + case ' ': + case '\t': + if(inquote) + goto Default; + /* fall through */ + case '\n': + *str = 0; + if(!intok) + continue; + intok = 0; + if(na < max) + continue; + /* fall through */ + case 0: + return na; + case '"': + inquote ^= 1; + /* fall through */ + Default: + default: + if(intok) + continue; + args[na++] = str; + intok = 1; + } + return 0; /* can't get here; silence compiler */ +} + +Addr* +readaddrs(char *file, Addr *a) +{ + int fd; + int i, n; + char buf[8*1024]; + char *f[128]; + Addr **l; + Addr *first; + + /* add to end */ + first = a; + for(l = &first; *l != nil; l = &(*l)->next) + ; + + /* read in the addresses */ + fd = open(file, OREAD); + if(fd < 0) + return first; + n = read(fd, buf, sizeof(buf)-1); + close(fd); + if(n <= 0) + return first; + buf[n] = 0; + + n = tokenize822(buf, f, nelem(f)); + for(i = 0; i < n; i++){ + *l = a = emalloc(sizeof *a); + l = &a->next; + a->val = estrdup(f[i]); + } + return first; +} |