|
|
1.1 root 1: /* annosbr.c - prepend annotation to messages */
2:
3: #include "../h/mh.h"
4: #include "../zotnet/tws.h"
5: #include <errno.h>
6: #include <stdio.h>
7: #include <sys/types.h>
8: #include <sys/stat.h>
9:
10:
11: extern int errno;
12:
13: long lseek ();
14:
15: /* */
16:
17: annotate (file, comp, text, inplace)
18: register char *file,
19: *comp,
20: *text;
21: int inplace;
22: {
23: int i,
24: fd;
25:
26: if ((fd = lkopen (file, 2)) == NOTOK) {
27: switch (errno) {
28: case ENOENT:
29: break;
30:
31: default:
32: admonish (file, "unable to lock and open");
33: break;
34: }
35: return 1;
36: }
37:
38: i = annosbr (fd, file, comp, text, inplace);
39:
40: (void) lkclose (fd, file);
41:
42: return i;
43: }
44:
45: /* */
46:
47: static annosbr (src, file, comp, text, inplace)
48: register char *file,
49: *comp,
50: *text;
51: int src,
52: inplace;
53: {
54: int mode,
55: fd;
56: register char *cp,
57: *sp;
58: char buffer[BUFSIZ],
59: tmpfil[BUFSIZ];
60: struct stat st;
61: register FILE *tmp;
62:
63: mode = fstat (src, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
64:
65: (void) strcpy (tmpfil, m_scratch (file, "annotate"));
66:
67: if ((tmp = fopen (tmpfil, "w")) == NULL) {
68: admonish (tmpfil, "unable to create");
69: return 1;
70: }
71: (void) chmod (tmpfil, mode);
72:
73: fprintf (tmp, "%s: %s\n", comp, dtimenow ());
74: if (cp = text) {
75: do {
76: while (*cp == ' ' || *cp == '\t')
77: cp++;
78: sp = cp;
79: while (*cp && *cp++ != '\n')
80: continue;
81: if (cp - sp)
82: fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
83: } while (*cp);
84: if (cp[-1] != '\n' && cp != text)
85: (void) putc ('\n', tmp);
86: }
87: (void) fflush (tmp);
88: cpydata (src, fileno (tmp), file, tmpfil);
89: (void) fclose (tmp);
90:
91: if (inplace) {
92: if ((fd = open (tmpfil, 0)) == NOTOK)
93: adios (tmpfil, "unable to open for re-reading");
94: (void) lseek (src, 0L, 0);
95: cpydata (fd, src, tmpfil, file);
96: (void) close (fd);
97: (void) unlink (tmpfil);
98: }
99: else {
100: (void) strcpy (buffer, m_backup (file));
101: if (rename (file, buffer) == NOTOK) {
102: admonish (buffer, "unable to rename %s to", file);
103: return 1;
104: }
105: if (rename (tmpfil, file) == NOTOK) {
106: admonish (file, "unable to rename %s to", tmpfil);
107: return 1;
108: }
109: }
110:
111: return 0;
112: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.