xref: /plan9-contrib/sys/src/cmd/auth/lib/rdbio.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <auth.h>
6 #include "authsrv.h"
7 
8 void
9 clrbio(Acctbio *a)
10 {
11 	int i;
12 
13 	if(a->user)
14 		free(a->user);
15 	if(a->name)
16 		free(a->name);
17 	if(a->dept)
18 		free(a->dept);
19 	for(i = 0; i < Nemail; i++)
20 		if(a->email[i])
21 			free(a->email[i]);
22 	memset(a, 0, sizeof(Acctbio));
23 }
24 
25 void
26 rdbio(char *file, char *user, Acctbio *a)
27 {
28 	Biobuf *b;
29 	char *p, *cp, *next;
30 	int ne, ulen;
31 
32 	memset(a, 0, sizeof(Acctbio));
33 	b = Bopen(file, OREAD);
34 	if(b == 0)
35 		return;
36 	ulen = strlen(user);
37 	while(p = Brdline(b, '\n')){
38 		if(strncmp(p, user, ulen) != 0)
39 			continue;
40 		if(p[ulen] && p[ulen] != ' ' && p[ulen] != '\t')
41 			continue;
42 
43 		p[Blinelen(b)-1] = 0;
44 		p += ulen;
45 		clrbio(a);
46 
47 		/* get name */
48 		while(*p == ' ' || *p == '\t')
49 			p++;
50 		for(cp = p; *cp; cp++){
51 			if(isdigit(*cp) || *cp == '<'){
52 				while(cp > p && *(cp-1) != ' ' && *(cp-1) != '\t')
53 					cp--;
54 				break;
55 			}
56 		}
57 		next = cp;
58 		while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
59 			cp--;
60 		a->name = malloc(cp - p + 1);
61 		strncpy(a->name, p, cp - p);
62 		a->name[cp - p] = 0;
63 		p = next;
64 
65 		/* get dept */
66 		for(cp = p; *cp; cp++){
67 			if(*cp == '<')
68 				break;
69 		}
70 		next = cp;
71 		while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
72 			cp--;
73 		a->dept = malloc(cp - p + 1);
74 		strncpy(a->dept, p, cp - p);
75 		a->dept[cp - p] = 0;
76 		p = next;
77 
78 		/* get emails */
79 		ne = 0;
80 		for(cp = p; *cp && ne < Nemail;){
81 			if(*cp != '<'){
82 				cp++;
83 				continue;
84 			}
85 			p = ++cp;
86 			while(*cp && *cp != '>')
87 				cp++;
88 			if(cp == p)
89 				break;
90 			a->email[ne] = malloc(cp - p + 1);
91 			strncpy(a->email[ne], p, cp - p);
92 			a->email[ne][cp-p] = 0;
93 			ne++;
94 		}
95 	}
96 	a->user = strdup(user);
97 	Bterm(b);
98 }
99