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.
23 static int annosbr (int, char *, char *, char *, int, int, int, int);
26 * This "local" global and the annopreserve() function are a hack that allows additional
27 * functionality to be added to anno without piling on yet another annotate() argument.
30 static int preserve_actime_and_modtime = 0; /* set to preserve access and modification times on annotated message */
33 annotate (char *file, char *comp, char *text, int inplace, int datesw, int delete, int append)
39 /* open and lock the file to be annotated */
40 if ((fd = lkopen (file, O_RDWR, 0)) == NOTOK) {
46 admonish (file, "unable to lock and open");
52 if (stat(file, &s) == -1) {
53 advise("can't get access and modification times for %s", file);
54 preserve_actime_and_modtime = 0;
57 b.actime = s.st_atime;
58 b.modtime = s.st_mtime;
60 i = annosbr (fd, file, comp, text, inplace, datesw, delete, append);
62 if (preserve_actime_and_modtime && utime(file, &b) == -1)
63 advise("can't set access and modification times for %s", file);
70 * Produce a listing of all header fields (annotations) whose field name matches
71 * comp. Number the listing if number is set. Treate 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.
77 annolist(char *file, char *comp, char *text, int number)
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 */
89 if ((fp = fopen(file, "r")) == (FILE *)0)
90 adios(file, "unable to open");
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.
97 field = (char *)mh_xmalloc(field_size = 256);
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 field = (char *) mh_xrealloc((void *)field, field_size += 256);
130 * NUL-terminate the field..
135 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
136 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
140 (void)printf("%d\t", ++count);
142 if (text == (char *)0 && (sp = strrchr(cp, '/')) != (char *)0)
145 (void)printf("%s\n", cp);
148 } while (*field != '\0' && *field != '-');
162 * Set the preserve-times flag. This hack eliminates the need for an additional argument to annotate().
166 annopreserve(int preserve)
168 preserve_actime_and_modtime = preserve;
173 annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw, int delete, int append)
177 char buffer[BUFSIZ], tmpfil[BUFSIZ];
180 int c; /* current character */
181 int count; /* header field (annotation) counter */
182 char *field; /* buffer for header field */
183 int field_size; /* size of field buffer */
184 FILE *fp; /* file pointer made from locked file descriptor */
185 int length; /* length of field name */
186 int n; /* number of bytes written */
188 mode = fstat (fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
190 strncpy (tmpfil, m_scratch (file, "annotate"), sizeof(tmpfil));
192 if ((tmp = fopen (tmpfil, "w")) == NULL) {
193 admonish (tmpfil, "unable to create");
196 chmod (tmpfil, mode);
199 * We're going to need to copy some of the message file to the temporary
200 * file while examining the contents. Convert the message file descriptor
201 * to a file pointer since it's a lot easier and more efficient to use
202 * stdio for this. Also allocate a buffer to hold the header components
203 * as they're read in. This buffer is grown as needed later.
206 if (delete >= -1 || append != 0) {
207 if ((fp = fdopen(fd, "r")) == (FILE *)0)
208 adios(NULL, "unable to fdopen file.");
210 field = (char *)mh_xmalloc(field_size = 256);
214 * We're trying to delete a header field (annotation )if the delete flag is
215 * not -2 or less. A value greater than zero means that we're deleting the
216 * nth header field that matches the field (component) name. A value of
217 * zero means that we're deleting the first field in which both the field
218 * name matches the component name and the field body matches the text.
219 * The text is matched in its entirety if it begins with a slash; otherwise
220 * the text is matched against whatever portion of the field body follows
221 * the last slash. This allows matching of both absolute and relative path
222 * names. This is because this functionality was added to support attachments.
223 * It might be worth having a separate flag to indicate path name matching to
224 * make it more general. A value of -1 means to delete all matching fields.
229 * Get the length of the field name since we use it often.
232 length = strlen(comp);
235 * Initialize the field counter. This is only used if we're deleting by
242 * Copy lines from the input file to the temporary file until we either find the one
243 * that we're looking for (which we don't copy) or we reach the end of the headers.
244 * Both a blank line and a line beginning with a - terminate the headers so that we
245 * can handle both drafts and RFC-2822 format messages.
250 * Get a line from the input file, growing the field buffer as needed. We do this
251 * so that we can fit an entire line in the buffer making it easy to do a string
252 * comparison on both the field name and the field body which might be a long path
256 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
257 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
263 if (++n >= field_size - 1) {
264 field = (char *) mh_xrealloc((void *)field, field_size *= 2);
271 * NUL-terminate the field..
277 * Check for a match on the field name. We delete the line by not copying it to the
280 * o The delete flag is 0, meaning that we're going to delete the first matching
281 * field, and the text is NULL meaning that we don't care about the field body.
283 * o The delete flag is 0, meaning that we're going to delete the first matching
284 * field, and the text begins with a / meaning that we're looking for a full
285 * path name, and the text matches the field body.
287 * o The delete flag is 0, meaning that we're going to delete the first matching
288 * field, the text does not begin with a / meaning that we're looking for the
289 * last path name component, and the last path name component matches the text.
291 * o The delete flag is positive meaning that we're going to delete the nth field
292 * with a matching field name, and this is the nth matching field name.
294 * o The delete flag is -1 meaning that we're going to delete all fields with a
295 * matching field name.
298 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
300 if (text == (char *)0)
303 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
307 if (strcmp(cp, text) == 0)
311 if ((sp = strrchr(cp, '/')) != (char *)0)
314 if (strcmp(cp, text) == 0)
319 else if (delete == -1)
322 else if (++count == delete)
327 * This line wasn't a match so copy it to the temporary file.
330 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
331 adios(NULL, "unable to write temporary file.");
333 } while (*field != '\0' && *field != '-');
336 * Get rid of the field buffer because we're done with it.
344 * Find the end of the headers before adding the annotations if we're
345 * appending instead of the default prepending. A special check for
346 * no headers is needed if appending.
351 * Copy lines from the input file to the temporary file until we
352 * reach the end of the headers.
355 if ((c = getc(fp)) == '\n')
361 while ((c = getc(fp)) != EOF) {
365 (void)ungetc(c = getc(fp), fp);
367 if (c == '\n' || c == '-')
375 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
378 while (*cp == ' ' || *cp == '\t')
381 while (*cp && *cp++ != '\n')
384 fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
386 if (cp[-1] != '\n' && cp != text)
394 * We've been messing with the input file position. Move the input file
395 * descriptor to the current place in the file because the stock data
396 * copying routine uses the descriptor, not the pointer.
399 if (append || delete >= -1) {
400 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
401 adios(NULL, "can't seek.");
404 cpydata (fd, fileno (tmp), file, tmpfil);
408 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
409 adios (tmpfil, "unable to open for re-reading");
411 lseek (fd, (off_t) 0, SEEK_SET);
414 * We're making the file shorter if we're deleting a header field
415 * so the file has to be truncated or it will contain garbage.
418 if (delete >= -1 && ftruncate(fd, 0) == -1)
419 adios(tmpfil, "unable to truncate.");
421 cpydata (tmpfd, fd, tmpfil, file);
425 strncpy (buffer, m_backup (file), sizeof(buffer));
426 if (rename (file, buffer) == NOTOK) {
428 case ENOENT: /* unlinked early - no annotations */
433 admonish (buffer, "unable to rename %s to", file);
438 if (rename (tmpfil, file) == NOTOK) {
439 admonish (file, "unable to rename %s to", tmpfil);
445 * Close the delete file so that we don't run out of file pointers if
446 * we're doing piles of files. Note that this will make the close() in
447 * lkclose() fail, but that failure is ignored so it's not a problem.