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