xref: /csrg-svn/contrib/ed/edhup.c (revision 57710)
157682Sbostic /*-
257682Sbostic  * Copyright (c) 1992 The Regents of the University of California.
357682Sbostic  * All rights reserved.
457682Sbostic  *
557682Sbostic  * This code is derived from software contributed to Berkeley by
657682Sbostic  * Rodney Ruddock of the University of Guelph.
757682Sbostic  *
857682Sbostic  * %sccs.include.redist.c%
957682Sbostic  */
1057682Sbostic 
1157682Sbostic #ifndef lint
12*57710Sbostic static char sccsid[] = "@(#)edhup.c	5.2 (Berkeley) 01/23/93";
1357682Sbostic #endif /* not lint */
1457682Sbostic 
15*57710Sbostic #include <sys/types.h>
16*57710Sbostic 
17*57710Sbostic #include <db.h>
18*57710Sbostic #include <regex.h>
19*57710Sbostic #include <setjmp.h>
20*57710Sbostic #include <stdio.h>
21*57710Sbostic #include <stdlib.h>
22*57710Sbostic #include <string.h>
23*57710Sbostic #include <unistd.h>
24*57710Sbostic 
2557682Sbostic #include "ed.h"
26*57710Sbostic #include "extern.h"
2757682Sbostic 
2857682Sbostic /*
29*57710Sbostic  * If a SIGHUP is received then user contact is severed. Try, if possible,
30*57710Sbostic  * to save the buffer. But be nice and don't save over remembered filename
31*57710Sbostic  * (you can figure out why, can't you?).  The buffer is saved in a file
32*57710Sbostic  * named "ed.hup" in the directory that ed was started-up in.  If a write
33*57710Sbostic  * cannot be made to that directory (say because it is read-only) then try
34*57710Sbostic  * writting "ed.hup" in the user's $HOME directory. Then exit.
3557682Sbostic  */
36*57710Sbostic __dead void
3757682Sbostic do_hup()
3857682Sbostic {
39*57710Sbostic 	char l_filename[FILENAME_LEN], *l_temp;
40*57710Sbostic 	FILE *l_fp;
4157682Sbostic 
42*57710Sbostic 	if (change_flag == 0)
43*57710Sbostic 		exit(1);		/* No need to save buffer contents. */
44*57710Sbostic 	if ((l_fp = fopen("ed.hup", "w")) == NULL) {
45*57710Sbostic 		/* Try writting ed.hup to the $HOME directory instead. */
46*57710Sbostic 		l_temp = getenv("HOME");
47*57710Sbostic 		if ((l_temp == NULL) || ((strlen(l_temp) + 7) > FILENAME_LEN))
48*57710Sbostic 			exit(1);
49*57710Sbostic 		strcpy(l_filename, l_temp);
50*57710Sbostic 		strcat(l_filename, "/ed.hup");
51*57710Sbostic 		if ((l_fp = fopen(l_filename, "w")) == NULL)
52*57710Sbostic 			exit(1);		/* We tried... */
53*57710Sbostic 	}
54*57710Sbostic 	edwrite(l_fp, top, bottom);
55*57710Sbostic 	fclose(l_fp);
56*57710Sbostic 	(dbhtmp->close) (dbhtmp);
57*57710Sbostic 	unlink(template);
58*57710Sbostic 	exit(1);				/* Hangup */
59*57710Sbostic }
60