-
Notifications
You must be signed in to change notification settings - Fork 0
/
elvprsv.c
329 lines (295 loc) · 7.65 KB
/
elvprsv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* elvprsv.c */
/* Author:
* Steve Kirkendall
* 1500 SW Park #326
* Portland OR, 97201
*/
/* This file contains the portable sources for the "elvprsv" program.
* "Elvprsv" is run by Elvis when Elvis is about to die. It is also
* run when the computer boots up. It is not intended to be run directly
* by the user, ever.
*
* Basically, this program does the following four things:
* - It extracts the text from the temporary file, and places the text in
* a file in the /usr/preserve directory.
* - It adds a line to the /usr/preserve/Index file, describing the file
* that it just preserved.
* - It removes the temporary file.
* - It sends mail to the owner of the file, saying that the file was
* preserved, and how it can be recovered.
*
* The /usr/preserve/Index file is a log file that contains one line for each
* file that has ever been preserved. Each line of this file describes one
* preserved file. The first word on the line is the name of the file that
* contains the preserved text. The second word is the full pathname of the
* file that was being edited; for anonymous buffers, this is the directory
* name plus "/foo".
*
* If elvprsv's first argument (after the command name) starts with a hyphen,
* then the characters after the hyphen are used as a description of when
* the editor went away. This is optional.
*
* The remaining arguments are all the names of temporary files that are
* to be preserved. For example, on a UNIX system, the /etc/rc file might
* invoke it this way:
*
* elvprsv "-the system went down" /tmp/elv_*.*
*
* This file contains only the portable parts of the preserve program.
* It must #include a system-specific file. The system-specific file is
* expected to define the following functions:
*
* char *ownername(char *filename) - returns name of person who owns file
*
* void mail(char *user, char *name, char *when, char *tmp)
* - tell user that file was preserved
*/
#include <stdio.h>
#include "config.h"
#include "vi.h"
/* We include ctype.c here (instead of including just ctype.h and linking
* with ctype.o) because on some systems ctype.o will have been compiled in
* "large model" and the elvprsv program is to be compiled in "small model"
* You can't mix models. By including ctype.c here, we can avoid linking
* with ctype.o.
*/
#include "ctype.c"
void preserve P_((char *, char *));
void main P_((int, char **));
#if AMIGA
BLK tmpblk;
# include "amiwild.c"
# include "amiprsv.c"
#endif
#if OSK
# undef sprintf
#endif
#if ANY_UNIX || OSK
# include "prsvunix.c"
#endif
#if MSDOS || TOS || OS2
# include "prsvdos.c"
# define WILDCARD_NO_MAIN
# include "wildcard.c"
#endif
BLK buf;
BLK hdr;
BLK name;
int rewrite_now; /* boolean: should we send text directly to orig file? */
/* This function preserves a single file, and announces its success/failure
* via an e-mail message.
*/
void preserve(tname, when)
char *tname; /* name of a temp file to be preserved */
char *when; /* description of when the editor died */
{
int infd; /* fd used for reading from the temp file */
FILE *outfp; /* fp used for writing to the recovery file */
FILE *index; /* fp used for appending to index file */
char outname[100]; /* the name of the recovery file */
char *user; /* name of the owner of the temp file */
#if AMIGA
char *prsvdir;
#endif
int i;
/* open the temp file */
infd = open(tname, O_RDONLY|O_BINARY);
if (infd < 0)
{
/* if we can't open the file, then we should assume that
* the filename contains wildcard characters that weren't
* expanded... and also assume that they weren't expanded
* because there are no files that need to be preserved.
* THEREFORE... we should silently ignore it.
* (Or loudly ignore it if the user was using -R)
*/
if (rewrite_now)
{
perror(tname);
}
return;
}
/* read the header and name from the file */
if (read(infd, hdr.c, BLKSIZE) != BLKSIZE
|| read(infd, name.c, BLKSIZE) != BLKSIZE)
{
/* something wrong with the file - sorry */
fprintf(stderr, "%s: truncated header blocks\n", tname);
close(infd);
return;
}
/* If the filename block contains an empty string, then Elvis was
* only keeping the temp file around because it contained some text
* that was needed for a named cut buffer. The user doesn't care
* about that kind of temp file, so we should silently delete it.
*/
if (name.c[0] == '\0' && name.c[1] == '\177')
{
close(infd);
unlink(tname);
return;
}
/* If there are no text blocks in the file, then we must've never
* really started editing. Discard the file.
*/
if (hdr.n[1] == 0)
{
close(infd);
unlink(tname);
return;
}
if (rewrite_now)
{
/* we don't need to open the index file */
index = (FILE *)0;
/* make sure we can read every block! */
for (i = 1; i < MAXBLKS && hdr.n[i]; i++)
{
lseek(infd, (long)hdr.n[i] * (long)BLKSIZE, 0);
if (read(infd, buf.c, BLKSIZE) != BLKSIZE
|| buf.c[0] == '\0')
{
/* messed up header */
fprintf(stderr, "%s: unrecoverable -- header trashed\n", name.c);
close(infd);
return;
}
}
/* open the user's file for writing */
outfp = fopen(name.c, "w");
if (!outfp)
{
perror(name.c);
close(infd);
return;
}
}
else
{
/* open/create the index file */
index = fopen(PRSVINDEX, "a");
if (!index)
{
perror(PRSVINDEX);
mail(ownername(tname), name.c, when, tname);
exit(2);
}
/* should be at the end of the file already, but MAKE SURE */
fseek(index, 0L, 2);
/* create the recovery file in the PRESVDIR directory */
#if AMIGA
prsvdir = &PRSVDIR[strlen(PRSVDIR) - 1];
if (*prsvdir == '/' || *prsvdir == ':')
{
sprintf(outname, "%sp%ld", PRSVDIR, ftell(index));
}
else
#endif
sprintf(outname, "%s%cp%ld", PRSVDIR, SLASH, ftell(index));
outfp = fopen(outname, "w");
if (!outfp)
{
perror(outname);
close(infd);
fclose(index);
mail(ownername(tname), name.c, when, tname);
return;
}
}
/* write the text of the file out to the recovery file */
for (i = 1; i < MAXBLKS && hdr.n[i]; i++)
{
lseek(infd, (long)hdr.n[i] * (long)BLKSIZE, 0);
if (read(infd, buf.c, BLKSIZE) != BLKSIZE
|| buf.c[0] == '\0')
{
/* messed up header */
fprintf(stderr, "%s: unrecoverable -- header trashed\n", name.c);
fclose(outfp);
close(infd);
if (index)
{
fclose(index);
}
unlink(outname);
return;
}
fputs(buf.c, outfp);
}
/* add a line to the index file */
if (index)
{
fprintf(index, "%s %s\n", outname, name.c);
}
/* close everything */
close(infd);
fclose(outfp);
if (index)
{
fclose(index);
}
/* Are we doing this due to something more frightening than just
* a ":preserve" command?
*/
if (*when)
{
/* send a mail message */
mail(ownername(tname), name.c, when, (char *)0);
/* remove the temp file -- the editor has died already */
unlink(tname);
}
}
void main(argc, argv)
int argc;
char **argv;
{
int i;
char *when = "the editor went away";
#ifdef __EMX__
_wildcard(&argc, &argv);
#else
# if MSDOS || TOS || OS2
/* expand any wildcards in the command line */
_ct_init("");
argv = wildexpand(&argc, argv);
# endif
#endif
/* do we have a "-c", "-R", or "-when elvis died" argument? */
i = 1;
if (argc >= i + 1 && !strcmp(argv[i], "-R"))
{
rewrite_now = 1;
when = "";
i++;
#if ANY_UNIX
setuid(geteuid());
#endif
}
#if OSK
else
{
setuid(0);
}
#endif
if (argc >= i + 1 && argv[i][0] == '-')
{
when = argv[i] + 1;
i++;
}
/* preserve everything we're supposed to */
while (i < argc)
{
char *p = argv[i] - 1;
/* reconvert any converted spaces */
while (*++p)
{
if ((unsigned char)(*p) == SPACEHOLDER)
{
*p = ' ';
}
}
preserve(argv[i], when);
i++;
}
}