+/*
+ * Produce a listing of all header fields (annotations) whose field name matches
+ * comp. Number the listing if number is set. Treate the field bodies as path
+ * names and just output the last component unless text is non-NULL. We don't
+ * care what text is set to.
+ */
+
+void
+annolist(char *file, char *comp, char *text, int number)
+{
+ int c; /* current character */
+ int count; /* header field (annotation) counter */
+ char *cp; /* miscellaneous character pointer */
+ char *field; /* buffer for header field */
+ int field_size; /* size of field buffer */
+ FILE *fp; /* file pointer made from locked file descriptor */
+ int length; /* length of field name */
+ int n; /* number of bytes written */
+ char *sp; /* another miscellaneous character pointer */
+
+ if ((fp = fopen(file, "r")) == (FILE *)0)
+ adios(file, "unable to open");
+
+ /*
+ * Allocate a buffer to hold the header components as they're read in.
+ * This buffer might need to be quite large, so we grow it as needed.
+ */
+
+ if ((field = (char *)malloc(field_size = 256)) == (char *)0)
+ adios(NULL, "can't allocate field buffer.");
+
+ /*
+ * Get the length of the field name since we use it often.
+ */
+
+ length = strlen(comp);
+
+ count = 0;
+
+ do {
+ /*
+ * 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) {
+ if ((field = (char *)realloc((void *)field, field_size += 256)) == (char *)0)
+ adios(NULL, "can't grow field buffer.");
+
+ cp = field + n - 1;
+ }
+ }
+
+ /*
+ * NUL-terminate the field..
+ */
+
+ *cp = '\0';
+
+ if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
+ for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
+ ;
+
+ if (number)
+ (void)printf("%d\t", ++count);
+
+ if (text == (char *)0 && (sp = strrchr(cp, '/')) != (char *)0)
+ cp = sp + 1;
+
+ (void)printf("%s\n", cp);
+ }
+
+ } while (*field != '\0' && *field != '-');
+
+ /*
+ * Clean up.
+ */
+
+ free(field);
+
+ (void)fclose(fp);
+
+ return;
+}
+