3 * annosbr.c -- prepend annotation to messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
22 static int annosbr (int, char *, char *, char *, int, int, int, int);
25 * This "local" global and the annopreserve() function are a hack that allows additional
26 * functionality to be added to anno without piling on yet another annotate() argument.
29 static int preserve_actime_and_modtime = 0; /* set to preserve access and modification times on annotated message */
32 annotate (char *file, char *comp, char *text, int inplace, int datesw, int delete, int append)
38 /* open and lock the file to be annotated */
39 if ((fd = lkopen (file, O_RDWR, 0)) == NOTOK) {
45 admonish (file, "unable to lock and open");
51 if (stat(file, &s) == -1) {
52 advise("can't get access and modification times for %s", file);
53 preserve_actime_and_modtime = 0;
56 b.actime = s.st_atime;
57 b.modtime = s.st_mtime;
59 i = annosbr (fd, file, comp, text, inplace, datesw, delete, append);
61 if (preserve_actime_and_modtime && utime(file, &b) == -1)
62 advise("can't set access and modification times for %s", file);
69 * Produce a listing of all header fields (annotations) whose field name matches
70 * comp. Number the listing if number is set. Treate the field bodies as path
71 * names and just output the last component unless text is non-NULL. We don't
72 * care what text is set to.
76 annolist(char *file, char *comp, char *text, int number)
78 int c; /* current character */
79 int count; /* header field (annotation) counter */
80 char *cp; /* miscellaneous character pointer */
81 char *field; /* buffer for header field */
82 int field_size; /* size of field buffer */
83 FILE *fp; /* file pointer made from locked file descriptor */
84 int length; /* length of field name */
85 int n; /* number of bytes written */
86 char *sp; /* another miscellaneous character pointer */
88 if ((fp = fopen(file, "r")) == (FILE *)0)
89 adios(file, "unable to open");
92 * Allocate a buffer to hold the header components as they're read in.
93 * This buffer might need to be quite large, so we grow it as needed.
96 if ((field = (char *)malloc(field_size = 256)) == (char *)0)
97 adios(NULL, "can't allocate field buffer.");
100 * Get the length of the field name since we use it often.
103 length = strlen(comp);
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
115 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
116 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
122 if (++n >= field_size - 1) {
123 if ((field = (char *)realloc((void *)field, field_size += 256)) == (char *)0)
124 adios(NULL, "can't grow field buffer.");
131 * NUL-terminate the field..
136 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
137 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
141 (void)printf("%d\t", ++count);
143 if (text == (char *)0 && (sp = strrchr(cp, '/')) != (char *)0)
146 (void)printf("%s\n", cp);
149 } while (*field != '\0' && *field != '-');
163 * Set the preserve-times flag. This hack eliminates the need for an additional argument to annotate().
167 annopreserve(int preserve)
169 preserve_actime_and_modtime = preserve;
174 annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw, int delete, int append)
178 char buffer[BUFSIZ], tmpfil[BUFSIZ];
181 int c; /* current character */
182 int count; /* header field (annotation) counter */
183 char *field; /* buffer for header field */
184 int field_size; /* size of field buffer */
185 FILE *fp; /* file pointer made from locked file descriptor */
186 int length; /* length of field name */
187 int n; /* number of bytes written */
189 mode = fstat (fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
191 strncpy (tmpfil, m_scratch (file, "annotate"), sizeof(tmpfil));
193 if ((tmp = fopen (tmpfil, "w")) == NULL) {
194 admonish (tmpfil, "unable to create");
197 chmod (tmpfil, mode);
200 * We're going to need to copy some of the message file to the temporary
201 * file while examining the contents. Convert the message file descriptor
202 * to a file pointer since it's a lot easier and more efficient to use
203 * stdio for this. Also allocate a buffer to hold the header components
204 * as they're read in. This buffer is grown as needed later.
207 if (delete >= -1 || append != 0) {
208 if ((fp = fdopen(fd, "r")) == (FILE *)0)
209 adios(NULL, "unable to fdopen file.");
211 if ((field = (char *)malloc(field_size = 256)) == (char *)0)
212 adios(NULL, "can't allocate field buffer.");
216 * We're trying to delete a header field (annotation )if the delete flag is
217 * not -2 or less. A value greater than zero means that we're deleting the
218 * nth header field that matches the field (component) name. A value of
219 * zero means that we're deleting the first field in which both the field
220 * name matches the component name and the field body matches the text.
221 * The text is matched in its entirety if it begins with a slash; otherwise
222 * the text is matched against whatever portion of the field body follows
223 * the last slash. This allows matching of both absolute and relative path
224 * names. This is because this functionality was added to support attachments.
225 * It might be worth having a separate flag to indicate path name matching to
226 * make it more general. A value of -1 means to delete all matching fields.
231 * Get the length of the field name since we use it often.
234 length = strlen(comp);
237 * Initialize the field counter. This is only used if we're deleting by
244 * Copy lines from the input file to the temporary file until we either find the one
245 * that we're looking for (which we don't copy) or we reach the end of the headers.
246 * Both a blank line and a line beginning with a - terminate the headers so that we
247 * can handle both drafts and RFC-2822 format messages.
252 * Get a line from the input file, growing the field buffer as needed. We do this
253 * so that we can fit an entire line in the buffer making it easy to do a string
254 * comparison on both the field name and the field body which might be a long path
258 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
259 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
265 if (++n >= field_size - 1) {
266 if ((field = (char *)realloc((void *)field, field_size *= 2)) == (char *)0)
267 adios(NULL, "can't grow field buffer.");
274 * NUL-terminate the field..
280 * Check for a match on the field name. We delete the line by not copying it to the
283 * o The delete flag is 0, meaning that we're going to delete the first matching
284 * field, and the text is NULL meaning that we don't care about the field body.
286 * o The delete flag is 0, meaning that we're going to delete the first matching
287 * field, and the text begins with a / meaning that we're looking for a full
288 * path name, and the text matches the field body.
290 * o The delete flag is 0, meaning that we're going to delete the first matching
291 * field, the text does not begin with a / meaning that we're looking for the
292 * last path name component, and the last path name component matches the text.
294 * o The delete flag is positive meaning that we're going to delete the nth field
295 * with a matching field name, and this is the nth matching field name.
297 * o The delete flag is -1 meaning that we're going to delete all fields with a
298 * matching field name.
301 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
303 if (text == (char *)0)
306 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
310 if (strcmp(cp, text) == 0)
314 if ((sp = strrchr(cp, '/')) != (char *)0)
317 if (strcmp(cp, text) == 0)
322 else if (delete == -1)
325 else if (++count == delete)
330 * This line wasn't a match so copy it to the temporary file.
333 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
334 adios(NULL, "unable to write temporary file.");
336 } while (*field != '\0' && *field != '-');
339 * Get rid of the field buffer because we're done with it.
347 * Find the end of the headers before adding the annotations if we're
348 * appending instead of the default prepending. A special check for
349 * no headers is needed if appending.
354 * Copy lines from the input file to the temporary file until we
355 * reach the end of the headers.
358 if ((c = getc(fp)) == '\n')
364 while ((c = getc(fp)) != EOF) {
368 (void)ungetc(c = getc(fp), fp);
370 if (c == '\n' || c == '-')
378 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
381 while (*cp == ' ' || *cp == '\t')
384 while (*cp && *cp++ != '\n')
387 fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
389 if (cp[-1] != '\n' && cp != text)
397 * We've been messing with the input file position. Move the input file
398 * descriptor to the current place in the file because the stock data
399 * copying routine uses the descriptor, not the pointer.
402 if (append || delete >= -1) {
403 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
404 adios(NULL, "can't seek.");
407 cpydata (fd, fileno (tmp), file, tmpfil);
411 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
412 adios (tmpfil, "unable to open for re-reading");
414 lseek (fd, (off_t) 0, SEEK_SET);
417 * We're making the file shorter if we're deleting a header field
418 * so the file has to be truncated or it will contain garbage.
421 if (delete >= -1 && ftruncate(fd, 0) == -1)
422 adios(tmpfil, "unable to truncate.");
424 cpydata (tmpfd, fd, tmpfil, file);
428 strncpy (buffer, m_backup (file), sizeof(buffer));
429 if (rename (file, buffer) == NOTOK) {
431 case ENOENT: /* unlinked early - no annotations */
436 admonish (buffer, "unable to rename %s to", file);
441 if (rename (tmpfil, file) == NOTOK) {
442 admonish (file, "unable to rename %s to", tmpfil);
448 * Close the delete file so that we don't run out of file pointers if
449 * we're doing piles of files. Note that this will make the close() in
450 * lkclose() fail, but that failure is ignored so it's not a problem.