3 * annosbr.c -- prepend annotation to messages
18 static int annosbr (int, char *, char *, char *, int, int);
22 annotate (char *file, char *comp, char *text, int inplace, int datesw)
26 /* open and lock the file to be annotated */
27 if ((fd = lkopen (file, O_RDWR, 0)) == NOTOK) {
33 admonish (file, "unable to lock and open");
39 i = annosbr (fd, file, comp, text, inplace, datesw);
46 annosbr (int fd, char *file, char *comp, char *text, int inplace, int datesw)
50 char buffer[BUFSIZ], tmpfil[BUFSIZ];
54 mode = fstat (fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
56 strncpy (tmpfil, m_scratch (file, "annotate"), sizeof(tmpfil));
58 if ((tmp = fopen (tmpfil, "w")) == NULL) {
59 admonish (tmpfil, "unable to create");
65 fprintf (tmp, "%s: %s\n", comp, dtimenow (0));
68 while (*cp == ' ' || *cp == '\t')
71 while (*cp && *cp++ != '\n')
74 fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
76 if (cp[-1] != '\n' && cp != text)
80 cpydata (fd, fileno (tmp), file, tmpfil);
84 if ((tmpfd = open (tmpfil, O_RDONLY)) == NOTOK)
85 adios (tmpfil, "unable to open for re-reading");
86 lseek (fd, (off_t) 0, SEEK_SET);
87 cpydata (tmpfd, fd, tmpfil, file);
91 strncpy (buffer, m_backup (file), sizeof(buffer));
92 if (rename (file, buffer) == NOTOK) {
94 case ENOENT: /* unlinked early - no annotations */
99 admonish (buffer, "unable to rename %s to", file);
104 if (rename (tmpfil, file) == NOTOK) {
105 admonish (file, "unable to rename %s to", tmpfil);