3 * annosbr.c -- prepend annotation to messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
21 static int annosbr (int, char *, char *, char *, int, int, int, int);
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.
28 static int preserve_actime_and_modtime = 0; /* set to preserve access and modification times on annotated message */
31 annotate (char *file, char *comp, char *text, int inplace, int datesw, int delete, int append)
37 /* open and lock the file to be annotated */
38 if ((fd = lkopen (file, O_RDWR, 0)) == NOTOK) {
44 admonish (file, "unable to lock and open");
50 if (stat(file, &s) == -1) {
51 advise("can't get access and modification times for %s", file);
52 preserve_actime_and_modtime = 0;
55 b.actime = s.st_atime;
56 b.modtime = s.st_mtime;
58 i = annosbr (fd, file, comp, text, inplace, datesw, delete, append);
60 if (preserve_actime_and_modtime && utime(file, &b) == -1)
61 advise("can't set access and modification times for %s", file);
68 * Produce a listing of all header fields (annotations) whose field name matches
69 * comp. Number the listing if number is set. Treate the field bodies as path
70 * names and just output the last component unless text is non-NULL. We don't
71 * care what text is set to.
75 annolist(char *file, char *comp, char *text, int number)
77 int c; /* current character */
78 int count; /* header field (annotation) counter */
79 char *cp; /* miscellaneous character pointer */
80 char *field; /* buffer for header field */
81 int field_size; /* size of field buffer */
82 FILE *fp; /* file pointer made from locked file descriptor */
83 int length; /* length of field name */
84 int n; /* number of bytes written */
85 char *sp; /* another miscellaneous character pointer */
87 if ((fp = fopen(file, "r")) == (FILE *)0)
88 adios(file, "unable to open");
91 * Allocate a buffer to hold the header components as they're read in.
92 * This buffer might need to be quite large, so we grow it as needed.
95 field = (char *)mh_xmalloc(field_size = 256);
98 * Get the length of the field name since we use it often.
101 length = strlen(comp);
107 * Get a line from the input file, growing the field buffer as needed. We do this
108 * so that we can fit an entire line in the buffer making it easy to do a string
109 * comparison on both the field name and the field body which might be a long path
113 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
114 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
120 if (++n >= field_size - 1) {
121 field = (char *) mh_xrealloc((void *)field, field_size += 256);
128 * NUL-terminate the field..
133 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
134 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
138 (void)printf("%d\t", ++count);
140 if (text == (char *)0 && (sp = strrchr(cp, '/')) != (char *)0)
143 (void)printf("%s\n", cp);
146 } while (*field != '\0' && *field != '-');
160 * Set the preserve-times flag. This hack eliminates the need for an additional argument to annotate().
164 annopreserve(int preserve)
166 preserve_actime_and_modtime = preserve;
171 annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw, int delete, int append)
175 char buffer[BUFSIZ], tmpfil[BUFSIZ];
178 int c; /* current character */
179 int count; /* header field (annotation) counter */
180 char *field = NULL; /* buffer for header field */
181 int field_size = 0; /* size of field buffer */
182 FILE *fp = NULL; /* file pointer made from locked file descriptor */
183 int length; /* length of field name */
184 int n; /* number of bytes written */
186 mode = fstat (fd, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
188 strncpy (tmpfil, m_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil));
189 chmod (tmpfil, mode);
192 * We're going to need to copy some of the message file to the temporary
193 * file while examining the contents. Convert the message file descriptor
194 * to a file pointer since it's a lot easier and more efficient to use
195 * stdio for this. Also allocate a buffer to hold the header components
196 * as they're read in. This buffer is grown as needed later.
199 if (delete >= -1 || append != 0) {
200 if ((fp = fdopen(fd, "r")) == (FILE *)0)
201 adios(NULL, "unable to fdopen file.");
203 field = (char *)mh_xmalloc(field_size = 256);
207 * We're trying to delete a header field (annotation )if the delete flag is
208 * not -2 or less. A value greater than zero means that we're deleting the
209 * nth header field that matches the field (component) name. A value of
210 * zero means that we're deleting the first field in which both the field
211 * name matches the component name and the field body matches the text.
212 * The text is matched in its entirety if it begins with a slash; otherwise
213 * the text is matched against whatever portion of the field body follows
214 * the last slash. This allows matching of both absolute and relative path
215 * names. This is because this functionality was added to support attachments.
216 * It might be worth having a separate flag to indicate path name matching to
217 * make it more general. A value of -1 means to delete all matching fields.
222 * Get the length of the field name since we use it often.
225 length = strlen(comp);
228 * Initialize the field counter. This is only used if we're deleting by
235 * Copy lines from the input file to the temporary file until we either find the one
236 * that we're looking for (which we don't copy) or we reach the end of the headers.
237 * Both a blank line and a line beginning with a - terminate the headers so that we
238 * can handle both drafts and RFC-2822 format messages.
243 * Get a line from the input file, growing the field buffer as needed. We do this
244 * so that we can fit an entire line in the buffer making it easy to do a string
245 * comparison on both the field name and the field body which might be a long path
249 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
250 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
256 if (++n >= field_size - 1) {
257 field = (char *) mh_xrealloc((void *)field, field_size *= 2);
264 * NUL-terminate the field..
270 * Check for a match on the field name. We delete the line by not copying it to the
273 * o The delete flag is 0, meaning that we're going to delete the first matching
274 * field, and the text is NULL meaning that we don't care about the field body.
276 * o The delete flag is 0, meaning that we're going to delete the first matching
277 * field, and the text begins with a / meaning that we're looking for a full
278 * path name, and the text matches the field body.
280 * o The delete flag is 0, meaning that we're going to delete the first matching
281 * field, the text does not begin with a / meaning that we're looking for the
282 * last path name component, and the last path name component matches the text.
284 * o The delete flag is positive meaning that we're going to delete the nth field
285 * with a matching field name, and this is the nth matching field name.
287 * o The delete flag is -1 meaning that we're going to delete all fields with a
288 * matching field name.
291 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
293 if (text == (char *)0)
296 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
300 if (strcmp(cp, text) == 0)
304 if ((sp = strrchr(cp, '/')) != (char *)0)
307 if (strcmp(cp, text) == 0)
312 else if (delete == -1)
315 else if (++count == delete)
320 * This line wasn't a match so copy it to the temporary file.
323 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
324 adios(NULL, "unable to write temporary file.");
326 } while (*field != '\0' && *field != '-');
329 * Get rid of the field buffer because we're done with it.
337 * Find the end of the headers before adding the annotations if we're
338 * appending instead of the default prepending. A special check for
339 * no headers is needed if appending.
344 * Copy lines from the input file to the temporary file until we
345 * reach the end of the headers.
348 if ((c = getc(fp)) == '\n')
354 while ((c = getc(fp)) != EOF) {
358 (void)ungetc(c = getc(fp), fp);
360 if (c == '\n' || c == '-')
368 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
371 while (*cp == ' ' || *cp == '\t')
374 while (*cp && *cp++ != '\n')
377 fprintf (tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
379 if (cp[-1] != '\n' && cp != text)
387 * We've been messing with the input file position. Move the input file
388 * descriptor to the current place in the file because the stock data
389 * copying routine uses the descriptor, not the pointer.
392 if (append || delete >= -1) {
393 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
394 adios(NULL, "can't seek.");
397 cpydata (fd, fileno (tmp), file, tmpfil);
401 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
402 adios (tmpfil, "unable to open for re-reading");
404 lseek (fd, (off_t) 0, SEEK_SET);
407 * We're making the file shorter if we're deleting a header field
408 * so the file has to be truncated or it will contain garbage.
411 if (delete >= -1 && ftruncate(fd, 0) == -1)
412 adios(tmpfil, "unable to truncate.");
414 cpydata (tmpfd, fd, tmpfil, file);
418 strncpy (buffer, m_backup (file), sizeof(buffer));
419 if (rename (file, buffer) == NOTOK) {
421 case ENOENT: /* unlinked early - no annotations */
426 admonish (buffer, "unable to rename %s to", file);
431 if (rename (tmpfil, file) == NOTOK) {
432 admonish (file, "unable to rename %s to", tmpfil);
438 * Close the delete file so that we don't run out of file pointers if
439 * we're doing piles of files. Note that this will make the close() in
440 * lkclose() fail, but that failure is ignored so it's not a problem.