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