+ /*
+ * Get a line from the input file, growing the field buffer as needed. We do this
+ * so that we can fit an entire line in the buffer making it easy to do a string
+ * comparison on both the field name and the field body which might be a long path
+ * name.
+ */
+
+ for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
+ if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
+ (void)ungetc(c, fp);
+ c = '\n';
+ break;
+ }
+
+ if (++n >= field_size - 1) {
+ field = (char *) mh_xrealloc((void *)field, field_size *= 2);
+
+ cp = field + n - 1;
+ }
+ }
+
+ /*
+ * NUL-terminate the field..
+ */
+
+ *cp = '\0';
+
+ /*
+ * Check for a match on the field name. We delete the line by not copying it to the
+ * temporary file if
+ *
+ * o The delete flag is 0, meaning that we're going to delete the first matching
+ * field, and the text is NULL meaning that we don't care about the field body.
+ *
+ * o The delete flag is 0, meaning that we're going to delete the first matching
+ * field, and the text begins with a / meaning that we're looking for a full
+ * path name, and the text matches the field body.
+ *
+ * o The delete flag is 0, meaning that we're going to delete the first matching
+ * field, the text does not begin with a / meaning that we're looking for the
+ * last path name component, and the last path name component matches the text.
+ *
+ * o The delete flag is positive meaning that we're going to delete the nth field
+ * with a matching field name, and this is the nth matching field name.
+ *
+ * o The delete flag is -1 meaning that we're going to delete all fields with a
+ * matching field name.
+ */
+
+ if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
+ if (delete == 0) {
+ if (text == (char *)0)
+ break;
+
+ for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
+ ;
+
+ if (*text == '/') {
+ if (strcmp(cp, text) == 0)
+ break;
+ }
+ else {
+ if ((sp = strrchr(cp, '/')) != (char *)0)
+ cp = sp + 1;
+
+ if (strcmp(cp, text) == 0)
+ break;
+ }
+ }
+
+ else if (delete == -1)
+ continue;
+
+ else if (++count == delete)
+ break;
+ }
+
+ /*
+ * This line wasn't a match so copy it to the temporary file.
+ */
+
+ if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
+ adios(NULL, "unable to write temporary file.");
+
+ } while (*field != '\0' && *field != '-');
+
+ /*
+ * Get rid of the field buffer because we're done with it.
+ */
+
+ free((void *)field);
+ }
+
+ else {
+ /*
+ * Find the end of the headers before adding the annotations if we're
+ * appending instead of the default prepending. A special check for
+ * no headers is needed if appending.
+ */
+
+ if (append) {
+ /*
+ * Copy lines from the input file to the temporary file until we
+ * reach the end of the headers.
+ */
+
+ if ((c = getc(fp)) == '\n')
+ rewind(fp);
+
+ else {
+ (void)putc(c, tmp);
+
+ while ((c = getc(fp)) != EOF) {
+ (void)putc(c, tmp);
+
+ if (c == '\n') {
+ (void)ungetc(c = getc(fp), fp);
+
+ if (c == '\n' || c == '-')
+ break;
+ }
+ }
+ }
+ }
+
+ if (datesw)
+ fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
+ if ((cp = text)) {
+ do {
+ while (*cp == ' ' || *cp == '\t')
+ cp++;
+ sp = cp;
+ while (*cp && *cp++ != '\n')
+ continue;
+ if (cp - sp)
+ fprintf (tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
+ } while (*cp);
+ if (cp[-1] != '\n' && cp != text)
+ putc ('\n', tmp);
+ }