shithub: riscv

ref: 52e4e51b50589ecb2b06559c28b7ce9f682815be
dir: /sys/src/cmd/auth/lib/wrbio.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
#include <authsrv.h>
#include "authcmdlib.h"

void
wrbio(char *file, Acctbio *a)
{
	char buf[1024];
	int i, fd, n;

	fd = open(file, OWRITE);
	if(fd < 0){
		fd = create(file, OWRITE, 0660);
		if(fd < 0)
			error("can't create %s", file);
	}
	if(seek(fd, 0, 2) < 0)
		error("can't seek %s", file);

	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);

	n = 0;
	n += snprint(buf+n, sizeof(buf)-n, "%s|%s|%s|%s",
		a->user, a->postid, a->name, a->dept);
	for(i = 0; i < Nemail; i++){
		if(a->email[i] == 0)
			break;
		n += snprint(buf+n, sizeof(buf)-n, "|%s", a->email[i]);
	}
	n += snprint(buf+n, sizeof(buf)-n, "\n");

	write(fd, buf, n);
	close(fd);
}