Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / annosbr.c
1 /* annosbr.c - prepend annotation to messages */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: annosbr.c,v 2.6 1993/08/25 17:24:39 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include "../zotnet/tws.h"
8 #include <errno.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13
14 extern int  errno;
15 off_t lseek ();
16 static annosbr();
17
18 /* \f */
19
20 annotate (file, comp, text, inplace, datesw)
21 register char   *file,
22                 *comp,
23                 *text;
24 int     inplace,
25         datesw;
26 {
27     int     i,
28             fd;
29
30     if ((fd = lkopen (file, 2)) == NOTOK) {
31         switch (errno) {
32             case ENOENT: 
33                 break;
34
35             default: 
36                 admonish (file, "unable to lock and open");
37                 break;
38         }
39         return 1;
40     }
41
42     i = annosbr (fd, file, comp, text, inplace, datesw);
43
44     (void) lkclose (fd, file);
45
46     return i;
47 }
48
49 /* \f */
50
51 static  annosbr (src, file, comp, text, inplace, datesw)
52 register char  *file,
53                *comp,
54                *text;
55 int     src,
56         inplace,
57         datesw;
58 {
59     int     mode,
60             fd;
61     register char  *cp,
62                    *sp;
63     char    buffer[BUFSIZ],
64             tmpfil[BUFSIZ];
65     struct stat st;
66     register    FILE *tmp;
67
68     mode = fstat (src, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
69
70     (void) strcpy (tmpfil, m_scratch (file, "annotate"));
71
72     if ((tmp = fopen (tmpfil, "w")) == NULL) {
73         admonish (tmpfil, "unable to create");
74         return 1;
75     }
76     (void) chmod (tmpfil, mode);
77
78     if (datesw)
79         fprintf (tmp, "%s: %s\n", comp, dtimenow ());
80     if (cp = text) {
81         do {
82             while (*cp == ' ' || *cp == '\t')
83                 cp++;
84             sp = cp;
85             while (*cp && *cp++ != '\n')
86                 continue;
87             if (cp - sp)
88                 fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
89         } while (*cp);
90         if (cp[-1] != '\n' && cp != text)
91             (void) putc ('\n', tmp);
92     }
93     (void) fflush (tmp);
94     cpydata (src, fileno (tmp), file, tmpfil);
95     (void) fclose (tmp);
96
97     if (inplace) {
98         if ((fd = open (tmpfil, 0)) == NOTOK)
99             adios (tmpfil, "unable to open for re-reading");
100         (void) lseek (src, (off_t)0, 0);
101         cpydata (fd, src, tmpfil, file);
102         (void) close (fd);
103         (void) unlink (tmpfil);
104     }
105     else {
106         (void) strcpy (buffer, m_backup (file));
107         if (rename (file, buffer) == NOTOK) {
108             switch (errno) {
109                 case ENOENT:    /* unlinked early - no annotations */
110                     (void) unlink (tmpfil);
111                     break;
112
113                 default:
114                     admonish (buffer, "unable to rename %s to", file);
115                     break;
116             }
117             return 1;
118         }
119         if (rename (tmpfil, file) == NOTOK) {
120             admonish (file, "unable to rename %s to", tmpfil);
121             return 1;
122         }
123     }
124
125     return 0;
126 }