Mercurial > hg > Applications > mh
comparison uip/annosbr.c @ 0:bce86c4163a3
Initial revision
author | kono |
---|---|
date | Mon, 18 Apr 2005 23:46:02 +0900 |
parents | |
children | a6481689f99c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bce86c4163a3 |
---|---|
1 /* annosbr.c - prepend annotation to messages */ | |
2 #ifndef lint | |
3 static char ident[] = "@(#)$Id$"; | |
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 /* */ | |
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 #ifndef __CYGWIN32__ | |
45 /* cygwin32 cannot rename() my locked file. X-< */ | |
46 (void) lkclose (fd, file); | |
47 #endif | |
48 | |
49 return i; | |
50 } | |
51 | |
52 /* */ | |
53 | |
54 static annosbr (src, file, comp, text, inplace, datesw) | |
55 register char *file, | |
56 *comp, | |
57 *text; | |
58 int src, | |
59 inplace, | |
60 datesw; | |
61 { | |
62 int mode, | |
63 fd; | |
64 register char *cp, | |
65 *sp; | |
66 char buffer[BUFSIZ], | |
67 tmpfil[BUFSIZ]; | |
68 struct stat st; | |
69 register FILE *tmp; | |
70 | |
71 mode = fstat (src, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot (); | |
72 | |
73 (void) strcpy (tmpfil, m_scratch (file, "annotate")); | |
74 | |
75 if ((tmp = fopen (tmpfil, "w")) == NULL) { | |
76 admonish (tmpfil, "unable to create"); | |
77 return 1; | |
78 } | |
79 (void) chmod (tmpfil, mode); | |
80 | |
81 if (datesw) | |
82 fprintf (tmp, "%s: %s\n", comp, dtimenow ()); | |
83 if (cp = text) { | |
84 do { | |
85 while (*cp == ' ' || *cp == '\t') | |
86 cp++; | |
87 sp = cp; | |
88 while (*cp && *cp++ != '\n') | |
89 continue; | |
90 if (cp - sp) | |
91 fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp); | |
92 } while (*cp); | |
93 if (cp[-1] != '\n' && cp != text) | |
94 (void) putc ('\n', tmp); | |
95 } | |
96 (void) fflush (tmp); | |
97 cpydata (src, fileno (tmp), file, tmpfil); | |
98 (void) fclose (tmp); | |
99 | |
100 if (inplace) { | |
101 if ((fd = open (tmpfil, 0)) == NOTOK) | |
102 adios (tmpfil, "unable to open for re-reading"); | |
103 (void) lseek (src, (off_t)0, 0); | |
104 cpydata (fd, src, tmpfil, file); | |
105 (void) close (fd); | |
106 (void) unlink (tmpfil); | |
107 #ifdef __CYGWIN32__ | |
108 (void) lkclose (src, file); | |
109 #endif | |
110 } | |
111 else { | |
112 (void) strcpy (buffer, m_backup (file)); | |
113 #ifdef __CYGWIN32__ | |
114 (void) lkclose (src, file); | |
115 #endif | |
116 if (rename (file, buffer) == NOTOK) { | |
117 switch (errno) { | |
118 case ENOENT: /* unlinked early - no annotations */ | |
119 (void) unlink (tmpfil); | |
120 break; | |
121 | |
122 default: | |
123 admonish (buffer, "unable to rename %s to", file); | |
124 break; | |
125 } | |
126 return 1; | |
127 } | |
128 if (rename (tmpfil, file) == NOTOK) { | |
129 admonish (file, "unable to rename %s to", tmpfil); | |
130 return 1; | |
131 } | |
132 } | |
133 | |
134 return 0; | |
135 } |