Blob
1 #include <u.h>2 #include <libc.h>3 #include "dat.h"5 void*6 emalloc(int size)7 {8 void *a;10 a = mallocz(size, 1);11 if(a == nil)12 sysfatal("%r");13 return a;14 }16 char*17 estrdup(char *s)18 {19 s = strdup(s);20 if(s == nil)21 sysfatal("%r");22 return s;23 }25 /*26 * like tokenize but obey "" quoting27 */28 int29 tokenize822(char *str, char **args, int max)30 {31 int na;32 int intok = 0, inquote = 0;34 if(max <= 0)35 return 0;36 for(na=0; ;str++)37 switch(*str) {38 case ' ':39 case '\t':40 if(inquote)41 goto Default;42 /* fall through */43 case '\n':44 *str = 0;45 if(!intok)46 continue;47 intok = 0;48 if(na < max)49 continue;50 /* fall through */51 case 0:52 return na;53 case '"':54 inquote ^= 1;55 /* fall through */56 Default:57 default:58 if(intok)59 continue;60 args[na++] = str;61 intok = 1;62 }63 return 0; /* can't get here; silence compiler */64 }66 Addr*67 readaddrs(char *file, Addr *a)68 {69 int fd;70 int i, n;71 char buf[8*1024];72 char *f[128];73 Addr **l;74 Addr *first;76 /* add to end */77 first = a;78 for(l = &first; *l != nil; l = &(*l)->next)79 ;81 /* read in the addresses */82 fd = open(file, OREAD);83 if(fd < 0)84 return first;85 n = read(fd, buf, sizeof(buf)-1);86 close(fd);87 if(n <= 0)88 return first;89 buf[n] = 0;91 n = tokenize822(buf, f, nelem(f));92 for(i = 0; i < n; i++){93 *l = a = emalloc(sizeof *a);94 l = &a->next;95 a->val = estrdup(f[i]);96 }97 return first;98 }