]> diplodocus.org Git - nmh/blob - uip/annosbr.c
fdcompare.c: Move interface to own file.
[nmh] / uip / annosbr.c
1 /* annosbr.c -- prepend annotation to messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/error.h"
10 #include "h/tws.h"
11 #include "h/utils.h"
12 #include "sbr/lock_file.h"
13 #include "sbr/m_mktemp.h"
14 #include <fcntl.h>
15 #include <utime.h>
16
17
18 /*
19 * static prototypes
20 */
21 static int annosbr (int, char *, char *, char *, bool, bool, int, bool);
22
23 /*
24 * This "local" global and the annopreserve() function are a hack that allows additional
25 * functionality to be added to anno without piling on yet another annotate() argument.
26 */
27
28 static int preserve_actime_and_modtime = 0; /* set to preserve access and modification times on annotated message */
29
30 int
31 annotate (char *file, char *comp, char *text, bool inplace, bool datesw, int delete, bool append)
32 {
33 int i, fd;
34 struct utimbuf b;
35 struct stat s;
36 int failed_to_lock = 0;
37
38 /* open and lock the file to be annotated */
39 if ((fd = lkopendata (file, O_RDWR, 0, &failed_to_lock)) == NOTOK) {
40 switch (errno) {
41 case ENOENT:
42 break;
43
44 default:
45 if (failed_to_lock) {
46 admonish (file, "unable to lock");
47 } else {
48 admonish (file, "unable to open");
49 }
50 break;
51 }
52 return 1;
53 }
54
55 if (stat(file, &s) == -1) {
56 inform("can't get access and modification times for %s", file);
57 preserve_actime_and_modtime = 0;
58 }
59
60 b.actime = s.st_atime;
61 b.modtime = s.st_mtime;
62
63 i = annosbr (fd, file, comp, text, inplace, datesw, delete, append);
64
65 if (preserve_actime_and_modtime && utime(file, &b) == -1)
66 inform("can't set access and modification times for %s", file);
67
68 lkclosedata (fd, file);
69 return i;
70 }
71
72 /*
73 * Produce a listing of all header fields (annotations) whose field name matches
74 * comp. Number the listing if number is set. Treat the field bodies as path
75 * names and just output the last component unless text is non-NULL. We don't
76 * care what text is set to.
77 */
78
79 void
80 annolist(char *file, char *comp, char *text, int number)
81 {
82 int c; /* current character */
83 int count; /* header field (annotation) counter */
84 char *cp; /* miscellaneous character pointer */
85 char *field; /* buffer for header field */
86 int field_size; /* size of field buffer */
87 FILE *fp; /* file pointer made from locked file descriptor */
88 int length; /* length of field name */
89 int n; /* number of bytes written */
90 char *sp; /* another miscellaneous character pointer */
91
92 if ((fp = fopen(file, "r")) == NULL)
93 adios(file, "unable to open");
94
95 /*
96 * Allocate a buffer to hold the header components as they're read in.
97 * This buffer might need to be quite large, so we grow it as needed.
98 */
99
100 field = mh_xmalloc(field_size = 256);
101
102 /*
103 * Get the length of the field name since we use it often.
104 */
105
106 length = strlen(comp);
107
108 count = 0;
109
110 do {
111 /*
112 * Get a line from the input file, growing the field buffer as needed. We do this
113 * so that we can fit an entire line in the buffer making it easy to do a string
114 * comparison on both the field name and the field body which might be a long path
115 * name.
116 */
117
118 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
119 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
120 (void)ungetc(c, fp);
121 break;
122 }
123
124 if (++n >= field_size - 1) {
125 field = mh_xrealloc((void *)field, field_size += 256);
126
127 cp = field + n - 1;
128 }
129 }
130
131 /*
132 * NUL-terminate the field..
133 */
134
135 *cp = '\0';
136
137 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
138 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
139 ;
140
141 if (number)
142 (void)printf("%d\t", ++count);
143
144 if (text == NULL && (sp = strrchr(cp, '/')))
145 cp = sp + 1;
146
147 puts(cp);
148 }
149
150 } while (*field != '\0' && *field != '-');
151
152 /*
153 * Clean up.
154 */
155
156 free(field);
157
158 (void)fclose(fp);
159 }
160
161 /*
162 * Set the preserve-times flag. This hack eliminates the need for an additional argument to annotate().
163 */
164
165 void
166 annopreserve(int preserve)
167 {
168 preserve_actime_and_modtime = preserve;
169 }
170
171 static int
172 annosbr (int fd, char *file, char *comp, char *text, bool inplace, bool datesw, int delete, bool append)
173 {
174 int mode, tmpfd;
175 char *cp, *sp;
176 char buffer[BUFSIZ], tmpfil[BUFSIZ];
177 struct stat st;
178 FILE *tmp;
179 int c; /* current character */
180 int count; /* header field (annotation) counter */
181 char *field = NULL; /* buffer for header field */
182 int field_size = 0; /* size of field buffer */
183 FILE *fp = NULL; /* file pointer made from locked file descriptor */
184 int length; /* length of field name */
185 int n; /* number of bytes written */
186
187 mode = fstat (fd, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
188
189 if ((cp = m_mktemp2(file, "annotate", NULL, &tmp)) == NULL) {
190 die("unable to create temporary file");
191 }
192 strncpy (tmpfil, cp, sizeof(tmpfil));
193 chmod (tmpfil, mode);
194
195 /*
196 * We're going to need to copy some of the message file to the temporary
197 * file while examining the contents. Convert the message file descriptor
198 * to a file pointer since it's a lot easier and more efficient to use
199 * stdio for this. Also allocate a buffer to hold the header components
200 * as they're read in. This buffer is grown as needed later.
201 */
202
203 if (delete >= -1 || append) {
204 if ((fp = fdopen(fd, "r")) == NULL)
205 die("unable to fdopen file.");
206
207 field = mh_xmalloc(field_size = 256);
208 }
209
210 /*
211 * We're trying to delete a header field (annotation )if the delete flag is
212 * not -2 or less. A value greater than zero means that we're deleting the
213 * nth header field that matches the field (component) name. A value of
214 * zero means that we're deleting the first field in which both the field
215 * name matches the component name and the field body matches the text.
216 * The text is matched in its entirety if it begins with a slash; otherwise
217 * the text is matched against whatever portion of the field body follows
218 * the last slash. This allows matching of both absolute and relative path
219 * names. This is because this functionality was added to support attachments.
220 * It might be worth having a separate flag to indicate path name matching to
221 * make it more general. A value of -1 means to delete all matching fields.
222 */
223
224 if (delete >= -1) {
225 /*
226 * Get the length of the field name since we use it often.
227 */
228
229 length = strlen(comp);
230
231 /*
232 * Initialize the field counter. This is only used if we're deleting by
233 * number.
234 */
235
236 count = 0;
237
238 /*
239 * Copy lines from the input file to the temporary file until we either find the one
240 * that we're looking for (which we don't copy) or we reach the end of the headers.
241 * Both a blank line and a line beginning with a - terminate the headers so that we
242 * can handle both drafts and RFC-2822 format messages.
243 */
244
245 do {
246 /*
247 * Get a line from the input file, growing the field buffer as needed. We do this
248 * so that we can fit an entire line in the buffer making it easy to do a string
249 * comparison on both the field name and the field body which might be a long path
250 * name.
251 */
252
253 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
254 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
255 (void)ungetc(c, fp);
256 c = '\n';
257 break;
258 }
259
260 if (++n >= field_size - 1) {
261 field = mh_xrealloc((void *)field, field_size *= 2);
262
263 cp = field + n - 1;
264 }
265 }
266
267 /*
268 * NUL-terminate the field..
269 */
270
271 *cp = '\0';
272
273 /*
274 * Check for a match on the field name. We delete the line by not copying it to the
275 * temporary file if
276 *
277 * o The delete flag is 0, meaning that we're going to delete the first matching
278 * field, and the text is NULL meaning that we don't care about the field body.
279 *
280 * o The delete flag is 0, meaning that we're going to delete the first matching
281 * field, and the text begins with a / meaning that we're looking for a full
282 * path name, and the text matches the field body.
283 *
284 * o The delete flag is 0, meaning that we're going to delete the first matching
285 * field, the text does not begin with a / meaning that we're looking for the
286 * last path name component, and the last path name component matches the text.
287 *
288 * o The delete flag is positive meaning that we're going to delete the nth field
289 * with a matching field name, and this is the nth matching field name.
290 *
291 * o The delete flag is -1 meaning that we're going to delete all fields with a
292 * matching field name.
293 */
294
295 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
296 if (delete == 0) {
297 if (text == NULL)
298 break;
299
300 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
301 ;
302
303 if (*text == '/') {
304 if (strcmp(cp, text) == 0)
305 break;
306 }
307 else {
308 if ((sp = strrchr(cp, '/')) != NULL)
309 cp = sp + 1;
310
311 if (strcmp(cp, text) == 0)
312 break;
313 }
314 }
315
316 else if (delete == -1)
317 continue;
318
319 else if (++count == delete)
320 break;
321 }
322
323 /*
324 * This line wasn't a match so copy it to the temporary file.
325 */
326
327 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
328 die("unable to write temporary file.");
329
330 } while (*field != '\0' && *field != '-');
331
332 /*
333 * Get rid of the field buffer because we're done with it.
334 */
335
336 free(field);
337 }
338
339 else {
340 /*
341 * Find the end of the headers before adding the annotations if we're
342 * appending instead of the default prepending. A special check for
343 * no headers is needed if appending.
344 */
345
346 if (append) {
347 /*
348 * Copy lines from the input file to the temporary file until we
349 * reach the end of the headers.
350 */
351
352 if ((c = getc(fp)) == '\n')
353 rewind(fp);
354
355 else {
356 (void)putc(c, tmp);
357
358 while ((c = getc(fp)) != EOF) {
359 (void)putc(c, tmp);
360
361 if (c == '\n') {
362 (void)ungetc(c = getc(fp), fp);
363
364 if (c == '\n' || c == '-')
365 break;
366 }
367 }
368 }
369 }
370
371 if (datesw)
372 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
373 if ((cp = text)) {
374 do {
375 while (*cp == ' ' || *cp == '\t')
376 cp++;
377 sp = cp;
378 while (*cp && *cp++ != '\n')
379 continue;
380 if (cp - sp)
381 fprintf (tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
382 } while (*cp);
383 if (cp[-1] != '\n' && cp != text)
384 putc ('\n', tmp);
385 }
386 }
387
388 fflush (tmp);
389
390 /*
391 * We've been messing with the input file position. Move the input file
392 * descriptor to the current place in the file because the stock data
393 * copying routine uses the descriptor, not the pointer.
394 */
395
396 if (fp && lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
397 die("can't seek.");
398
399 cpydata (fd, fileno (tmp), file, tmpfil);
400 fclose (tmp);
401
402 if (inplace) {
403 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
404 adios (tmpfil, "unable to open for re-reading");
405
406 lseek(fd, 0, SEEK_SET);
407
408 /*
409 * We're making the file shorter if we're deleting a header field
410 * so the file has to be truncated or it will contain garbage.
411 */
412
413 if (delete >= -1 && ftruncate(fd, 0) == -1)
414 adios(tmpfil, "unable to truncate.");
415
416 cpydata (tmpfd, fd, tmpfil, file);
417 close (tmpfd);
418 (void) m_unlink (tmpfil);
419 } else {
420 strncpy (buffer, m_backup (file), sizeof(buffer));
421 if (rename (file, buffer) == NOTOK) {
422 switch (errno) {
423 case ENOENT: /* unlinked early - no annotations */
424 (void) m_unlink (tmpfil);
425 break;
426
427 default:
428 admonish (buffer, "unable to rename %s to", file);
429 break;
430 }
431 return 1;
432 }
433 if (rename (tmpfil, file) == NOTOK) {
434 admonish (file, "unable to rename %s to", tmpfil);
435 return 1;
436 }
437 }
438
439 /*
440 * Close the delete file so that we don't run out of file pointers if
441 * we're doing piles of files. Note that this will make the close() in
442 * lkclose() fail, but that failure is ignored so it's not a problem.
443 */
444
445 if (fp)
446 (void)fclose(fp);
447
448 return 0;
449 }