1 /* @(#)tmpfiles.c	1.14 09/07/09 Copyright 1984-2009 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)tmpfiles.c	1.14 09/07/09 Copyright 1984-2009 J. Schilling";
6 #endif
7 /*
8  *	Filename manipulation for various tmp files of ved.
9  *
10  *	Copyright (c) 1984-2009 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include "ved.h"
27 #include "buffer.h"
28 
29 EXPORT	Uchar	swapname[TMPNSIZE]; /* file name of current buffer backup file*/
30 EXPORT	Uchar	execname[TMPNSIZE]; /* file name of current execute buffer    */
31 EXPORT	Uchar	protname[TMPNSIZE]; /* file name of current recover protocol  */
32 
33 
34 EXPORT	FILE	*takefile;	/* FILE * of current take buffer	    */
35 EXPORT	epos_t	takesize;	/* file size of current take buffer	    */
36 EXPORT	Uchar	takename[TMPNSIZE]; /* file name of current take buffer	    */
37 
38 EXPORT	FILE	*delfile;	/* FILE * of delete buffer		    */
39 EXPORT	epos_t	delsize;	/* file size of delete buffer		    */
40 EXPORT	Uchar	delname[TMPNSIZE]; /* file name of delete buffer	    */
41 
42 EXPORT	FILE	*rubfile;	/* FILE * of rubout buffer		    */
43 EXPORT	epos_t	rubsize;	/* file size of rubout buffer		    */
44 EXPORT	Uchar	rubname[TMPNSIZE]; /* file name of rubout buffer	    */
45 
46 char	*tmpdirs[] = {
47 	"/var/tmp",
48 	"/usr/tmp",
49 	"/tmp",
50 	NULL,
51 };
52 
53 LOCAL	Uchar	*tmpdir;		/* Standard tmp file dir prefix	    */
54 LOCAL	Uchar	*ftmpdir = UC "/tmp";	/* Fast tmp directory if available  */
55 LOCAL	Uchar	*tmppre = UC "ved";	/* Filename prefix for all tmp files */
56 
57 LOCAL	void	findtmpdir	__PR((void));
58 EXPORT	FILE	*tmpfopen	__PR((ewin_t *wp, Uchar *name, char *mode));
59 EXPORT	void	tmpsetup	__PR((void));
60 EXPORT	void	takepath	__PR((Uchar *pathname, int pathsize, Uchar *name));
61 EXPORT	void	tmpopen		__PR((ewin_t *wp));
62 LOCAL	void	tmpdelete	__PR((void));
63 EXPORT	void	tmpcleanup	__PR((ewin_t *wp, BOOL force));
64 
65 /*
66  * Find tmp directory prefix for this edit session.
67  */
68 LOCAL void
findtmpdir()69 findtmpdir()
70 {
71 	char	**tp;
72 	char	*ft;
73 	char	*td;
74 
75 	ft = getenv("VED_FTMPDIR");
76 	td = getenv("VED_TMPDIR");
77 	if (ft == NULL)
78 		ft = td;
79 
80 	if (ft != NULL && readable(UC ft) && writable(UC ft))
81 		ftmpdir = UC ft;
82 	if (td != NULL && readable(UC td) && writable(UC td))
83 		tmpdir = UC td;
84 
85 	for (tp = &tmpdirs[0]; *tp != NULL; tp++) {
86 		if (readable(UC *tp) && writable(UC *tp))
87 			break;
88 	}
89 	if (tmpdir == NULL) {
90 		if (*tp != 0) {
91 			tmpdir = UC *tp;
92 		} else {
93 			tmpdir = UC ".";
94 		}
95 	}
96 /*	errmsgno(-1, "tmpdir: '%s'\n", tmpdir);*/
97 /*	sleep(1);*/
98 	if (!readable(ftmpdir) || !writable(ftmpdir))
99 		ftmpdir = UC ".";
100 }
101 
102 /*
103  * Open a tmp file and make set private access permissions.
104  */
105 EXPORT FILE *
tmpfopen(wp,name,mode)106 tmpfopen(wp, name, mode)
107 	ewin_t	*wp;
108 	Uchar	*name;
109 	char	*mode;
110 {
111 	FILE	*f;
112 
113 	f = opencomerr(wp, name, mode);
114 	if (f != (FILE *)NULL)
115 		stmpfmodes(name);
116 	return (f);
117 }
118 
119 /*
120  * Set up the names of several files that are known
121  * at start time of an editing session.
122  */
123 EXPORT void
tmpsetup()124 tmpsetup()
125 {
126 	if (tmpdir == NULL)
127 		findtmpdir();
128 
129 	snprintf(C swapname, sizeof (swapname), "%s/%sF.%d", ftmpdir, tmppre, pid);
130 	snprintf(C protname, sizeof (protname), "%s/%sP.%d", tmpdir, tmppre, pid);
131 
132 	snprintf(C execname, sizeof (execname), "%s/%sX.%d", tmpdir, tmppre, pid);
133 
134 	snprintf(C delname, sizeof (delname), "%s/%sD.%d", tmpdir, tmppre, pid);
135 	snprintf(C rubname, sizeof (rubname), "%s/%sB.%d", tmpdir, tmppre, pid);
136 	snprintf(C takename, sizeof (takename), "%s/%sT.%d", tmpdir, tmppre, pid);
137 }
138 
139 /*
140  * Set up the pathname of a take buffer.
141  */
142 EXPORT void
takepath(pathname,pathsize,name)143 takepath(pathname, pathsize, name)
144 	Uchar	*pathname;
145 	int	pathsize;
146 	Uchar	*name;
147 {
148 	snprintf(C pathname, pathsize, "%s/%sT%s.%d", tmpdir, tmppre, name, pid);
149 }
150 
151 /*
152  * Open several tmp files at startup.
153  */
154 EXPORT void
tmpopen(wp)155 tmpopen(wp)
156 	ewin_t	*wp;
157 {
158 	delfile = tmpfopen(wp, delname, "cwrtb");
159 	rubfile = tmpfopen(wp, rubname, "cwrtb");
160 	takefile = tmpfopen(wp, takename, "cwrtb");
161 }
162 
163 /*
164  * Close and remove several tmp files.
165  */
166 LOCAL void
tmpdelete()167 tmpdelete()
168 {
169 	if (execname[0] != '\0')
170 		unlink(C execname);
171 
172 	if (delfile != (FILE *)0)
173 		fclose(delfile);
174 
175 	if (delname[0] != '\0')
176 		unlink(C delname);
177 
178 
179 	if (rubfile != (FILE *)0)
180 		fclose(rubfile);
181 
182 	if (rubname[0] != '\0')
183 		unlink(C rubname);
184 
185 
186 	if (takefile != (FILE *)0)
187 		fclose(takefile);
188 
189 	if (takename[0] != '\0')
190 		unlink(C takename);
191 }
192 
193 /*
194  * Close and remove all tmporary files of VED.
195  */
196 EXPORT void
tmpcleanup(wp,force)197 tmpcleanup(wp, force)
198 	ewin_t	*wp;
199 	BOOL	force;
200 {
201 	if (force || wp->modflg == 0) {
202 		termbuffers(wp);
203 		deleteprot();
204 		tmpdelete();
205 		deletetake();
206 	}
207 }
208