jehanne/sys/src/cmd/auth/convbio.c

149 lines
2.9 KiB
C

/*
* This file is part of the UCB release of Plan 9. It is subject to the license
* terms in the LICENSE file found in the top-level directory of this
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
* part of the UCB release of Plan 9, including this file, may be copied,
* modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/
/* Portions of this file are Copyright (C) 2015-2018 Giacomo Tesio <giacomo@tesio.it>
* See /doc/license/gpl-2.0.txt for details about the licensing.
*/
/* Portions of this file are Copyright (C) 9front's team.
* See /doc/license/9front-mit for details about the licensing.
* See http://git.9front.org/plan9front/plan9front/HEAD/info.html for a list of authors.
*/
#include <u.h>
#include <lib9.h>
#include <bio.h>
#include <chartypes.h>
#include "authcmdlib.h"
void
clrbio(Acctbio *a)
{
int i;
if(a->user)
free(a->user);
if(a->name)
free(a->name);
if(a->dept)
free(a->dept);
for(i = 0; i < Nemail; i++)
if(a->email[i])
free(a->email[i]);
memset(a, 0, sizeof(Acctbio));
}
int
ordbio(Biobuf *b, Acctbio *a)
{
char *p, *cp, *next;
int ne;
clrbio(a);
while(p = Brdline(b, '\n')){
if(*p == '\n')
continue;
p[Blinelen(b)-1] = 0;
/* get user */
for(cp = p; *cp && *cp != ' ' && *cp != '\t'; cp++)
;
a->user = malloc(cp - p + 1);
strncpy(a->user, p, cp - p);
a->user[cp - p] = 0;
p = cp;
/* get name */
while(*p == ' ' || *p == '\t')
p++;
for(cp = p; *cp; cp++){
if(isdigit(*cp) || *cp == '<'){
while(cp > p && *(cp-1) != ' ' && *(cp-1) != '\t')
cp--;
break;
}
}
next = cp;
while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
cp--;
a->name = malloc(cp - p + 1);
strncpy(a->name, p, cp - p);
a->name[cp - p] = 0;
p = next;
/* get dept */
for(cp = p; *cp; cp++){
if(*cp == '<')
break;
}
next = cp;
while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
cp--;
a->dept = malloc(cp - p + 1);
strncpy(a->dept, p, cp - p);
a->dept[cp - p] = 0;
p = next;
/* get emails */
ne = 0;
for(cp = p; *cp && ne < Nemail;){
if(*cp != '<'){
cp++;
continue;
}
p = ++cp;
while(*cp && *cp != '>')
cp++;
if(cp == p)
break;
a->email[ne] = malloc(cp - p + 1);
strncpy(a->email[ne], p, cp - p);
a->email[ne][cp-p] = 0;
ne++;
}
return 0;
}
return -1;
}
void
nwrbio(Biobuf *b, Acctbio *a)
{
int i;
if(a->postid == 0)
a->postid = "";
if(a->name == 0)
a->name = "";
if(a->dept == 0)
a->dept = "";
if(a->email[0] == 0)
a->email[0] = strdup(a->user);
Bprint(b, "%s|%s|%s|%s|%s", a->user, a->user, a->name, a->dept, a->email[0]);
for(i = 1; i < Nemail; i++){
if(a->email[i] == 0)
break;
Bprint(b, "|%s", a->email[i]);
}
Bprint(b, "\n");
}
void
main(void)
{
Biobuf in, out;
Acctbio a;
Binit(&in, 0, OREAD);
Binit(&out, 1, OWRITE);
while(ordbio(&in, &a) == 0)
nwrbio(&out, &a);
Bterm(&in);
Bterm(&out);
}