|
|
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: static int annosbr();
16:
17: /* */
18:
19: annotate(file, comp, text, inplace)
20: register char *file, *comp, *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 int
48: annosbr(src, file, comp, text, inplace)
49: register char *file, *comp, *text;
50: int src, inplace;
51: {
52: int mode,
53: fd;
54: register char *cp,
55: *sp;
56: char buffer[BUFSIZ],
57: tmpfil[BUFSIZ];
58: struct stat st;
59: register FILE *tmp;
60:
61: mode = fstat (src, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot ();
62:
63: (void) strcpy (tmpfil, m_scratch (file, "annotate"));
64:
65: if ((tmp = fopen (tmpfil, "w")) == NULL) {
66: admonish (tmpfil, "unable to create");
67: return 1;
68: }
69: (void) chmod (tmpfil, mode);
70:
71: fprintf (tmp, "%s: %s\n", comp, dtimenow ());
72: if (cp = text) {
73: do {
74: while (*cp == ' ' || *cp == '\t')
75: cp++;
76: sp = cp;
77: while (*cp && *cp++ != '\n')
78: continue;
79: if (cp - sp)
80: fprintf (tmp, "%s: %*.*s", comp, cp - sp, cp - sp, sp);
81: } while (*cp);
82: if (cp[-1] != '\n' && cp != text)
83: (void) putc ('\n', tmp);
84: }
85: (void) fflush (tmp);
86: cpydata (src, fileno (tmp), file, tmpfil);
87: (void) fclose (tmp);
88:
89: if (inplace) {
90: if ((fd = open (tmpfil, 0)) == NOTOK)
91: adios (tmpfil, "unable to open for re-reading");
92: (void) lseek (src, 0L, 0);
93: cpydata (fd, src, tmpfil, file);
94: (void) close (fd);
95: (void) unlink (tmpfil);
96: }
97: else {
98: (void) strcpy (buffer, m_backup (file));
99: if (rename (file, buffer) == NOTOK) {
100: admonish (buffer, "unable to rename %s to", file);
101: return 1;
102: }
103: if (rename (tmpfil, file) == NOTOK) {
104: admonish (file, "unable to rename %s to", tmpfil);
105: return 1;
106: }
107: }
108:
109: return 0;
110: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.