switch (errno) {
case ENOENT:
break;
-
default:
admonish(file, "unable to lock and open");
break;
i = annosbr(fd, file, comp, text, datesw, delete, append);
- if (preserve && utime(file, &b) == -1)
+ if (preserve && utime(file, &b) == -1) {
advise("can't set access and modification times for %s", file);
-
+ }
lkclose(fd, file);
return i;
}
/*
** Produce a listing of all header fields (annotations) whose field
-** name matches comp. Number the listing if number is set. Treate the
+** name matches comp. Number the listing if number is set. Treat 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 c;
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 */
+ char *cp;
+ char *field;
+ int field_size;
+ FILE *fp;
+ int length;
int n; /* number of bytes written */
- char *sp; /* another miscellaneous character pointer */
+ char *sp;
- if ((fp = fopen(file, "r")) == (FILE *)0)
+ if ((fp = fopen(file, "r")) == NULL) {
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.
- */
+ /* We'll grow this buffer as needed. */
field = (char *)mh_xmalloc(field_size = 256);
- /*
- ** Get the length of the field name since we use it often.
- */
- length = strlen(comp);
-
+ length = strlen(comp); /* Convenience copy. */
count = 0;
do {
** 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') {
ungetc(c, fp);
c = '\n';
break;
}
-
if (++n >= field_size - 1) {
- field = (char *) mh_xrealloc((void *)field, field_size += 256);
-
+ field = (char *)mh_xrealloc(field,
+ field_size += 256);
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)
+ if (strncasecmp(field, comp, length)==0 &&
+ field[length] == ':') {
+ for (cp = field + length + 1;
+ *cp == ' ' || *cp == '\t'; cp++) {
+ continue;
+ }
+ if (number) {
printf("%d\t", ++count);
-
- if (text == NULL && (sp = strrchr(cp, '/')) != NULL)
+ }
+ if (!text && (sp = strrchr(cp, '/'))) {
cp = sp + 1;
-
+ }
printf("%s\n", cp);
}
- } while (*field != '\0' && *field != '-');
+ } while (*field && *field != '-');
- /* Clean up. */
free(field);
fclose(fp);
char tmpfil[BUFSIZ];
struct stat st;
FILE *tmp;
- int c; /* current character */
+ int c;
int count; /* header field (annotation) counter */
- char *field = NULL; /* buffer for header field */
- int field_size = 0; /* size of field buffer */
- FILE *fp = NULL; /* file pointer made from locked file descriptor */
- int length; /* length of field name */
+ char *field = NULL;
+ int field_size = 0;
+ FILE *fp = NULL;
+ int length;
int n; /* number of bytes written */
mode = fstat(fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
- strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil));
+ strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
+ sizeof(tmpfil));
chmod(tmpfil, mode);
/*
** a buffer to hold the header components as they're read in.
** This buffer is grown as needed later.
*/
-
if (delete >= -1 || append != 0) {
- if ((fp = fdopen(fd, "r")) == (FILE *)0)
+ if ((fp = fdopen(fd, "r")) == NULL) {
adios(NULL, "unable to fdopen file.");
-
+ }
field = (char *)mh_xmalloc(field_size = 256);
}
/*
- ** We're trying to delete a header field (annotation )if the
- ** delete flag is not -2 or less. A value greater than zero
+ ** We're trying to delete a header field (annotation) if the
+ ** delete flag is greater -2. A value greater than zero
** means that we're deleting the nth header field that matches
** the field (component) name. A value of zero means that
** we're deleting the first field in which both the field name
** to make it more general. A value of -1 means to delete all
** matching fields.
*/
-
if (delete >= -1) {
- /* Get the length of the field name since we use it often. */
- length = strlen(comp);
-
- /*
- ** Initialize the field counter. This is only used if
- ** we're deleting by number.
- */
- count = 0;
+ length = strlen(comp); /* convenience copy */
+ count = 0; /* Only used if we're deleting by number. */
/*
** Copy lines from the input file to the temporary file
** with a - terminate the headers so that we can handle
** both drafts and RFC-2822 format messages.
*/
-
do {
/*
** Get a line from the input file, growing 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') {
+ for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
+ if (c == '\n' && (c = getc(fp)) != ' ' &&
+ c != '\t') {
ungetc(c, fp);
c = '\n';
break;
}
if (++n >= field_size - 1) {
- field = (char *) mh_xrealloc((void *)field, field_size *= 2);
-
+ field = (char *) mh_xrealloc(field,
+ field_size *= 2);
cp = field + n - 1;
}
}
-
- /* NUL-terminate the field */
*cp = '\0';
/*
** going to delete all fields with a
** matching field name.
*/
-
- if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
- if (delete == 0) {
- if (text == NULL)
+ if (strncasecmp(field, comp, length)==0 &&
+ field[length] == ':') {
+ if (!delete) {
+ if (!text) {
break;
-
- for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
- ;
-
- if (*text == '/') {
- if (strcmp(cp, text) == 0)
- break;
}
- else {
- if ((sp = strrchr(cp, '/')) != NULL)
+ for (cp=field+length+1;
+ *cp==' ' || *cp=='\t';
+ cp++) {
+ continue;
+ }
+ if (*text == '/' &&
+ strcmp(cp, text)==0) {
+ break;
+ } else {
+ if ((sp = strrchr(cp, '/'))) {
cp = sp + 1;
-
- if (strcmp(cp, text) == 0)
+ }
+ if (strcmp(cp, text)==0) {
break;
+ }
}
- }
-
- else if (delete == -1)
- continue;
-
- else if (++count == delete)
+ } 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))
+ 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);
+ }
+ } while (*field && *field != '-');
+ free(field);
} else {
/*
** 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')
+ if ((c = getc(fp)) == '\n') {
rewind(fp);
-
- else {
+ } else {
putc(c, tmp);
-
while ((c = getc(fp)) != EOF) {
putc(c, tmp);
-
if (c == '\n') {
ungetc(c = getc(fp), fp);
-
- if (c == '\n' || c == '-')
+ if (c == '\n' || c == '-') {
break;
+ }
}
}
}
}
- if (datesw)
+ if (datesw) {
fprintf(tmp, "%s: %s\n", comp, dtimenow(0));
+ }
if ((cp = text)) {
do {
- while (*cp == ' ' || *cp == '\t')
+ while (*cp == ' ' || *cp == '\t') {
cp++;
+ }
sp = cp;
- while (*cp && *cp++ != '\n')
+ while (*cp && *cp++ != '\n') {
continue;
- if (cp - sp)
- fprintf(tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
+ }
+ if (cp - sp) {
+ fprintf(tmp, "%s: %*.*s", comp,
+ (int)(cp - sp),
+ (int)(cp - sp), sp);
+ }
} while (*cp);
- if (cp[-1] != '\n' && cp != text)
+ if (cp[-1] != '\n' && cp != text) {
putc('\n', tmp);
+ }
}
}
-
fflush(tmp);
/*
** because the stock data copying routine uses the descriptor,
** not the pointer.
*/
-
if (append || delete >= -1) {
- if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
+ if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
adios(NULL, "can't seek.");
+ }
}
cpydata(fd, fileno(tmp), file, tmpfil);
fclose(tmp);
- if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK)
+ if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
adios(tmpfil, "unable to open for re-reading");
-
+ }
lseek(fd, (off_t) 0, SEEK_SET);
/*
- ** We're making the file shorter if we're deleting a
- ** header field so the file has to be truncated or it
- ** will contain garbage.
+ ** We're making the file shorter if we're deleting a header field
+ ** so the file has to be truncated or it will contain garbage.
*/
-
- if (delete >= -1 && ftruncate(fd, 0) == -1)
+ if (delete >= -1 && ftruncate(fd, 0) == -1) {
adios(tmpfil, "unable to truncate.");
-
+ }
cpydata(tmpfd, fd, tmpfil, file);
close(tmpfd);
unlink(tmpfil);
** we're doing piles of files. Note that this will make the close() in
** lkclose() fail, but that failure is ignored so it's not a problem.
*/
-
- if (delete >= -1)
+ if (delete >= -1) {
fclose(fp);
-
+ }
return 0;
}