]> diplodocus.org Git - nmh/blob - docs/historical/SRI-NOSC/annotate.c
Added start_test/finish_test to a bunch of tests.
[nmh] / docs / historical / SRI-NOSC / annotate.c
1 #include "mh.h"
2 #include "stat.h"
3
4 /* annotate file component data
5 *
6 * prepends Component: data
7 * date stamp
8 */
9
10 /*extern int inplace;*/ /* preserve links in anno */
11 int inplace 1; /* preserve links in anno */
12
13 annotate(file, comp, text)
14 char *file, *comp, *text;
15 {
16 register int src, tmp;
17 register char *cp;
18 int cnt;
19 char buf[512], *sp, tmpfil[128];
20 long now;
21 struct inode stbuf;
22
23 if((src = open((cp = file), 2)) == -1) { /* this should be an X-open*/
24 printf("Can't open "); flush();
25 perror(cp);
26 return(1);
27 }
28 copy(cp, buf);
29 sp = cp = buf;
30 while(*cp) if(*cp++ == '/') sp = cp;
31 if(sp != buf) {
32 *sp = 0;
33 cp = copy(buf, tmpfil);
34 } else
35 cp = tmpfil;
36 copy(makename("annot",".tmp"), cp);
37 fstat(src, &stbuf);
38 if((tmp = creat(tmpfil, stbuf.i_mode&0777)) == -1) {
39 printf("Can't create "); flush();
40 perror(tmpfil);
41 return(1);
42 }
43 cp = comp;
44 if(*cp >= 'a' && *cp <= 'z') *cp =- 040;
45 type(tmp, cp);
46 type(tmp, ": ");
47 time(&now);
48 cp = cdate(&now);
49 cp[9] = ' '; cp[15] = 0;
50 if(*cp == ' ') cp++;
51 type(tmp, "<<");
52 type(tmp, cp);
53 type(tmp, ">>\n");
54 cp = text;
55 do {
56 if(*cp == ' ' || *cp == '\t') cp++;
57 sp = cp;
58 while(*cp && *cp++ != '\n') ;
59 if(cp - sp) {
60 type(tmp, comp);
61 type(tmp, ": ");
62 write(tmp, sp, cp-sp);
63 }
64 } while(*cp);
65 if(cp[-1] != '\n' && cp != text) type(tmp, "\n");
66 do
67 if((cnt = read(src, buf, sizeof buf)) > 0)
68 write(tmp, buf, cnt);
69 while(cnt == sizeof buf);
70 if(inplace) {
71 close(tmp);
72 tmp = open(tmpfil, 0); /* reopen for reading */
73 seek(src, 0, 0);
74 do
75 if((cnt = read(tmp, buf, sizeof buf)) > 0)
76 write(src, buf, cnt);
77 while(cnt == sizeof buf);
78 } else {
79 /* cp = copy(file, buf); */
80 /* *--cp =| 0200; */
81 /* copy(".bak", copy(file, buf)); */
82 cp = copy(file, buf);
83 cp[1] = 0;
84 do
85 *cp = cp[-1];
86 while(--cp >= buf && *cp != '/');
87 *++cp = ','; /* New backup convention */
88 unlink(buf);
89 if(link(file, buf) == -1) {
90 printf("Can't rename %s to bak file.\n", file);
91 return(1);
92 }
93 if(unlink(file) == -1) {
94 printf("Can't unlink %s\n", file);
95 return(1);
96 }
97 if(link(tmpfil, file) == -1) {
98 printf("Can't lnk temp file \"%s\" to %s\n",
99 tmpfil, file);
100 return(1);
101 }
102 }
103 close(src);
104 close(tmp);
105 unlink(tmpfil);
106 return(0);
107 }