3 * annosbr.c -- prepend annotation to messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
22 static int annosbr (int, char *, char *, char *, int, int);
26 annotate (char *file, char *comp, char *text, int inplace, int datesw)
30 /* open and lock the file to be annotated */
31 if ((fd = lkopen (file, O_RDWR, 0)) == NOTOK) {
37 admonish (file, "unable to lock and open");
43 i = annosbr (fd, file, comp, text, inplace, datesw);
50 annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw)
54 char buffer[BUFSIZ], tmpfil[BUFSIZ];
58 mode = fstat (fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
60 strncpy (tmpfil, m_scratch (file, "annotate"), sizeof(tmpfil));
62 if ((tmp = fopen (tmpfil, "w")) == NULL) {
63 admonish (tmpfil, "unable to create");
69 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
72 while (*cp == ' ' || *cp == '\t')
75 while (*cp && *cp++ != '\n')
78 fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
80 if (cp[-1] != '\n' && cp != text)
84 cpydata (fd, fileno (tmp), file, tmpfil);
88 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
89 adios (tmpfil, "unable to open for re-reading");
90 lseek (fd, (off_t) 0, SEEK_SET);
91 cpydata (tmpfd, fd, tmpfil, file);
95 strncpy (buffer, m_backup (file), sizeof(buffer));
96 if (rename (file, buffer) == NOTOK) {
98 case ENOENT: /* unlinked early - no annotations */
103 admonish (buffer, "unable to rename %s to", file);
108 if (rename (tmpfil, file) == NOTOK) {
109 admonish (file, "unable to rename %s to", tmpfil);