Blob
1 #include "stdinc.h"2 #include "dat.h"3 #include "fns.h"5 int6 namecmp(char *s, char *t)7 {8 return strncmp(s, t, ANameSize);9 }11 void12 namecp(char *dst, char *src)13 {14 strncpy(dst, src, ANameSize - 1);15 dst[ANameSize - 1] = '\0';16 }18 int19 nameok(char *name)20 {21 char *t;22 int c;24 if(name == nil)25 return -1;26 for(t = name; c = *t; t++)27 if(t - name >= ANameSize28 || c < ' ' || c >= 0x7f)29 return -1;30 return 0;31 }33 int34 stru32int(char *s, u32int *r)35 {36 char *t;37 u32int n, nn, m;38 int c;40 m = TWID32 / 10;41 n = 0;42 for(t = s; ; t++){43 c = *t;44 if(c < '0' || c > '9')45 break;46 if(n > m)47 return -1;48 nn = n * 10 + c - '0';49 if(nn < n)50 return -1;51 n = nn;52 }53 *r = n;54 return s != t && *t == '\0';55 }57 int58 stru64int(char *s, u64int *r)59 {60 char *t;61 u64int n, nn, m;62 int c;64 m = TWID64 / 10;65 n = 0;66 for(t = s; ; t++){67 c = *t;68 if(c < '0' || c > '9')69 break;70 if(n > m)71 return -1;72 nn = n * 10 + c - '0';73 if(nn < n)74 return -1;75 n = nn;76 }77 *r = n;78 return s != t && *t == '\0';79 }81 int82 vttypevalid(int type)83 {84 return type < VtMaxType;85 }87 static char*88 logit(int severity, char *fmt, va_list args)89 {90 char *s;92 s = vsmprint(fmt, args);93 if(s == nil)94 return nil;95 if(argv0 == nil)96 fprint(2, "%s: err %d: %s\n", argv0, severity, s);97 else98 fprint(2, "err %d: %s\n", severity, s);99 return s;100 }102 void103 seterr(int severity, char *fmt, ...)104 {105 char *s;106 va_list args;108 va_start(args, fmt);109 s = logit(severity, fmt, args);110 va_end(args);111 if(s == nil)112 werrstr("error setting error");113 else{114 werrstr("%s", s);115 free(s);116 }117 }119 void120 logerr(int severity, char *fmt, ...)121 {122 char *s;123 va_list args;125 va_start(args, fmt);126 s = logit(severity, fmt, args);127 va_end(args);128 free(s);129 }131 u32int132 now(void)133 {134 return time(nil);135 }137 int abortonmem = 1;139 void *140 emalloc(ulong n)141 {142 void *p;144 p = malloc(n);145 if(p == nil){146 if(abortonmem)147 abort();148 sysfatal("out of memory allocating %lud", n);149 }150 memset(p, 0xa5, n);151 if(0)print("emalloc %p-%p by %lux\n", p, (char*)p+n, getcallerpc(&n));152 return p;153 }155 void *156 ezmalloc(ulong n)157 {158 void *p;160 p = malloc(n);161 if(p == nil){162 if(abortonmem)163 abort();164 sysfatal("out of memory allocating %lud", n);165 }166 memset(p, 0, n);167 if(0)print("ezmalloc %p-%p by %lux\n", p, (char*)p+n, getcallerpc(&n));168 return p;169 }171 void *172 erealloc(void *p, ulong n)173 {174 p = realloc(p, n);175 if(p == nil){176 if(abortonmem)177 abort();178 sysfatal("out of memory allocating %lud", n);179 }180 if(0)print("erealloc %p-%p by %lux\n", p, (char*)p+n, getcallerpc(&p));181 return p;182 }184 char *185 estrdup(char *s)186 {187 char *t;188 int n;190 n = strlen(s) + 1;191 t = emalloc(n);192 memmove(t, s, n);193 if(0)print("estrdup %p-%p by %lux\n", t, (char*)t+n, getcallerpc(&s));194 return t;195 }197 /*198 * return floor(log2(v))199 */200 int201 u64log2(u64int v)202 {203 int i;205 for(i = 0; i < 64; i++)206 if((v >> i) <= 1)207 break;208 return i;209 }211 int212 vtproc(void (*fn)(void*), void *arg)213 {214 proccreate(fn, arg, 256*1024);215 return 0;216 }218 int219 ientryfmt(Fmt *fmt)220 {221 IEntry *ie;223 ie = va_arg(fmt->args, IEntry*);224 return fmtprint(fmt, "%V %22lld %3d %5d %3d",225 ie->score, ie->ia.addr, ie->ia.type, ie->ia.size, ie->ia.blocks);226 }228 void229 ventifmtinstall(void)230 {231 fmtinstall('F', vtfcallfmt);232 fmtinstall('H', encodefmt);233 fmtinstall('I', ientryfmt);234 fmtinstall('V', vtscorefmt);235 }237 uint238 msec(void)239 {240 return nsec()/1000000;241 }243 uint244 countbits(uint n)245 {246 n = (n&0x55555555)+((n>>1)&0x55555555);247 n = (n&0x33333333)+((n>>2)&0x33333333);248 n = (n&0x0F0F0F0F)+((n>>4)&0x0F0F0F0F);249 n = (n&0x00FF00FF)+((n>>8)&0x00FF00FF);250 n = (n&0x0000FFFF)+((n>>16)&0x0000FFFF);251 return n;252 }