1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include "authcmdlib.h"
6
7 void
clrbio(Acctbio * a)8 clrbio(Acctbio *a)
9 {
10 int i;
11
12 if(a->user)
13 free(a->user);
14 if(a->name)
15 free(a->name);
16 if(a->dept)
17 free(a->dept);
18 for(i = 0; i < Nemail; i++)
19 if(a->email[i])
20 free(a->email[i]);
21 memset(a, 0, sizeof(Acctbio));
22 }
23
24 int
ordbio(Biobuf * b,Acctbio * a)25 ordbio(Biobuf *b, Acctbio *a)
26 {
27 char *p, *cp, *next;
28 int ne;
29
30 clrbio(a);
31 while(p = Brdline(b, '\n')){
32 if(*p == '\n')
33 continue;
34
35 p[Blinelen(b)-1] = 0;
36
37 /* get user */
38 for(cp = p; *cp && *cp != ' ' && *cp != '\t'; cp++)
39 ;
40 a->user = malloc(cp - p + 1);
41 strncpy(a->user, p, cp - p);
42 a->user[cp - p] = 0;
43 p = cp;
44
45 /* get name */
46 while(*p == ' ' || *p == '\t')
47 p++;
48 for(cp = p; *cp; cp++){
49 if(isdigit(*cp) || *cp == '<'){
50 while(cp > p && *(cp-1) != ' ' && *(cp-1) != '\t')
51 cp--;
52 break;
53 }
54 }
55 next = cp;
56 while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
57 cp--;
58 a->name = malloc(cp - p + 1);
59 strncpy(a->name, p, cp - p);
60 a->name[cp - p] = 0;
61 p = next;
62
63 /* get dept */
64 for(cp = p; *cp; cp++){
65 if(*cp == '<')
66 break;
67 }
68 next = cp;
69 while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
70 cp--;
71 a->dept = malloc(cp - p + 1);
72 strncpy(a->dept, p, cp - p);
73 a->dept[cp - p] = 0;
74 p = next;
75
76 /* get emails */
77 ne = 0;
78 for(cp = p; *cp && ne < Nemail;){
79 if(*cp != '<'){
80 cp++;
81 continue;
82 }
83 p = ++cp;
84 while(*cp && *cp != '>')
85 cp++;
86 if(cp == p)
87 break;
88 a->email[ne] = malloc(cp - p + 1);
89 strncpy(a->email[ne], p, cp - p);
90 a->email[ne][cp-p] = 0;
91 ne++;
92 }
93 return 0;
94 }
95 return -1;
96 }
97
98 void
nwrbio(Biobuf * b,Acctbio * a)99 nwrbio(Biobuf *b, Acctbio *a)
100 {
101 int i;
102
103 if(a->postid == 0)
104 a->postid = "";
105 if(a->name == 0)
106 a->name = "";
107 if(a->dept == 0)
108 a->dept = "";
109 if(a->email[0] == 0)
110 a->email[0] = strdup(a->user);
111
112 Bprint(b, "%s|%s|%s|%s|%s", a->user, a->user, a->name, a->dept, a->email[0]);
113 for(i = 1; i < Nemail; i++){
114 if(a->email[i] == 0)
115 break;
116 Bprint(b, "|%s", a->email[i]);
117 }
118 Bprint(b, "\n");
119 }
120
121 void
main(void)122 main(void)
123 {
124 Biobuf in, out;
125 Acctbio a;
126
127 Binit(&in, 0, OREAD);
128 Binit(&out, 1, OWRITE);
129 while(ordbio(&in, &a) == 0)
130 nwrbio(&out, &a);
131 Bterm(&in);
132 Bterm(&out);
133 }
134