2 * annosbr.c -- prepend annotation to messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
20 static int annosbr (int, char *, char *, char *, int, int, int, int);
23 * This "local" global and the annopreserve() function are a hack that
24 * allows additional functionality to be added to anno without piling
25 * on yet another annotate() argument.
28 /* set to preserve access and modification times on annotated message */
29 static int preserve_actime_and_modtime = 0;
32 annotate (char *file, char *comp, char *text, int inplace, int datesw,
33 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
71 * name matches comp. Number the listing if number is set. Treate the
72 * field bodies as path names and just output the last component unless
73 * text is non-NULL. We don't 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.
96 field = (char *)mh_xmalloc(field_size = 256);
99 * 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
108 * as needed. We do this so that we can fit an entire line
109 * in the buffer making it easy to do a string comparison
110 * on both the field name and the field body which might be
114 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
115 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
121 if (++n >= field_size - 1) {
122 field = (char *) mh_xrealloc((void *)field, field_size += 256);
129 * NUL-terminate the field..
134 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
135 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
139 (void)printf("%d\t", ++count);
141 if (text == (char *)0 && (sp = strrchr(cp, '/')) != (char *)0)
144 (void)printf("%s\n", cp);
147 } while (*field != '\0' && *field != '-');
161 * Set the preserve-times flag. This hack eliminates the need for an
162 * 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,
174 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 = NULL; /* buffer for header field */
184 int field_size = 0; /* size of field buffer */
185 FILE *fp = NULL; /* 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_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil));
192 chmod (tmpfil, mode);
195 * We're going to need to copy some of the message file to the
196 * temporary file while examining the contents. Convert the
197 * message file descriptor to a file pointer since it's a lot
198 * easier and more efficient to use stdio for this. Also allocate
199 * a buffer to hold the header components as they're read in.
200 * This buffer is grown as needed later.
203 if (delete >= -1 || append != 0) {
204 if ((fp = fdopen(fd, "r")) == (FILE *)0)
205 adios(NULL, "unable to fdopen file.");
207 field = (char *)mh_xmalloc(field_size = 256);
211 * We're trying to delete a header field (annotation )if the
212 * delete flag is not -2 or less. A value greater than zero
213 * means that we're deleting the nth header field that matches
214 * the field (component) name. A value of zero means that
215 * we're deleting the first field in which both the field name
216 * matches the component name and the field body matches the text.
217 * The text is matched in its entirety if it begins with a slash;
218 * otherwise the text is matched against whatever portion of the
219 * field body follows the last slash. This allows matching of
220 * both absolute and relative path names. This is because this
221 * functionality was added to support attachments. It might be
222 * worth having a separate flag to indicate path name matching
223 * to make it more general. A value of -1 means to delete all
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
236 * we're deleting by number.
242 * Copy lines from the input file to the temporary file
243 * until we either find the one that we're looking
244 * for (which we don't copy) or we reach the end of
245 * the headers. Both a blank line and a line beginning
246 * with a - terminate the headers so that we can handle
247 * both drafts and RFC-2822 format messages.
252 * Get a line from the input file, growing the
253 * field buffer as needed. We do this so that
254 * we can fit an entire line in the buffer making
255 * it easy to do a string comparison on both the
256 * field name and the field body which might be
260 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
261 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
267 if (++n >= field_size - 1) {
268 field = (char *) mh_xrealloc((void *)field, field_size *= 2);
275 * NUL-terminate the field..
281 * Check for a match on the field name. We delete
282 * the line by not copying it to the temporary
285 * o The delete flag is 0, meaning that we're
286 * going to delete the first matching
287 * field, and the text is NULL meaning that
288 * we don't care about the field body.
290 * o The delete flag is 0, meaning that we're
291 * going to delete the first matching
292 * field, and the text begins with a / meaning
293 * that we're looking for a full path name,
294 * and the text matches the field body.
296 * o The delete flag is 0, meaning that we're
297 * going to delete the first matching
298 * field, the text does not begin with a /
299 * meaning that we're looking for the last
300 * path name component, and the last path
301 * name component matches the text.
303 * o The delete flag is positive meaning that
304 * we're going to delete the nth field
305 * with a matching field name, and this is
306 * the nth matching field name.
308 * o The delete flag is -1 meaning that we're
309 * going to delete all fields with a
310 * matching field name.
313 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
315 if (text == (char *)0)
318 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
322 if (strcmp(cp, text) == 0)
326 if ((sp = strrchr(cp, '/')) != (char *)0)
329 if (strcmp(cp, text) == 0)
334 else if (delete == -1)
337 else if (++count == delete)
342 * This line wasn't a match so copy it to the
346 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
347 adios(NULL, "unable to write temporary file.");
349 } while (*field != '\0' && *field != '-');
352 * Get rid of the field buffer because we're done with it.
360 * Find the end of the headers before adding the
361 * annotations if we're appending instead of the default
362 * prepending. A special check for no headers is needed
368 * Copy lines from the input file to the temporary
369 * file until we reach the end of the headers.
372 if ((c = getc(fp)) == '\n')
378 while ((c = getc(fp)) != EOF) {
382 (void)ungetc(c = getc(fp), fp);
384 if (c == '\n' || c == '-')
392 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
395 while (*cp == ' ' || *cp == '\t')
398 while (*cp && *cp++ != '\n')
401 fprintf (tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
403 if (cp[-1] != '\n' && cp != text)
411 * We've been messing with the input file position. Move the
412 * input file descriptor to the current place in the file
413 * because the stock data copying routine uses the descriptor,
417 if (append || delete >= -1) {
418 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
419 adios(NULL, "can't seek.");
422 cpydata (fd, fileno (tmp), file, tmpfil);
426 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
427 adios (tmpfil, "unable to open for re-reading");
429 lseek (fd, (off_t) 0, SEEK_SET);
432 * We're making the file shorter if we're deleting a
433 * header field so the file has to be truncated or it
434 * will contain garbage.
437 if (delete >= -1 && ftruncate(fd, 0) == -1)
438 adios(tmpfil, "unable to truncate.");
440 cpydata (tmpfd, fd, tmpfil, file);
444 strncpy (buffer, m_backup (file), sizeof(buffer));
445 if (rename (file, buffer) == NOTOK) {
447 case ENOENT: /* unlinked early - no annotations */
452 admonish (buffer, "unable to rename %s to", file);
457 if (rename (tmpfil, file) == NOTOK) {
458 admonish (file, "unable to rename %s to", tmpfil);
464 * Close the delete file so that we don't run out of file pointers if
465 * we're doing piles of files. Note that this will make the close() in
466 * lkclose() fail, but that failure is ignored so it's not a problem.