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

char	*QFILE;
struct	qent	q;
short	qfd;
char	nulls[64];
extern	uint	lent;
struct	qent	trash;


main(argc,argv)
char **argv;
{
	if(argc < 2){
bad:
		printf("quodel: Usage: quodel [-f quotafile] name ... ...\n");
		exit(1);
	}

	if(**++argv == '-'){
		if(argc < 4)
			goto bad;
		argc -= 2;
		QFILE = *++argv;
		argv++;
	}
	else	QFILE = QUOFILE;
	if((qfd = open(QFILE, 2)) == -1){
		printf("Can not open quotafile %s\n", QFILE);
		exit(3);
	}
	while(--argc)
		delete(*argv++);
	close(qfd);
	exit(0);
}
delete(s)
char *s;
{
	register uint nent;
	register uint tmp;
	static char blah[16];

	if((nent = getqent(qfd, &q, s)) == 0){
		printf("\"%s\" not found in %s\n", s, QFILE);
		return;
	}
	lseek(qfd, (long)(nent)*QSZ, 0);
	write(qfd, nulls, QSZ);
	if(++nent >= NHENT)
		nent = 0;

lp:
	lseek(qfd, (long)(nent)*QSZ, 0);
	if(read(qfd, &q, QSZ) != QSZ){
		printf("Quofile read error nent=%d\n", nent);
		exit(3);
	}
	if(q.q_ln[0] == 0)return;
	strncpy(blah, q.q_ln, 8);
	blah[8] = 0;
	lseek(qfd, -(long)(QSZ), 1);
delit:
	write(qfd, nulls, QSZ);
	if(tmp = getqent(qfd, &trash, blah)){
		printf("Duplicate entry in quofile for %8.8s -- removed\n",
			trash.q_ln);
		lseek(qfd, (long)(tmp)*QSZ, 0);
		goto delit;
	}
	lseek(qfd, (long)(lent)*QSZ, 0);
	write(qfd, &q, QSZ);
	if(++nent >= NHENT)
		nent = 0;
	goto lp;
}
