]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/progs/annotate.c
Updated documentation and comments about sendmail/pipe.
[nmh] / docs / historical / mh-jun-1982 / progs / annotate.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 time_t time();
13 long lseek();
14
15 /* annotate file component data
16 *
17 * prepends Component: data
18 * date stamp
19 */
20
21 annotate(file, comp, text, inplace)
22 char *file, *comp, *text;
23 {
24 register int src;
25 register char *cp;
26 register FILE *tmp;
27 int cnt, fd;
28 char buf[BUFSIZ], *sp, tmpfil[128];
29 long now;
30 struct stat stbuf;
31 char *cdate();
32
33 if((src = open((cp = file), 2)) == -1) { /* this should be an X-open*/
34 fprintf(stderr, "Can't open ");
35 perror(cp);
36 return(1);
37 }
38 VOID copy(cp, buf);
39 sp = r1bindex(buf, '/');
40 if(sp != buf) {
41 *sp = 0;
42 cp = copy(buf, tmpfil);
43 } else
44 cp = tmpfil;
45 VOID copy(makename("ano",".tmp"), cp);
46 VOID fstat(src, &stbuf);
47 if((tmp = fopen(tmpfil, "w")) == NULL) {
48 fprintf(stderr, "Can't create ");
49 perror(tmpfil);
50 return(1);
51 }
52 VOID chmod(tmpfil, (int)stbuf.st_mode&0777);
53 cp = comp;
54 if(*cp >= 'a' && *cp <= 'z') *cp -= 040;
55 now = time((long *)0);
56 cp = cdate(&now);
57 cp[9] = ' '; cp[15] = 0;
58 if(*cp == ' ') cp++;
59 fprintf(tmp, "%s: <<%s>>\n", comp, cp);
60 cp = text;
61 do {
62 if(*cp == ' ' || *cp == '\t') cp++;
63 sp = cp;
64 while(*cp && *cp++ != '\n') ;
65 if(cp - sp)
66 fprintf(tmp, "%s: %*.*s", comp, cp-sp, cp-sp, sp);
67 } while(*cp);
68 if(cp[-1] != '\n' && cp != text) putc('\n', tmp);
69 do
70 if((cnt = read(src, buf, sizeof buf)) > 0)
71 if(fwrite(buf, cnt, 1, tmp) != 1) {
72 fprintf(stderr, "anno: Error writing to ");
73 perror(tmpfil);
74 done(1);
75 }
76 while(cnt == sizeof buf);
77 VOID fclose(tmp);
78 if(inplace) {
79 fd = open(tmpfil, 0); /* reopen for reading */
80 VOID lseek(src, 0l, 0);
81 do
82 if((cnt = read(fd, buf, sizeof buf)) > 0)
83 if(write(src, buf, cnt) != cnt) {
84 fprintf(stderr, "anno: Error rewriting ");
85 perror(file);
86 done(1);
87 }
88 while(cnt == sizeof buf);
89 VOID close(fd);
90 } else {
91 /* cp = copy(file, buf); */
92 /* *--cp =| 0200; */
93 /* VOID copy(".bak", copy(file, buf)); */
94 cp = copy(file, buf);
95 cp[1] = 0;
96 do
97 *cp = cp[-1];
98 while(--cp >= buf && *cp != '/');
99 *++cp = ','; /* New backup convention */
100 VOID unlink(buf);
101 if(link(file, buf) == -1) {
102 fprintf(stderr, "Can't rename %s to bak file.\n", file);
103 return(1);
104 }
105 if(unlink(file) == -1) {
106 fprintf(stderr, "Can't unlink %s\n", file);
107 return(1);
108 }
109 if(link(tmpfil, file) == -1) {
110 fprintf(stderr, "Can't lnk temp file \"%s\" to %s\n",
111 tmpfil, file);
112 return(1);
113 }
114 }
115 VOID close(src);
116 VOID unlink(tmpfil);
117 retur