#include <stdio.h>
#ifndef DEMO
#include "config.h"
#include "global.h"
#include "commands.h"
#include "proc.h"
#endif

FILE *
fopennew (fname, mode)
char *fname, *mode;
{
char newname[256];

	sprintf (newname, "%s.new", fname);
	return (fopen (newname, mode));
}


int
merge (fname)
char *fname;
{
FILE *fp, *fpnew;
char buf[128], name[128];

	sprintf (name, "%s.new", fname);
	if ((fpnew = fopen(name, "rt")) == (FILE *)NULL)
		return 0;
	if ((fp = fopen(fname, "r+t")) == (FILE *)NULL)		{
		fclose (fpnew);
		rename(name, fname);
		return 1;
	}
	pwait (NULL);
	fseek (fp, 0, 2);
	while (fgets (buf, 128, fpnew))		{
		pwait (NULL);
		fputs (buf, fp);
	}
	fclose (fp);
	fclose (fpnew);
	remove (name);
	return 1;
}


#ifdef DEMO

int
pwait (i)
int i;
{
}


void
main ()
{
	merge ("/nos/src/test1");
}

#endif
