#include "hsubs.h"
#include "quotas.h"

extern uint lent;
struct qent q;
char tmp[64];

struct oq {
	char ln[9];
	short	uid,gid;
	char	flg;
	char	fs[8];
	uint	dquo,fquo,dused,fused;
};

char qbuf[5120];
main()
{
	short qfd, ofd;
	register struct oq *poq, *pol;
	register short r;

	if((qfd = creat("/accting/quofile", 0600)) == -1){
		printf("cant creat new quofile\n");
		exit(2);
	}
	chown("/accting/quofile", 3, 2);
	lseek(qfd, (long)(NHENT)*QSZ, 0);
	lseek(qfd, -(long)(QSZ), 1);
	write(qfd, qbuf, QSZ);
	close(qfd);
	qfd = open("/accting/quofile", 2);

	if((ofd = open("/etc/quofile", 0)) == -1){
		printf("cant open old quofile\n");
		exit(3);
	}

	while((r = read(ofd, qbuf, 5120)) > 0 && (r%32) == 0){
		for(poq = qbuf, pol = &qbuf[r]; poq < pol; poq++){
			if(getqent(qfd, tmp, poq->ln)){
				printf("Dup %s\n", poq->ln);
				continue;
			}
			lseek(qfd, (long)(lent)*QSZ, 0);
			strncpy(q.q_ln, poq->ln, 9);
			q.q_dquo = poq->dquo;
			q.q_fquo = poq->fquo;
			q.q_dused = poq->dused;
			q.q_fused = poq->fused;
			q.q_flg = poq->flg;
			q.q_uid = poq->uid;
			q.q_gid = poq->gid;
			strncpy(q.q_fs, (poq->fs)+1, 6);
			q.q_mused = q.q_mquo = 0;
			if(q.q_uid >= 10000)q.q_mquo = 80;
			write(qfd, &q, QSZ);
			nullout(q.q_ln, 9);
			nullout(q.q_fs, 6);
		}
	}
	close(qfd);
	close(ofd);
}
nullout(s, c)
register char *s;
register short c;
{
	while(c--)*s++ = 0;
}
