xref: /plan9/sys/src/cmd/webfs/util.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ndb.h>
5 #include <fcall.h>
6 #include <thread.h>
7 #include <9p.h>
8 #include <ctype.h>
9 #include "dat.h"
10 #include "fns.h"
11 
12 void*
erealloc(void * a,uint n)13 erealloc(void *a, uint n)
14 {
15 	a = realloc(a, n);
16 	if(a == nil)
17 		sysfatal("realloc %d: out of memory", n);
18 	setrealloctag(a, getcallerpc(&a));
19 	return a;
20 }
21 
22 void*
emalloc(uint n)23 emalloc(uint n)
24 {
25 	void *a;
26 
27 	a = mallocz(n, 1);
28 	if(a == nil)
29 		sysfatal("malloc %d: out of memory", n);
30 	setmalloctag(a, getcallerpc(&n));
31 	return a;
32 }
33 
34 char*
estrdup(char * s)35 estrdup(char *s)
36 {
37 	s = strdup(s);
38 	if(s == nil)
39 		sysfatal("strdup: out of memory");
40 	setmalloctag(s, getcallerpc(&s));
41 	return s;
42 }
43 
44 char*
estredup(char * s,char * e)45 estredup(char *s, char *e)
46 {
47 	char *t;
48 
49 	t = emalloc(e-s+1);
50 	memmove(t, s, e-s);
51 	t[e-s] = '\0';
52 	setmalloctag(t, getcallerpc(&s));
53 	return t;
54 }
55 
56 char*
estrmanydup(char * s,...)57 estrmanydup(char *s, ...)
58 {
59 	char *p, *t;
60 	int len;
61 	va_list arg;
62 
63 	len = strlen(s);
64 	va_start(arg, s);
65 	while((p = va_arg(arg, char*)) != nil)
66 		len += strlen(p);
67 	len++;
68 
69 	t = emalloc(len);
70 	strcpy(t, s);
71 	va_start(arg, s);
72 	while((p = va_arg(arg, char*)) != nil)
73 		strcat(t, p);
74 	return t;
75 }
76 
77 char*
strlower(char * s)78 strlower(char *s)
79 {
80 	char *t;
81 
82 	for(t=s; *t; t++)
83 		if('A' <= *t && *t <= 'Z')
84 			*t += 'a'-'A';
85 	return s;
86 }
87