source: trunk/minix/commands/aal/archiver.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 15.2 KB
Line 
1/*
2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
4 */
5/* ar - archiver Author: Michiel Huisjes */
6/* Made into arch/aal by Ceriel Jacobs
7 */
8
9static char RcsId[] = "$Header: /cvsup/minix/src/commands/aal/archiver.c,v 1.1.1.1 2005/04/21 14:53:57 beng Exp $";
10
11/*
12 * Usage: [arch|aal] [adprtvx] archive [file] ...
13 * v: verbose
14 * x: extract
15 * a: append
16 * r: replace (append when not in archive)
17 * d: delete
18 * t: print contents of archive
19 * p: print named files
20 * l: temporaries in current directory instead of /usr/tmp
21 * c: don't give "create" message
22 * u: replace only if dated later than member in archive
23#ifdef DISTRIBUTION
24 * D: make distribution: use distr_time, uid=2, gid=2, mode=0644
25#endif
26 */
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#ifndef S_IREAD
31#define S_IREAD S_IRUSR
32#endif
33#ifndef S_IWRITE
34#define S_IWRITE S_IWUSR
35#endif
36#ifndef S_IEXEC
37#define S_IEXEC S_IXUSR
38#endif
39#include <signal.h>
40#include <arch.h>
41#ifdef AAL
42#include <ranlib.h>
43#include <out.h>
44#define MAGIC_NUMBER AALMAG
45long offset;
46struct ranlib *tab;
47unsigned int tnum = 0;
48char *tstrtab;
49unsigned int tssiz = 0;
50char *malloc(), *realloc(), *strcpy(), *strncpy();
51long time();
52unsigned int tabsz, strtabsz;
53#else
54#define MAGIC_NUMBER ARMAG
55#endif
56long lseek();
57
58#define odd(nr) (nr & 01)
59#define even(nr) (odd(nr) ? nr + 1 : nr)
60
61typedef char BOOL;
62#define FALSE 0
63#define TRUE 1
64
65#define READ 0
66#define APPEND 2
67#define CREATE 1
68
69#define MEMBER struct ar_hdr
70
71#define NIL_PTR ((char *) 0)
72#define NIL_MEM ((MEMBER *) 0)
73#define NIL_LONG ((long *) 0)
74
75#define IO_SIZE (10 * 1024)
76
77#define equal(str1, str2) (!strncmp((str1), (str2), 14))
78#ifndef S_ISDIR
79#define S_ISDIR(m) (m & S_IFDIR) /* is a directory */
80#endif
81
82BOOL verbose;
83BOOL app_fl;
84BOOL ex_fl;
85BOOL show_fl;
86BOOL pr_fl;
87BOOL rep_fl;
88BOOL del_fl;
89BOOL nocr_fl;
90BOOL local_fl;
91BOOL update_fl;
92#ifdef DISTRIBUTION
93BOOL distr_fl;
94long distr_time;
95#endif
96
97int ar_fd;
98
99char io_buffer[IO_SIZE];
100
101char *progname;
102
103char temp_buf[32];
104char *temp_arch = &temp_buf[0];
105extern char *mktemp();
106extern char *ctime();
107
108usage()
109{
110 error(TRUE, "usage: %s %s archive [file] ...\n",
111 progname,
112#ifdef AAL
113 "[acdrtxvlu]"
114#else
115 "[acdprtxvlu]"
116#endif
117 );
118}
119
120/*VARARGS2*/
121error(quit, str1, str2, str3, str4)
122BOOL quit;
123char *str1, *str2, *str3, *str4;
124{
125 char errbuf[256];
126
127 sprint(errbuf, str1, str2, str3, str4);
128 write(2, errbuf, strlen(errbuf));
129 if (quit) {
130 unlink(temp_arch);
131 _exit(1);
132 }
133}
134
135char *basename(path)
136char *path;
137{
138 register char *ptr = path;
139 register char *last = NIL_PTR;
140
141 while (*ptr != '\0') {
142 if (*ptr == '/')
143 last = ptr;
144 ptr++;
145 }
146 if (last == NIL_PTR)
147 return path;
148 if (*(last + 1) == '\0') {
149 *last = '\0';
150 return basename(path);
151 }
152 return last + 1;
153}
154
155extern unsigned int rd_unsigned2();
156
157open_archive(name, mode)
158register char *name;
159register int mode;
160{
161 unsigned short magic = 0;
162 int fd;
163
164 if (mode == CREATE) {
165 if ((fd = creat(name, 0666)) < 0)
166 error(TRUE, "cannot creat %s\n", name);
167 magic = MAGIC_NUMBER;
168 wr_int2(fd, magic);
169 return fd;
170 }
171
172 if ((fd = open(name, mode)) < 0) {
173 if (mode == APPEND) {
174 close(open_archive(name, CREATE));
175 if (!nocr_fl) error(FALSE, "%s: creating %s\n", progname, name);
176 return open_archive(name, APPEND);
177 }
178 error(TRUE, "cannot open %s\n", name);
179 }
180 lseek(fd, 0L, 0);
181 magic = rd_unsigned2(fd);
182 if (magic != AALMAG && magic != ARMAG)
183 error(TRUE, "%s is not in ar format\n", name);
184
185 return fd;
186}
187
188#if __STDC__
189void catch(int sig)
190#else
191catch()
192#endif
193{
194 unlink(temp_arch);
195 _exit (2);
196}
197
198main(argc, argv)
199int argc;
200char *argv[];
201{
202 register char *ptr;
203 int needs_arg = 0;
204
205 progname = argv[0];
206
207 if (argc < 3)
208 usage();
209
210 for (ptr = argv[1]; *ptr; ptr++) {
211 switch (*ptr) {
212 case 't' :
213 show_fl = TRUE;
214 break;
215 case 'v' :
216 verbose = TRUE;
217 break;
218 case 'x' :
219 ex_fl = TRUE;
220 break;
221 case 'a' :
222 needs_arg = 1;
223 app_fl = TRUE;
224 break;
225 case 'c' :
226 nocr_fl = TRUE;
227 break;
228#ifndef AAL
229 case 'p' :
230 needs_arg = 1;
231 pr_fl = TRUE;
232 break;
233#endif
234 case 'd' :
235 needs_arg = 1;
236 del_fl = TRUE;
237 break;
238 case 'r' :
239 needs_arg = 1;
240 rep_fl = TRUE;
241 break;
242 case 'l' :
243 local_fl = TRUE;
244 break;
245 case 'u' :
246 update_fl = TRUE;
247 break;
248#ifdef DISTRIBUTION
249 case 'D' :
250 distr_fl = TRUE;
251 break;
252#endif
253 default :
254 usage();
255 }
256 }
257
258 if (needs_arg && argc <= 3)
259 usage();
260#ifdef DISTRIBUTION
261 if (distr_fl) {
262 struct stat statbuf;
263
264 stat(progname, &statbuf);
265 distr_time = statbuf.st_mtime;
266 }
267#endif
268 if (local_fl) strcpy(temp_arch, "ar.XXXXXX");
269 else strcpy(temp_arch, "/usr/tmp/ar.XXXXXX");
270
271 if (app_fl + ex_fl + del_fl + rep_fl + show_fl + pr_fl != 1)
272 usage();
273
274 if (update_fl && !rep_fl)
275 usage();
276
277 if (rep_fl || del_fl
278#ifdef AAL
279 || app_fl
280#endif
281 ) {
282 mktemp(temp_arch);
283 }
284#ifdef AAL
285 tab = (struct ranlib *) malloc(512 * sizeof(struct ranlib));
286 tstrtab = malloc(4096);
287 if (!tab || !tstrtab) error(TRUE,"Out of core\n");
288 tabsz = 512;
289 strtabsz = 4096;
290#endif
291
292 signal(SIGINT, catch);
293 get(argc, argv);
294
295 return 0;
296}
297
298MEMBER *
299get_member()
300{
301 static MEMBER member;
302
303again:
304 if (rd_arhdr(ar_fd, &member) == 0)
305 return NIL_MEM;
306 if (member.ar_size < 0) {
307 error(TRUE, "archive has member with negative size\n");
308 }
309#ifdef AAL
310 if (equal(SYMDEF, member.ar_name)) {
311 lseek(ar_fd, member.ar_size, 1);
312 goto again;
313 }
314#endif
315 return &member;
316}
317
318char *get_mode();
319
320get(argc, argv)
321int argc;
322register char *argv[];
323{
324 register MEMBER *member;
325 int i = 0;
326 int temp_fd, read_chars;
327
328 ar_fd = open_archive(argv[2], (show_fl || pr_fl || ex_fl) ? READ : APPEND);
329 if (rep_fl || del_fl
330#ifdef AAL
331 || app_fl
332#endif
333 )
334 temp_fd = open_archive(temp_arch, CREATE);
335 while ((member = get_member()) != NIL_MEM) {
336 if (argc > 3) {
337 for (i = 3; i < argc; i++) {
338 if (equal(basename(argv[i]), member->ar_name))
339 break;
340 }
341 if (i == argc || app_fl) {
342 if (rep_fl || del_fl
343#ifdef AAL
344 || app_fl
345#endif
346 ) {
347#ifdef AAL
348 if (i != argc) {
349 print("%s: already in archive\n", argv[i]);
350 argv[i] = "";
351 }
352#endif
353 wr_arhdr(temp_fd, member);
354 copy_member(member, ar_fd, temp_fd, 0);
355 }
356 else {
357#ifndef AAL
358 if (app_fl && i != argc) {
359 print("%s: already in archive\n", argv[i]);
360 argv[i] = "";
361 }
362#endif
363 lseek(ar_fd, even(member->ar_size),1);
364 }
365 continue;
366 }
367 }
368 if (ex_fl || pr_fl)
369 extract(member);
370 else {
371 if (rep_fl) {
372 int isold = 0;
373 if(update_fl) {
374 struct stat status;
375 if (stat(argv[i], &status) >= 0) {
376 if(status.st_mtime <= member->ar_date)
377 isold = 1;
378 }
379 }
380 if(!isold)
381 add(argv[i], temp_fd, "r - %s\n");
382 else {
383 wr_arhdr(temp_fd, member);
384 copy_member(member, ar_fd, temp_fd, 0);
385 if(verbose)
386 show("r - %s (old)\n", member->ar_name);
387 }
388 }
389 else if (show_fl) {
390 char buf[sizeof(member->ar_name) + 2];
391 register char *p = buf, *q = member->ar_name;
392
393 while (q <= &member->ar_name[sizeof(member->ar_name)-1] && *q) {
394 *p++ = *q++;
395 }
396 *p++ = '\n';
397 *p = '\0';
398 if (verbose) {
399 char *mode = get_mode(member->ar_mode);
400 char *date = ctime(&(member->ar_date));
401
402 *(date + 16) = '\0';
403 *(date + 24) = '\0';
404
405 print("%s%3u/%u%7ld %s %s %s",
406 mode,
407 (unsigned) (member->ar_uid & 0377),
408 (unsigned) (member->ar_gid & 0377),
409 member->ar_size,
410 date+4,
411 date+20,
412 buf);
413 }
414 else print(buf);
415 }
416 else if (del_fl)
417 show("d - %s\n", member->ar_name);
418 lseek(ar_fd, even(member->ar_size), 1);
419 }
420 argv[i] = "";
421 }
422
423 if (argc > 3) {
424 for (i = 3; i < argc; i++)
425 if (argv[i][0] != '\0') {
426#ifndef AAL
427 if (app_fl)
428 add(argv[i], ar_fd, "a - %s\n");
429 else
430#endif
431 if (rep_fl
432#ifdef AAL
433 || app_fl
434#endif
435 )
436 add(argv[i], temp_fd, "a - %s\n");
437 else {
438 print("%s: not found\n", argv[i]);
439 }
440 }
441 }
442
443 if (rep_fl || del_fl
444#ifdef AAL
445 || app_fl
446#endif
447 ) {
448 signal(SIGINT, SIG_IGN);
449 close(ar_fd);
450 close(temp_fd);
451 ar_fd = open_archive(argv[2], CREATE);
452 temp_fd = open_archive(temp_arch, APPEND);
453#ifdef AAL
454 write_symdef();
455#endif
456 while ((read_chars = read(temp_fd, io_buffer, IO_SIZE)) > 0)
457 mwrite(ar_fd, io_buffer, read_chars);
458 close(temp_fd);
459 unlink(temp_arch);
460 }
461 close(ar_fd);
462}
463
464add(name, fd, mess)
465char *name;
466int fd;
467char *mess;
468{
469 static MEMBER member;
470 register int read_chars;
471 struct stat status;
472 int src_fd;
473
474 if (stat(name, &status) < 0) {
475 error(FALSE, "cannot find %s\n", name);
476 return;
477 }
478 else if (S_ISDIR(status.st_mode)) {
479 error(FALSE, "%s is a directory (ignored)\n", name);
480 return;
481 }
482 else if ((src_fd = open(name, 0)) < 0) {
483 error(FALSE, "cannot open %s\n", name);
484 return;
485 }
486
487 strncpy (member.ar_name, basename (name), sizeof(member.ar_name));
488 member.ar_uid = status.st_uid;
489 member.ar_gid = status.st_gid;
490 member.ar_mode = status.st_mode;
491 member.ar_date = status.st_mtime;
492 member.ar_size = status.st_size;
493#ifdef DISTRIBUTION
494 if (distr_fl) {
495 member.ar_uid = 2;
496 member.ar_gid = 2;
497 member.ar_mode = 0644;
498 member.ar_date = distr_time;
499 }
500#endif
501 wr_arhdr(fd, &member);
502#ifdef AAL
503 do_object(src_fd, member.ar_size);
504 lseek(src_fd, 0L, 0);
505 offset += AR_TOTAL + even(member.ar_size);
506#endif
507 while (status.st_size > 0) {
508 int x = IO_SIZE;
509
510 read_chars = x;
511 if (status.st_size < x) {
512 x = status.st_size;
513 read_chars = x;
514 status.st_size = 0;
515 x = even(x);
516 }
517 else status.st_size -= x;
518 if (read(src_fd, io_buffer, read_chars) != read_chars) {
519 error(FALSE,"%s seems to shrink\n", name);
520 break;
521 }
522 mwrite(fd, io_buffer, x);
523 }
524
525 if (verbose)
526 show(mess, member.ar_name);
527 close(src_fd);
528}
529
530extract(member)
531register MEMBER *member;
532{
533 int fd = 1;
534 char buf[sizeof(member->ar_name) + 1];
535
536 strncpy(buf, member->ar_name, sizeof(member->ar_name));
537 buf[sizeof(member->ar_name)] = 0;
538 if (pr_fl == FALSE && (fd = creat(buf, 0666)) < 0) {
539 error(FALSE, "cannot create %s\n", buf);
540 fd = -1;
541 }
542
543 if (verbose) {
544 if (pr_fl == FALSE) show("x - %s\n", buf);
545 else show("\n<%s>\n\n", buf);
546 }
547
548 copy_member(member, ar_fd, fd, 1);
549
550 if (fd >= 0 && fd != 1)
551 close(fd);
552 if (pr_fl == FALSE) chmod(buf, member->ar_mode);
553}
554
555copy_member(member, from, to, extracting)
556register MEMBER *member;
557int from, to;
558{
559 register int rest;
560 long mem_size = member->ar_size;
561 BOOL is_odd = odd(mem_size) ? TRUE : FALSE;
562
563#ifdef AAL
564 if (! extracting) {
565 long pos = lseek(from, 0L, 1);
566
567 do_object(from, mem_size);
568 offset += AR_TOTAL + even(mem_size);
569 lseek(from, pos, 0);
570 }
571#endif
572 do {
573 rest = mem_size > (long) IO_SIZE ? IO_SIZE : (int) mem_size;
574 if (read(from, io_buffer, rest) != rest) {
575 char buf[sizeof(member->ar_name) + 1];
576
577 strncpy(buf, member->ar_name, sizeof(member->ar_name));
578 buf[sizeof(member->ar_name)] = 0;
579 error(TRUE, "read error on %s\n", buf);
580 }
581 if (to >= 0) mwrite(to, io_buffer, rest);
582 mem_size -= (long) rest;
583 } while (mem_size > 0L);
584
585 if (is_odd) {
586 lseek(from, 1L, 1);
587 if (to >= 0 && ! extracting)
588 lseek(to, 1L, 1);
589 }
590}
591
592char *
593get_mode(mode)
594register int mode;
595{
596 static char mode_buf[11];
597 register int tmp = mode;
598 int i;
599
600 mode_buf[9] = ' ';
601 for (i = 0; i < 3; i++) {
602 mode_buf[i * 3] = (tmp & S_IREAD) ? 'r' : '-';
603 mode_buf[i * 3 + 1] = (tmp & S_IWRITE) ? 'w' : '-';
604 mode_buf[i * 3 + 2] = (tmp & S_IEXEC) ? 'x' : '-';
605 tmp <<= 3;
606 }
607 if (mode & S_ISUID)
608 mode_buf[2] = 's';
609 if (mode & S_ISGID)
610 mode_buf[5] = 's';
611 return mode_buf;
612}
613
614wr_fatal()
615{
616 error(TRUE, "write error\n");
617}
618
619rd_fatal()
620{
621 error(TRUE, "read error\n");
622}
623
624mwrite(fd, address, bytes)
625int fd;
626register char *address;
627register int bytes;
628{
629 if (write(fd, address, bytes) != bytes)
630 error(TRUE, "write error\n");
631}
632
633show(s, name)
634char *s, *name;
635{
636 MEMBER x;
637 char buf[sizeof(x.ar_name)+1];
638 register char *p = buf, *q = name;
639
640 while (q <= &name[sizeof(x.ar_name)-1] && *q) *p++ = *q++;
641 *p++ = '\0';
642 print(s, buf);
643}
644
645#ifdef AAL
646/*
647 * Write out the ranlib table: first 4 bytes telling how many ranlib structs
648 * there are, followed by the ranlib structs,
649 * then 4 bytes giving the size of the string table, followed by the string
650 * table itself.
651 */
652write_symdef()
653{
654 register struct ranlib *ran;
655 register int i;
656 register long delta;
657 MEMBER arbuf;
658
659 if (odd(tssiz))
660 tstrtab[tssiz++] = '\0';
661 for (i = 0; i < sizeof(arbuf.ar_name); i++)
662 arbuf.ar_name[i] = '\0';
663 strcpy(arbuf.ar_name, SYMDEF);
664 arbuf.ar_size = 4 + 2 * 4 * (long)tnum + 4 + (long)tssiz;
665 time(&arbuf.ar_date);
666 arbuf.ar_uid = getuid();
667 arbuf.ar_gid = getgid();
668 arbuf.ar_mode = 0444;
669#ifdef DISTRIBUTION
670 if (distr_fl) {
671 arbuf.ar_uid = 2;
672 arbuf.ar_gid = 2;
673 arbuf.ar_date = distr_time;
674 }
675#endif
676 wr_arhdr(ar_fd,&arbuf);
677 wr_long(ar_fd, (long) tnum);
678 /*
679 * Account for the space occupied by the magic number
680 * and the ranlib table.
681 */
682 delta = 2 + AR_TOTAL + arbuf.ar_size;
683 for (ran = tab; ran < &tab[tnum]; ran++) {
684 ran->ran_pos += delta;
685 }
686
687 wr_ranlib(ar_fd, tab, (long) tnum);
688 wr_long(ar_fd, (long) tssiz);
689 wr_bytes(ar_fd, tstrtab, (long) tssiz);
690}
691
692/*
693 * Return whether the bytes in `buf' form a good object header.
694 * The header is put in `headp'.
695 */
696int
697is_outhead(headp)
698 register struct outhead *headp;
699{
700
701 return !BADMAGIC(*headp) && headp->oh_nname != 0;
702}
703
704do_object(f, size)
705 long size;
706{
707 struct outhead headbuf;
708
709 if (size < SZ_HEAD) {
710 /* It can't be an object file. */
711 return;
712 }
713 /*
714 * Read a header to see if it is an object file.
715 */
716 if (! rd_fdopen(f)) {
717 rd_fatal();
718 }
719 rd_ohead(&headbuf);
720 if (!is_outhead(&headbuf)) {
721 return;
722 }
723 do_names(&headbuf);
724}
725
726/*
727 * First skip the names and read in the string table, then seek back to the
728 * name table and read and write the names one by one. Update the ranlib table
729 * accordingly.
730 */
731do_names(headp)
732 struct outhead *headp;
733{
734 register char *strings;
735 register int nnames = headp->oh_nname;
736#define NNAMES 100
737 struct outname namebuf[NNAMES];
738 long xxx = OFF_CHAR(*headp);
739
740 if ( headp->oh_nchar != (unsigned int)headp->oh_nchar ||
741 (strings = malloc((unsigned int)headp->oh_nchar)) == (char *)0
742 ) {
743 error(TRUE, "string table too big\n");
744 }
745 rd_string(strings, headp->oh_nchar);
746 while (nnames) {
747 int i = nnames >= NNAMES ? NNAMES : nnames;
748 register struct outname *p = namebuf;
749
750 nnames -= i;
751 rd_name(namebuf, i);
752 while (i--) {
753 long off = p->on_foff - xxx;
754 if (p->on_foff == (long)0) {
755 p++;
756 continue; /* An unrecognizable name. */
757 }
758 p->on_mptr = strings + off;
759 /*
760 * Only enter names that are exported and are really
761 * defined. Also enter common names. Note, that
762 * this might cause problems when the name is really
763 * defined in a later file, with a value != 0.
764 * However, this problem also exists on the Unix
765 * ranlib archives.
766 */
767 if ( (p->on_type & S_EXT) &&
768 (p->on_type & S_TYP) != S_UND
769 )
770 enter_name(p);
771 p++;
772 }
773 }
774 free(strings);
775}
776
777enter_name(namep)
778 struct outname *namep;
779{
780 register char *cp;
781
782 if (tnum >= tabsz) {
783 tab = (struct ranlib *)
784 realloc((char *) tab, (tabsz += 512) * sizeof(struct ranlib));
785 if (! tab) error(TRUE, "Out of core\n");
786 }
787 tab[tnum].ran_off = tssiz;
788 tab[tnum].ran_pos = offset;
789
790 for (cp = namep->on_mptr;; cp++) {
791 if (tssiz >= strtabsz) {
792 tstrtab = realloc(tstrtab, (strtabsz += 4096));
793 if (! tstrtab) error(TRUE, "string table overflow\n");
794 }
795 tstrtab[tssiz++] = *cp;
796 if (!*cp) break;
797 }
798 tnum++;
799}
800#endif AAL
Note: See TracBrowser for help on using the repository browser.