#include "memo.h"

get_memofile()
{
	char *cbuf;
	if ((cbuf = getenv("memofile")) != NULL)
		strcpy(mfile, cbuf);
	else if ((cbuf = getenv("HOME")) != NULL) {
		strcpy(mfile, cbuf);
		strcat(mfile, "/.memos");
	}
	else {
		fprintf(stderr, "memo: can't find memo file -- no 'HOME' or 'memofile' environment variable.");
		exit(1);
	}
}

flip_files(mfile, nmfile)
char *mfile;
char *nmfile;
{
	char cbuf[MAXLINE];
	
	strcpy(cbuf, mfile);
	strcat(cbuf, "~");

	link(mfile, cbuf);	/* mv ~/.memos to ~/.memos~ */
	unlink(mfile);
	link(nmfile, mfile);	/* mv ~/.memo.temp.<pid> to .memos */
	unlink(nmfile);
}

get_temp_file(nmfile)
char *nmfile;

{
	char cbuf[MAXLINE];

	strcpy(nmfile, mfile);
	strcat(nmfile, ".temp.");
	ltoa(getpid(), cbuf);
	strcat(nmfile, cbuf);
}
