1*36027Sbostic static char sccsid[] = "@(#)arcv.c 4.2 10/21/88";
2609Sbill /*
3609Sbill * arcv - convert old to new archive format
4609Sbill */
5609Sbill
6609Sbill #include <signal.h>
7895Sbill #include <ar.h>
8609Sbill #define OARMAG 0177545
9609Sbill struct oar_hdr {
10609Sbill char oar_name[14];
11609Sbill long oar_date;
12609Sbill char oar_uid;
13609Sbill char oar_gid;
14609Sbill int oar_mode;
15609Sbill long oar_size;
16609Sbill };
17609Sbill
18609Sbill struct ar_hdr nh;
19609Sbill struct oar_hdr oh;
20*36027Sbostic char tmp[] = "/usr/tmp/arcXXXXXX";
21609Sbill char *mktemp();
22609Sbill int f;
23609Sbill char buf[512];
24609Sbill int tf;
main(argc,argv)25609Sbill main(argc, argv)
26609Sbill char *argv[];
27609Sbill {
28609Sbill register i;
29609Sbill
30609Sbill if (argc>1 && strcmp(argv[1], "-t")==0) {
31609Sbill argc--;
32609Sbill argv++;
33*36027Sbostic } else {
34*36027Sbostic strcpy(tmp, "/tmp/arcXXXXXX");
35*36027Sbostic }
36*36027Sbostic mktemp(tmp);
37609Sbill for(i=1; i<4; i++)
38609Sbill signal(i, SIG_IGN);
39609Sbill for(i=1; i<argc; i++) {
40609Sbill if (argc>1)
41609Sbill printf("%s:\n", argv[i]);
42609Sbill conv(argv[i]);
43609Sbill }
44609Sbill unlink(tmp);
45609Sbill return(0);
46609Sbill }
47609Sbill
conv(fil)48609Sbill conv(fil)
49609Sbill char *fil;
50609Sbill {
51609Sbill int oldmagic;
52609Sbill long n;
53609Sbill unsigned i;
54609Sbill
55609Sbill f = open(fil, 2);
56609Sbill if(f < 0) {
57609Sbill printf("arcv: cannot open %s\n", fil);
58609Sbill return;
59609Sbill }
60609Sbill close(creat(tmp, 0600));
61609Sbill tf = open(tmp, 2);
62609Sbill if(tf < 0) {
63609Sbill printf("arcv: cannot open temp\n");
64609Sbill close(f);
65609Sbill return;
66609Sbill }
67609Sbill oldmagic = 0;
68609Sbill read(f, (char *)&oldmagic, sizeof(oldmagic));
69609Sbill if(oldmagic != 0177545) {
70609Sbill printf("arcv: %s not old archive format\n", fil);
71609Sbill close(tf);
72609Sbill close(f);
73609Sbill return;
74609Sbill }
75609Sbill chkwrite(tf, ARMAG, SARMAG);
76609Sbill loop:
77609Sbill i = read(f, (char *)&oh, sizeof(oh));
78609Sbill if(i != sizeof(oh))
79609Sbill goto out;
80609Sbill
81609Sbill sprintf(buf, "%-16.14s%-12ld%-6u%-6u%-8o%-10ld%-2s",
82609Sbill oh.oar_name,
83609Sbill oh.oar_date,
84609Sbill oh.oar_uid,
85609Sbill oh.oar_gid,
86609Sbill (unsigned short)oh.oar_mode,
87609Sbill oh.oar_size,
88609Sbill ARFMAG);
89609Sbill strncpy((char *)&nh, buf, sizeof(nh));
90609Sbill n = oh.oar_size;
91609Sbill chkwrite(tf, (char *)&nh, sizeof(nh));
92609Sbill while(n > 0) {
93609Sbill i = 512;
94609Sbill if (n<i)
95609Sbill i = n;
96609Sbill read(f, buf, i&01? i+1:i);
97609Sbill if (i&01) {
98609Sbill buf[i] = '\n';
99609Sbill i++;
100609Sbill }
101609Sbill chkwrite(tf, buf, i);
102609Sbill n -= i;
103609Sbill }
104609Sbill goto loop;
105609Sbill out:
106609Sbill lseek(f, 0L, 0);
107609Sbill lseek(tf, 0L, 0);
108609Sbill while((i=read(tf, buf, 512)) > 0)
109609Sbill chkwrite(f, buf, i);
110609Sbill close(f);
111609Sbill close(tf);
112609Sbill }
113609Sbill
chkwrite(f,b,n)114609Sbill chkwrite(f, b, n)
115609Sbill char *b;
116609Sbill {
117609Sbill if (write(f, b, n) != n) {
118609Sbill printf("arcv: write error\n");
119609Sbill unlink(tmp);
120609Sbill exit(1);
121609Sbill }
122609Sbill }
123