xref: /csrg-svn/contrib/ed/edhup.c (revision 60663)
157682Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  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*60663Sbostic static char sccsid[] = "@(#)edhup.c	8.1 (Berkeley) 05/31/93";
1357682Sbostic #endif /* not lint */
1457682Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1758315Sbostic #include <limits.h>
1857710Sbostic #include <regex.h>
1957710Sbostic #include <setjmp.h>
2057710Sbostic #include <stdio.h>
2157710Sbostic #include <stdlib.h>
2257710Sbostic #include <string.h>
2357710Sbostic #include <unistd.h>
2457710Sbostic 
2558315Sbostic #ifdef DBI
2658315Sbostic #include <db.h>
2758315Sbostic #endif
2858315Sbostic 
2957682Sbostic #include "ed.h"
3057710Sbostic #include "extern.h"
3157682Sbostic 
3257682Sbostic /*
3357710Sbostic  * If a SIGHUP is received then user contact is severed. Try, if possible,
3457710Sbostic  * to save the buffer. But be nice and don't save over remembered filename
3557710Sbostic  * (you can figure out why, can't you?).  The buffer is saved in a file
3657710Sbostic  * named "ed.hup" in the directory that ed was started-up in.  If a write
3757710Sbostic  * cannot be made to that directory (say because it is read-only) then try
3857710Sbostic  * writting "ed.hup" in the user's $HOME directory. Then exit.
3957682Sbostic  */
4057710Sbostic __dead void
do_hup()4157682Sbostic do_hup()
4257682Sbostic {
4357710Sbostic 	char l_filename[FILENAME_LEN], *l_temp;
4457710Sbostic 	FILE *l_fp;
4557682Sbostic 
4658315Sbostic 	sigspecial++;
4757710Sbostic 	if (change_flag == 0)
4859475Sbostic 		exit(exit_code+2);		/* No need to save buffer contents. */
4957710Sbostic 	if ((l_fp = fopen("ed.hup", "w")) == NULL) {
5057710Sbostic 		/* Try writting ed.hup to the $HOME directory instead. */
5157710Sbostic 		l_temp = getenv("HOME");
5257710Sbostic 		if ((l_temp == NULL) || ((strlen(l_temp) + 7) > FILENAME_LEN))
5359475Sbostic 			exit(exit_code+2);
5457710Sbostic 		strcpy(l_filename, l_temp);
5557710Sbostic 		strcat(l_filename, "/ed.hup");
5657710Sbostic 		if ((l_fp = fopen(l_filename, "w")) == NULL)
5759475Sbostic 			exit(exit_code+2);		/* We tried... */
5857710Sbostic 	}
5957710Sbostic 	edwrite(l_fp, top, bottom);
6057710Sbostic 	fclose(l_fp);
6158315Sbostic #ifdef STDIO
6258315Sbostic 	fclose(fhtmp);
6358315Sbostic 	unlink(template);
6458315Sbostic #endif
6558315Sbostic #ifdef DBI
6657710Sbostic 	(dbhtmp->close) (dbhtmp);
6757710Sbostic 	unlink(template);
6858315Sbostic #endif
6959475Sbostic 	exit(exit_code+2);				/* Hangup */
7057710Sbostic }
71