#include <stdio.h>
#include <string.h>
#ifdef linux
#include <fcntl.h>
#include "../nolock.h"
#else
#include <sys/locking.h>
#endif
main()
{
	FILE           *inbuf, *outbuf;
	int             code;
	int             count;
	char            infile[99], outfile[99];
	int            datar;
	int		fds;
	printf("\nInput the old user file name: ");
	scanf("%s", infile);
	printf("\n");
	strcpy(outfile, infile);
	strcat(outfile, ".OLD");
	(void) unlink(outfile);
	(void) link(infile, outfile);
	(void) unlink(infile);
	if ((inbuf = fopen(outfile, "r")) == NULL) {
		printf("\n\rError opening old users file.\n\r");
		exit(1);
	}
	if ((outbuf = fopen(infile, "a+")) == NULL) {
		printf("\n\rError opening new users file.\n\r");
		exit(1);
	}
	fds = fileno(outbuf);
	rewind(outbuf);
	locking(fds, LK_LOCK, 0L);
	while ((datar = fgetc(inbuf)) != EOF) {
		if (datar == '~')
			{
			datar = fgetc(inbuf);
			if( datar == ' ') datar = '\n';
			putc('~', outbuf);
			}
		putc(datar, outbuf);
	}
	fclose(inbuf);
	fclose(outbuf);
	strcpy(outfile, "chmod 660 ");
	strcat(outfile, infile);
	(void) system(outfile);
	strcpy(outfile, "chown bbs ");
	strcat(outfile, infile);
	(void) system(outfile);
}
