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