5 * 14407 SW Teal Blvd. #C
11 /* This file contains the portable sources for the "elvprsv" program.
12 * "Elvprsv" is run by Elvis when Elvis is about to die. It is also
13 * run when the computer boots up. It is not intended to be run directly
16 * Basically, this program does the following four things:
17 * - It extracts the text from the temporary file, and places the text in
18 * a file in the /usr/preserve directory.
19 * - It adds a line to the /usr/preserve/Index file, describing the file
20 * that it just preserved.
21 * - It removes the temporary file.
22 * - It sends mail to the owner of the file, saying that the file was
23 * preserved, and how it can be recovered.
25 * The /usr/preserve/Index file is a log file that contains one line for each
26 * file that has ever been preserved. Each line of this file describes one
27 * preserved file. The first word on the line is the name of the file that
28 * contains the preserved text. The second word is the full pathname of the
29 * file that was being edited; for anonymous buffers, this is the directory
32 * If elvprsv's first argument (after the command name) starts with a hyphen,
33 * then the characters after the hyphen are used as a description of when
34 * the editor went away. This is optional.
36 * The remaining arguments are all the names of temporary files that are
37 * to be preserved. For example, on a UNIX system, the /etc/rc file might
40 * elvprsv "-the system went down" /tmp/elv_*.*
42 * This file contains only the portable parts of the preserve program.
43 * It must #include a system-specific file. The system-specific file is
44 * expected to define the following functions:
46 * char *ownername(char *filename) - returns name of person who owns file
48 * void mail(char *user, char *name, char *when)
49 * - tell user that file was preserved
58 #error AMIGA here DEBUG
68 # include "prsvunix.c"
73 # define WILDCARD_NO_MAIN
74 # include "wildcard.c"
81 int rewrite_now
; /* boolean: should we send text directly to orig file? */
85 /* This function preserves a single file, and announces its success/failure
86 * via an e-mail message.
88 void preserve(tname
, when
)
89 char *tname
; /* name of a temp file to be preserved */
90 char *when
; /* description of when the editor died */
92 int infd
; /* fd used for reading from the temp file */
93 FILE *outfp
; /* fp used for writing to the recovery file */
94 FILE *index
; /* fp used for appending to index file */
95 char outname
[100]; /* the name of the recovery file */
96 char *user
; /* name of the owner of the temp file */
102 /* open the temp file */
103 infd
= open(tname
, O_RDONLY
|O_BINARY
);
106 /* if we can't open the file, then we should assume that
107 * the filename contains wildcard characters that weren't
108 * expanded... and also assume that they weren't expanded
109 * because there are no files that need to be preserved.
110 * THEREFORE... we should silently ignore it.
111 * (Or loudly ignore it if the user was using -R)
120 /* read the header and name from the file */
121 if (read(infd
, hdr
.c
, BLKSIZE
) != BLKSIZE
122 || read(infd
, name
.c
, BLKSIZE
) != BLKSIZE
)
124 /* something wrong with the file - sorry */
125 fprintf(stderr
, "%s: trucated header blocks\n", tname
);
130 /* If the filename block contains an empty string, then Elvis was
131 * only keeping the temp file around because it contained some text
132 * that was needed for a named cut buffer. The user doesn't care
133 * about that kind of temp file, so we should silently delete it.
135 if (name
.c
[0] == '\0' && name
.c
[1] == '\177')
144 /* we don't need to open the index file */
147 /* make sure we can read every block! */
148 for (i
= 1; i
< MAXBLKS
&& hdr
.n
[i
]; i
++)
150 lseek(infd
, (long)hdr
.n
[i
] * (long)BLKSIZE
, 0);
151 if (read(infd
, buf
.c
, BLKSIZE
) != BLKSIZE
)
153 /* messed up header */
154 fprintf(stderr
, "%s: unrecoverable -- header trashed\n", name
.c
);
160 /* open the user's file for writing */
161 outfp
= fopen(name
.c
, "w");
171 /* open/create the index file */
172 index
= fopen(PRSVINDEX
, "a");
179 /* create the recovery file in the PRESVDIR directory */
181 prsvdir
= &PRSVDIR
[strlen(PRSVDIR
) - 1];
182 if (*prsvdir
== '/' || *prsvdir
== ':')
184 sprintf(outname
, "%sp%ld", PRSVDIR
, ftell(index
));
188 sprintf(outname
, "%s%cp%ld", PRSVDIR
, SLASH
, ftell(index
));
189 outfp
= fopen(outname
, "w");
199 /* write the text of the file out to the recovery file */
200 for (i
= 1; i
< MAXBLKS
&& hdr
.n
[i
]; i
++)
202 lseek(infd
, (long)hdr
.n
[i
] * (long)BLKSIZE
, 0);
203 if (read(infd
, buf
.c
, BLKSIZE
) != BLKSIZE
)
205 /* messed up header */
206 fprintf(stderr
, "%s: unrecoverable -- header trashed\n", name
.c
);
219 /* add a line to the index file */
222 fprintf(index
, "%s %s\n", outname
, name
.c
);
225 /* close everything */
233 /* Are we doing this due to something more frightening than just
234 * a ":preserve" command?
238 /* send a mail message */
239 mail(ownername(tname
), name
.c
, when
);
241 /* remove the temp file -- the editor has died already */
251 char *when
= "the editor went away";
254 /* expand any wildcards in the command line */
255 argv
= wildexpand(&argc
, argv
);
258 /* do we have a "when" argument? */
260 if (argc
>= i
+ 1 && !strcmp(argv
[i
], "-R"))
275 if (argc
>= i
+ 1 && argv
[i
][0] == '-')
281 /* preserve everything we're supposed to */
284 preserve(argv
[i
], when
);