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);
128 /* NUL-terminate the field */
131 if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
132 for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
136 (void)printf("%d\t", ++count);
138 if (text == (char *)0 && (sp = strrchr(cp, '/')) != (char *)0)
141 (void)printf("%s\n", cp);
144 } while (*field != '\0' && *field != '-');
154 ** Set the preserve-times flag. This hack eliminates the need for an
155 ** additional argument to annotate().
158 annopreserve(int preserve)
160 preserve_actime_and_modtime = preserve;
165 annosbr(int fd, char *file, char *comp, char *text, int inplace,
166 int datesw, int delete, int append)
170 char buffer[BUFSIZ], tmpfil[BUFSIZ];
173 int c; /* current character */
174 int count; /* header field (annotation) counter */
175 char *field = NULL; /* buffer for header field */
176 int field_size = 0; /* size of field buffer */
177 FILE *fp = NULL; /* file pointer made from locked file descriptor */
178 int length; /* length of field name */
179 int n; /* number of bytes written */
181 mode = fstat(fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
183 strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil));
187 ** We're going to need to copy some of the message file to the
188 ** temporary file while examining the contents. Convert the
189 ** message file descriptor to a file pointer since it's a lot
190 ** easier and more efficient to use stdio for this. Also allocate
191 ** a buffer to hold the header components as they're read in.
192 ** This buffer is grown as needed later.
195 if (delete >= -1 || append != 0) {
196 if ((fp = fdopen(fd, "r")) == (FILE *)0)
197 adios(NULL, "unable to fdopen file.");
199 field = (char *)mh_xmalloc(field_size = 256);
203 ** We're trying to delete a header field (annotation )if the
204 ** delete flag is not -2 or less. A value greater than zero
205 ** means that we're deleting the nth header field that matches
206 ** the field (component) name. A value of zero means that
207 ** we're deleting the first field in which both the field name
208 ** matches the component name and the field body matches the text.
209 ** The text is matched in its entirety if it begins with a slash;
210 ** otherwise the text is matched against whatever portion of the
211 ** field body follows the last slash. This allows matching of
212 ** both absolute and relative path names. This is because this
213 ** functionality was added to support attachments. It might be
214 ** worth having a separate flag to indicate path name matching
215 ** to make it more general. A value of -1 means to delete all
220 /* Get the length of the field name since we use it often. */
221 length = strlen(comp);
224 ** Initialize the field counter. This is only used if
225 ** we're deleting by number.
230 ** Copy lines from the input file to the temporary file
231 ** until we either find the one that we're looking
232 ** for (which we don't copy) or we reach the end of
233 ** the headers. Both a blank line and a line beginning
234 ** with a - terminate the headers so that we can handle
235 ** both drafts and RFC-2822 format messages.
240 ** Get a line from the input file, growing the
241 ** field buffer as needed. We do this so that
242 ** we can fit an entire line in the buffer making
243 ** it easy to do a string comparison on both the
244 ** field name and the field body which might be
248 for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
249 if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
255 if (++n >= field_size - 1) {
256 field = (char *) mh_xrealloc((void *)field, field_size *= 2);
262 /* NUL-terminate the field */
266 ** Check for a match on the field name. We delete
267 ** the line by not copying it to the temporary
270 ** o The delete flag is 0, meaning that we're
271 ** going to delete the first matching
272 ** field, and the text is NULL meaning that
273 ** we don't care about the field body.
275 ** o The delete flag is 0, meaning that we're
276 ** going to delete the first matching
277 ** field, and the text begins with a / meaning
278 ** that we're looking for a full path name,
279 ** and the text matches the field body.
281 ** o The delete flag is 0, meaning that we're
282 ** going to delete the first matching
283 ** field, the text does not begin with a /
284 ** meaning that we're looking for the last
285 ** path name component, and the last path
286 ** name component matches the text.
288 ** o The delete flag is positive meaning that
289 ** we're going to delete the nth field
290 ** with a matching field name, and this is
291 ** the nth matching field name.
293 ** o The delete flag is -1 meaning that we're
294 ** 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
331 if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
332 adios(NULL, "unable to write temporary file.");
334 } while (*field != '\0' && *field != '-');
337 ** Get rid of the field buffer because we're done with it.
344 ** Find the end of the headers before adding the
345 ** annotations if we're appending instead of the default
346 ** prepending. A special check for no headers is needed
352 ** Copy lines from the input file to the temporary
353 ** file until we reach the end of the headers.
356 if ((c = getc(fp)) == '\n')
362 while ((c = getc(fp)) != EOF) {
366 (void)ungetc(c = getc(fp), fp);
368 if (c == '\n' || c == '-')
376 fprintf(tmp, "%s: %s\n", comp, dtimenow(0));
379 while (*cp == ' ' || *cp == '\t')
382 while (*cp && *cp++ != '\n')
385 fprintf(tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
387 if (cp[-1] != '\n' && cp != text)
395 ** We've been messing with the input file position. Move the
396 ** input file descriptor to the current place in the file
397 ** because the stock data copying routine uses the descriptor,
401 if (append || delete >= -1) {
402 if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
403 adios(NULL, "can't seek.");
406 cpydata(fd, fileno(tmp), file, tmpfil);
410 if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK)
411 adios(tmpfil, "unable to open for re-reading");
413 lseek(fd, (off_t) 0, SEEK_SET);
416 ** We're making the file shorter if we're deleting a
417 ** header field so the file has to be truncated or it
418 ** 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.