2 Proprietary Rand Corporation
, 1981.
3 Further distribution of
this software
4 subject to the terms of the Rand
10 #include <sys/types.h>
15 /* annotate file component data
17 * prepends Component: data
21 annotate(file
, comp
, text
, inplace
)
22 char *file
, *comp
, *text
;
28 char buf
[BUFSIZ
], *sp
, tmpfil
[128];
33 if((src
= open((cp
= file
), 2)) == -1) { /* this should be an X-open*/
34 fprintf(stderr
, "Can't open ");
39 sp
= r1bindex(buf
, '/');
42 cp
= copy(buf
, 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 ");
52 VOID
chmod(tmpfil
, (int)stbuf
.st_mode
&0777);
54 if(*cp
>= 'a' && *cp
<= 'z') *cp
-= 040;
55 now
= time((long *)0);
57 cp
[9] = ' '; cp
[15] = 0;
59 fprintf(tmp
, "%s: <<%s>>\n", comp
, cp
);
62 if(*cp
== ' ' || *cp
== '\t') cp
++;
64 while(*cp
&& *cp
++ != '\n') ;
66 fprintf(tmp
, "%s: %*.*s", comp
, cp
-sp
, cp
-sp
, sp
);
68 if(cp
[-1] != '\n' && cp
!= text
) putc('\n', tmp
);
70 if((cnt
= read(src
, buf
, sizeof buf
)) > 0)
71 if(fwrite(buf
, cnt
, 1, tmp
) != 1) {
72 fprintf(stderr
, "anno: Error writing to ");
76 while(cnt
== sizeof buf
);
79 fd
= open(tmpfil
, 0); /* reopen for reading */
80 VOID
lseek(src
, 0l, 0);
82 if((cnt
= read(fd
, buf
, sizeof buf
)) > 0)
83 if(write(src
, buf
, cnt
) != cnt
) {
84 fprintf(stderr
, "anno: Error rewriting ");
88 while(cnt
== sizeof buf
);
91 /* cp = copy(file, buf); */
93 /* VOID copy(".bak", copy(file, buf)); */
98 while(--cp
>= buf
&& *cp
!= '/');
99 *++cp
= ','; /* New backup convention */
101 if(link(file
, buf
) == -1) {
102 fprintf(stderr
, "Can't rename %s to bak file.\n", file
);
105 if(unlink(file
) == -1) {
106 fprintf(stderr
, "Can't unlink %s\n", file
);
109 if(link(tmpfil
, file
) == -1) {
110 fprintf(stderr
, "Can't lnk temp file \"%s\" to %s\n",