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