vm: include no-caching bits in PTF_ALLFLAGS for flags sanity check.
[minix.git] / commands / aal / archiver.c
blob797f5ce11269871f29806adfe46191ee8d2257d5
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 */
9 static char RcsId[] = "$Header$";
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
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
45 long offset;
46 struct ranlib *tab;
47 unsigned int tnum = 0;
48 char *tstrtab;
49 unsigned int tssiz = 0;
50 char *malloc(), *realloc(), *strcpy(), *strncpy();
51 long time();
52 unsigned int tabsz, strtabsz;
53 #else
54 #define MAGIC_NUMBER ARMAG
55 #endif
56 long lseek();
58 #define odd(nr) (nr & 01)
59 #define even(nr) (odd(nr) ? nr + 1 : nr)
61 typedef char BOOL;
62 #define FALSE 0
63 #define TRUE 1
65 #define READ 0
66 #define APPEND 2
67 #define CREATE 1
69 #define MEMBER struct ar_hdr
71 #define NIL_PTR ((char *) 0)
72 #define NIL_MEM ((MEMBER *) 0)
73 #define NIL_LONG ((long *) 0)
75 #define IO_SIZE (10 * 1024)
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
82 BOOL verbose;
83 BOOL app_fl;
84 BOOL ex_fl;
85 BOOL show_fl;
86 BOOL pr_fl;
87 BOOL rep_fl;
88 BOOL del_fl;
89 BOOL nocr_fl;
90 BOOL local_fl;
91 BOOL update_fl;
92 #ifdef DISTRIBUTION
93 BOOL distr_fl;
94 long distr_time;
95 #endif
97 int ar_fd;
99 char io_buffer[IO_SIZE];
101 char *progname;
103 char temp_buf[32];
104 char *temp_arch = &temp_buf[0];
105 extern char *mktemp();
106 extern char *ctime();
108 usage()
110 error(TRUE, "usage: %s %s archive [file] ...\n",
111 progname,
112 #ifdef AAL
113 "[acdrtxvlu]"
114 #else
115 "[acdprtxvlu]"
116 #endif
120 /*VARARGS2*/
121 error(quit, str1, str2, str3, str4)
122 BOOL quit;
123 char *str1, *str2, *str3, *str4;
125 char errbuf[256];
127 sprint(errbuf, str1, str2, str3, str4);
128 write(2, errbuf, strlen(errbuf));
129 if (quit) {
130 unlink(temp_arch);
131 _exit(1);
135 char *basename(path)
136 char *path;
138 register char *ptr = path;
139 register char *last = NIL_PTR;
141 while (*ptr != '\0') {
142 if (*ptr == '/')
143 last = ptr;
144 ptr++;
146 if (last == NIL_PTR)
147 return path;
148 if (*(last + 1) == '\0') {
149 *last = '\0';
150 return basename(path);
152 return last + 1;
155 extern unsigned int rd_unsigned2();
157 open_archive(name, mode)
158 register char *name;
159 register int mode;
161 unsigned short magic = 0;
162 int fd;
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;
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);
178 error(TRUE, "cannot open %s\n", name);
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);
185 return fd;
188 #if __STDC__
189 void catch(int sig)
190 #else
191 catch()
192 #endif
194 unlink(temp_arch);
195 _exit (2);
198 main(argc, argv)
199 int argc;
200 char *argv[];
202 register char *ptr;
203 int needs_arg = 0;
205 progname = argv[0];
207 if (argc < 3)
208 usage();
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();
258 if (needs_arg && argc <= 3)
259 usage();
260 #ifdef DISTRIBUTION
261 if (distr_fl) {
262 struct stat statbuf;
264 stat(progname, &statbuf);
265 distr_time = statbuf.st_mtime;
267 #endif
268 if (local_fl) strcpy(temp_arch, "ar.XXXXXX");
269 else strcpy(temp_arch, "/usr/tmp/ar.XXXXXX");
271 if (app_fl + ex_fl + del_fl + rep_fl + show_fl + pr_fl != 1)
272 usage();
274 if (update_fl && !rep_fl)
275 usage();
277 if (rep_fl || del_fl
278 #ifdef AAL
279 || app_fl
280 #endif
282 mktemp(temp_arch);
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
292 signal(SIGINT, catch);
293 get(argc, argv);
295 return 0;
298 MEMBER *
299 get_member()
301 static MEMBER member;
303 again:
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");
309 #ifdef AAL
310 if (equal(SYMDEF, member.ar_name)) {
311 lseek(ar_fd, member.ar_size, 1);
312 goto again;
314 #endif
315 return &member;
318 char *get_mode();
320 get(argc, argv)
321 int argc;
322 register char *argv[];
324 register MEMBER *member;
325 int i = 0;
326 int temp_fd, read_chars;
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
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;
341 if (i == argc || app_fl) {
342 if (rep_fl || del_fl
343 #ifdef AAL
344 || app_fl
345 #endif
347 #ifdef AAL
348 if (i != argc) {
349 print("%s: already in archive\n", argv[i]);
350 argv[i] = "";
352 #endif
353 wr_arhdr(temp_fd, member);
354 copy_member(member, ar_fd, temp_fd, 0);
356 else {
357 #ifndef AAL
358 if (app_fl && i != argc) {
359 print("%s: already in archive\n", argv[i]);
360 argv[i] = "";
362 #endif
363 lseek(ar_fd, even(member->ar_size),1);
365 continue;
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;
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);
389 else if (show_fl) {
390 char buf[sizeof(member->ar_name) + 2];
391 register char *p = buf, *q = member->ar_name;
393 while (q <= &member->ar_name[sizeof(member->ar_name)-1] && *q) {
394 *p++ = *q++;
396 *p++ = '\n';
397 *p = '\0';
398 if (verbose) {
399 char *mode = get_mode(member->ar_mode);
400 char *date = ctime(&(member->ar_date));
402 *(date + 16) = '\0';
403 *(date + 24) = '\0';
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);
414 else print(buf);
416 else if (del_fl)
417 show("d - %s\n", member->ar_name);
418 lseek(ar_fd, even(member->ar_size), 1);
420 argv[i] = "";
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
436 add(argv[i], temp_fd, "a - %s\n");
437 else {
438 print("%s: not found\n", argv[i]);
443 if (rep_fl || del_fl
444 #ifdef AAL
445 || app_fl
446 #endif
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);
461 close(ar_fd);
464 add(name, fd, mess)
465 char *name;
466 int fd;
467 char *mess;
469 static MEMBER member;
470 register int read_chars;
471 struct stat status;
472 int src_fd;
474 if (stat(name, &status) < 0) {
475 error(FALSE, "cannot find %s\n", name);
476 return;
478 else if (S_ISDIR(status.st_mode)) {
479 error(FALSE, "%s is a directory (ignored)\n", name);
480 return;
482 else if ((src_fd = open(name, 0)) < 0) {
483 error(FALSE, "cannot open %s\n", name);
484 return;
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;
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;
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);
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;
522 mwrite(fd, io_buffer, x);
525 if (verbose)
526 show(mess, member.ar_name);
527 close(src_fd);
530 extract(member)
531 register MEMBER *member;
533 int fd = 1;
534 char buf[sizeof(member->ar_name) + 1];
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;
543 if (verbose) {
544 if (pr_fl == FALSE) show("x - %s\n", buf);
545 else show("\n<%s>\n\n", buf);
548 copy_member(member, ar_fd, fd, 1);
550 if (fd >= 0 && fd != 1)
551 close(fd);
552 if (pr_fl == FALSE) chmod(buf, member->ar_mode);
555 copy_member(member, from, to, extracting)
556 register MEMBER *member;
557 int from, to;
559 register int rest;
560 long mem_size = member->ar_size;
561 BOOL is_odd = odd(mem_size) ? TRUE : FALSE;
563 #ifdef AAL
564 if (! extracting) {
565 long pos = lseek(from, 0L, 1);
567 do_object(from, mem_size);
568 offset += AR_TOTAL + even(mem_size);
569 lseek(from, pos, 0);
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];
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);
581 if (to >= 0) mwrite(to, io_buffer, rest);
582 mem_size -= (long) rest;
583 } while (mem_size > 0L);
585 if (is_odd) {
586 lseek(from, 1L, 1);
587 if (to >= 0 && ! extracting)
588 lseek(to, 1L, 1);
592 char *
593 get_mode(mode)
594 register int mode;
596 static char mode_buf[11];
597 register int tmp = mode;
598 int i;
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;
607 if (mode & S_ISUID)
608 mode_buf[2] = 's';
609 if (mode & S_ISGID)
610 mode_buf[5] = 's';
611 return mode_buf;
614 wr_fatal()
616 error(TRUE, "write error\n");
619 rd_fatal()
621 error(TRUE, "read error\n");
624 mwrite(fd, address, bytes)
625 int fd;
626 register char *address;
627 register int bytes;
629 if (write(fd, address, bytes) != bytes)
630 error(TRUE, "write error\n");
633 show(s, name)
634 char *s, *name;
636 MEMBER x;
637 char buf[sizeof(x.ar_name)+1];
638 register char *p = buf, *q = name;
640 while (q <= &name[sizeof(x.ar_name)-1] && *q) *p++ = *q++;
641 *p++ = '\0';
642 print(s, buf);
645 #ifdef AAL
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.
652 write_symdef()
654 register struct ranlib *ran;
655 register int i;
656 register long delta;
657 MEMBER arbuf;
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;
675 #endif
676 wr_arhdr(ar_fd,&arbuf);
677 wr_long(ar_fd, (long) tnum);
679 * Account for the space occupied by the magic number
680 * and the ranlib table.
682 delta = 2 + AR_TOTAL + arbuf.ar_size;
683 for (ran = tab; ran < &tab[tnum]; ran++) {
684 ran->ran_pos += delta;
687 wr_ranlib(ar_fd, tab, (long) tnum);
688 wr_long(ar_fd, (long) tssiz);
689 wr_bytes(ar_fd, tstrtab, (long) tssiz);
693 * Return whether the bytes in `buf' form a good object header.
694 * The header is put in `headp'.
697 is_outhead(headp)
698 register struct outhead *headp;
701 return !BADMAGIC(*headp) && headp->oh_nname != 0;
704 do_object(f, size)
705 long size;
707 struct outhead headbuf;
709 if (size < SZ_HEAD) {
710 /* It can't be an object file. */
711 return;
714 * Read a header to see if it is an object file.
716 if (! rd_fdopen(f)) {
717 rd_fatal();
719 rd_ohead(&headbuf);
720 if (!is_outhead(&headbuf)) {
721 return;
723 do_names(&headbuf);
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.
731 do_names(headp)
732 struct outhead *headp;
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);
740 if ( headp->oh_nchar != (unsigned int)headp->oh_nchar ||
741 (strings = malloc((unsigned int)headp->oh_nchar)) == (char *)0
743 error(TRUE, "string table too big\n");
745 rd_string(strings, headp->oh_nchar);
746 while (nnames) {
747 int i = nnames >= NNAMES ? NNAMES : nnames;
748 register struct outname *p = namebuf;
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. */
758 p->on_mptr = strings + off;
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.
767 if ( (p->on_type & S_EXT) &&
768 (p->on_type & S_TYP) != S_UND
770 enter_name(p);
771 p++;
774 free(strings);
777 enter_name(namep)
778 struct outname *namep;
780 register char *cp;
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");
787 tab[tnum].ran_off = tssiz;
788 tab[tnum].ran_pos = offset;
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");
795 tstrtab[tssiz++] = *cp;
796 if (!*cp) break;
798 tnum++;
800 #endif AAL