Regenerated for 3.74.3
[make.git] / arscan.c
blobe63659e5e649988f8e4b4d90121a978e5e88516e
1 /* Library function for scanning an archive file.
2 Copyright (C) 1987, 89, 91, 92, 93, 94, 95 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include "make.h"
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #else
23 #include <sys/file.h>
24 #endif
26 #ifndef NO_ARCHIVES
28 /* SCO Unix's compiler defines both of these. */
29 #ifdef M_UNIX
30 #undef M_XENIX
31 #endif
33 /* On the sun386i and in System V rel 3, ar.h defines two different archive
34 formats depending upon whether you have defined PORTAR (normal) or PORT5AR
35 (System V Release 1). There is no default, one or the other must be defined
36 to have a nonzero value. */
38 #if (!defined (PORTAR) || PORTAR == 0) && (!defined (PORT5AR) || PORT5AR == 0)
39 #undef PORTAR
40 #ifdef M_XENIX
41 /* According to Jim Sievert <jas1@rsvl.unisys.com>, for SCO XENIX defining
42 PORTAR to 1 gets the wrong archive format, and defining it to 0 gets the
43 right one. */
44 #define PORTAR 0
45 #else
46 #define PORTAR 1
47 #endif
48 #endif
50 #include <ar.h>
52 /* Cray's <ar.h> apparently defines this. */
53 #ifndef AR_HDR_SIZE
54 #define AR_HDR_SIZE (sizeof (struct ar_hdr))
55 #endif
57 /* Takes three arguments ARCHIVE, FUNCTION and ARG.
59 Open the archive named ARCHIVE, find its members one by one,
60 and for each one call FUNCTION with the following arguments:
61 archive file descriptor for reading the data,
62 member name,
63 member name might be truncated flag,
64 member header position in file,
65 member data position in file,
66 member data size,
67 member date,
68 member uid,
69 member gid,
70 member protection mode,
71 ARG.
73 The descriptor is poised to read the data of the member
74 when FUNCTION is called. It does not matter how much
75 data FUNCTION reads.
77 If FUNCTION returns nonzero, we immediately return
78 what FUNCTION returned.
80 Returns -1 if archive does not exist,
81 Returns -2 if archive has invalid format.
82 Returns 0 if have scanned successfully. */
84 long int
85 ar_scan (archive, function, arg)
86 char *archive;
87 long int (*function) ();
88 long int arg;
90 #ifdef AIAMAG
91 FL_HDR fl_header;
92 #else
93 int long_name = 0;
94 #endif
95 char *namemap = 0;
96 register int desc = open (archive, O_RDONLY, 0);
97 if (desc < 0)
98 return -1;
99 #ifdef SARMAG
101 char buf[SARMAG];
102 register int nread = read (desc, buf, SARMAG);
103 if (nread != SARMAG || bcmp (buf, ARMAG, SARMAG))
105 (void) close (desc);
106 return -2;
109 #else
110 #ifdef AIAMAG
112 register int nread = read (desc, (char *) &fl_header, FL_HSZ);
113 if (nread != FL_HSZ || bcmp (fl_header.fl_magic, AIAMAG, SAIAMAG))
115 (void) close (desc);
116 return -2;
119 #else
121 #ifndef M_XENIX
122 int buf;
123 #else
124 unsigned short int buf;
125 #endif
126 register int nread = read(desc, &buf, sizeof (buf));
127 if (nread != sizeof (buf) || buf != ARMAG)
129 (void) close (desc);
130 return -2;
133 #endif
134 #endif
136 /* Now find the members one by one. */
138 #ifdef SARMAG
139 register long int member_offset = SARMAG;
140 #else
141 #ifdef AIAMAG
142 long int member_offset;
143 long int last_member_offset;
145 sscanf (fl_header.fl_fstmoff, "%12ld", &member_offset);
146 sscanf (fl_header.fl_lstmoff, "%12ld", &last_member_offset);
148 if (member_offset == 0)
150 /* Empty archive. */
151 close (desc);
152 return 0;
154 #else
155 #ifndef M_XENIX
156 register long int member_offset = sizeof (int);
157 #else /* Xenix. */
158 register long int member_offset = sizeof (unsigned short int);
159 #endif /* Not Xenix. */
160 #endif
161 #endif
163 while (1)
165 register int nread;
166 struct ar_hdr member_header;
167 #ifdef AIAMAG
168 char name[256];
169 int name_len;
170 long int dateval;
171 int uidval, gidval;
172 long int data_offset;
173 #else
174 char namebuf[sizeof member_header.ar_name + 1];
175 char *name;
176 int is_namemap; /* Nonzero if this entry maps long names. */
177 #endif
178 long int eltsize;
179 int eltmode;
180 long int fnval;
182 if (lseek (desc, member_offset, 0) < 0)
184 (void) close (desc);
185 return -2;
188 #ifdef AIAMAG
189 #define AR_MEMHDR (AR_HDR_SIZE - sizeof (member_header._ar_name))
190 nread = read (desc, (char *) &member_header, AR_MEMHDR);
192 if (nread != AR_MEMHDR)
194 (void) close (desc);
195 return -2;
198 sscanf (member_header.ar_namlen, "%4d", &name_len);
199 nread = read (desc, name, name_len);
201 if (nread != name_len)
203 (void) close (desc);
204 return -2;
207 name[name_len] = 0;
209 sscanf (member_header.ar_date, "%12ld", &dateval);
210 sscanf (member_header.ar_uid, "%12d", &uidval);
211 sscanf (member_header.ar_gid, "%12d", &gidval);
212 sscanf (member_header.ar_mode, "%12o", &eltmode);
213 sscanf (member_header.ar_size, "%12ld", &eltsize);
215 if ((data_offset = member_offset + AR_MEMHDR + name_len + 2) % 2)
216 ++data_offset;
218 fnval =
219 (*function) (desc, name, 0,
220 member_offset, data_offset, eltsize,
221 dateval, uidval, gidval,
222 eltmode, arg);
224 #else /* Not AIAMAG. */
225 nread = read (desc, (char *) &member_header, AR_HDR_SIZE);
226 if (nread == 0)
227 /* No data left means end of file; that is OK. */
228 break;
230 if (nread != AR_HDR_SIZE
231 #ifdef ARFMAG
232 || bcmp (member_header.ar_fmag, ARFMAG, 2)
233 #endif
236 (void) close (desc);
237 return -2;
240 name = namebuf;
241 bcopy (member_header.ar_name, name, sizeof member_header.ar_name);
243 register char *p = name + sizeof member_header.ar_name;
245 *p = '\0';
246 while (p > name && *--p == ' ');
248 #ifndef AIAMAG
249 /* If the member name is "//" or "ARFILENAMES/" this may be
250 a list of file name mappings. The maximum file name
251 length supported by the standard archive format is 14
252 characters. This member will actually always be the
253 first or second entry in the archive, but we don't check
254 that. */
255 is_namemap = (!strcmp (name, "//")
256 || !strcmp (name, "ARFILENAMES/"));
257 #endif /* Not AIAMAG. */
258 /* On some systems, there is a slash after each member name. */
259 if (*p == '/')
260 *p = '\0';
262 #ifndef AIAMAG
263 /* If the member name starts with a space or a slash, this
264 is an index into the file name mappings (used by GNU ar).
265 Otherwise if the member name looks like #1/NUMBER the
266 real member name appears in the element data (used by
267 4.4BSD). */
268 if (! is_namemap
269 && (name[0] == ' ' || name[0] == '/')
270 && namemap != 0)
272 name = namemap + atoi (name + 1);
273 long_name = 1;
275 else if (name[0] == '#'
276 && name[1] == '1'
277 && name[2] == '/')
279 int namesize = atoi (name + 3);
281 name = (char *) alloca (namesize + 1);
282 nread = read (desc, name, namesize);
283 if (nread != namesize)
285 close (desc);
286 return -2;
288 name[namesize] = '\0';
290 long_name = 1;
292 #endif /* Not AIAMAG. */
295 #ifndef M_XENIX
296 sscanf (member_header.ar_mode, "%o", &eltmode);
297 eltsize = atol (member_header.ar_size);
298 #else /* Xenix. */
299 eltmode = (unsigned short int) member_header.ar_mode;
300 eltsize = member_header.ar_size;
301 #endif /* Not Xenix. */
303 fnval =
304 (*function) (desc, name, ! long_name, member_offset,
305 member_offset + AR_HDR_SIZE, eltsize,
306 #ifndef M_XENIX
307 atol (member_header.ar_date),
308 atoi (member_header.ar_uid),
309 atoi (member_header.ar_gid),
310 #else /* Xenix. */
311 member_header.ar_date,
312 member_header.ar_uid,
313 member_header.ar_gid,
314 #endif /* Not Xenix. */
315 eltmode, arg);
317 #endif /* AIAMAG. */
319 if (fnval)
321 (void) close (desc);
322 return fnval;
325 #ifdef AIAMAG
326 if (member_offset == last_member_offset)
327 /* End of the chain. */
328 break;
330 sscanf (member_header.ar_nxtmem, "%12ld", &member_offset);
332 if (lseek (desc, member_offset, 0) != member_offset)
334 (void) close (desc);
335 return -2;
337 #else
339 /* If this member maps archive names, we must read it in. The
340 name map will always precede any members whose names must
341 be mapped. */
342 if (is_namemap)
344 char *clear;
345 char *limit;
347 namemap = (char *) alloca (eltsize);
348 nread = read (desc, namemap, eltsize);
349 if (nread != eltsize)
351 (void) close (desc);
352 return -2;
355 /* The names are separated by newlines. Some formats have
356 a trailing slash. Null terminate the strings for
357 convenience. */
358 limit = namemap + eltsize;
359 for (clear = namemap; clear < limit; clear++)
361 if (*clear == '\n')
363 *clear = '\0';
364 if (clear[-1] == '/')
365 clear[-1] = '\0';
369 is_namemap = 0;
372 member_offset += AR_HDR_SIZE + eltsize;
373 if (member_offset % 2 != 0)
374 member_offset++;
375 #endif
379 close (desc);
380 return 0;
383 /* Return nonzero iff NAME matches MEM.
384 If TRUNCATED is nonzero, MEM may be truncated to
385 sizeof (struct ar_hdr.ar_name) - 1. */
388 ar_name_equal (name, mem, truncated)
389 char *name, *mem;
390 int truncated;
392 char *p;
394 p = rindex (name, '/');
395 if (p != 0)
396 name = p + 1;
398 /* We no longer use this kludge, since we
399 now support long archive member names. */
401 #if 0 && !defined (AIAMAG) && !defined (APOLLO)
404 /* `reallylongname.o' matches `reallylongnam.o'.
405 If member names have a trailing slash, that's `reallylongna.o'. */
407 struct ar_hdr h;
408 unsigned int max = sizeof (h.ar_name);
409 unsigned int namelen, memlen;
411 if (strncmp (name, mem, max - 3))
412 return 0;
414 namelen = strlen (name);
415 memlen = strlen (mem);
417 if (namelen > memlen && memlen >= max - 1
418 && name[namelen - 2] == '.' && name[namelen - 1] == 'o'
419 && mem[memlen - 2] == '.' && mem[memlen - 1] == 'o')
420 return 1;
422 if (namelen != memlen)
423 return 0;
425 return (namelen < max - 3 || !strcmp (name + max - 3, mem + max - 3));
428 #else /* AIX or APOLLO. */
430 if (truncated)
432 #ifdef AIAMAG
433 /* TRUNCATED should never be set on this system. */
434 abort ();
435 #else
436 struct ar_hdr hdr;
437 return !strncmp (name, mem,
438 sizeof (hdr.ar_name) -
439 #if !defined (__hpux) && !defined (cray)
441 #else
443 #endif /* !__hpux && !cray */
445 #endif
448 return !strcmp (name, mem);
450 #endif
453 /* ARGSUSED */
454 static long int
455 ar_member_pos (desc, mem, truncated,
456 hdrpos, datapos, size, date, uid, gid, mode, name)
457 int desc;
458 char *mem;
459 int truncated;
460 long int hdrpos, datapos, size, date;
461 int uid, gid, mode;
462 char *name;
464 if (!ar_name_equal (name, mem, truncated))
465 return 0;
466 return hdrpos;
469 /* Set date of member MEMNAME in archive ARNAME to current time.
470 Returns 0 if successful,
471 -1 if file ARNAME does not exist,
472 -2 if not a valid archive,
473 -3 if other random system call error (including file read-only),
474 1 if valid but member MEMNAME does not exist. */
477 ar_member_touch (arname, memname)
478 char *arname, *memname;
480 register long int pos = ar_scan (arname, ar_member_pos, (long int) memname);
481 register int fd;
482 struct ar_hdr ar_hdr;
483 register int i;
484 struct stat statbuf;
486 if (pos < 0)
487 return (int) pos;
488 if (!pos)
489 return 1;
491 fd = open (arname, O_RDWR, 0666);
492 if (fd < 0)
493 return -3;
494 /* Read in this member's header */
495 if (lseek (fd, pos, 0) < 0)
496 goto lose;
497 if (AR_HDR_SIZE != read (fd, (char *) &ar_hdr, AR_HDR_SIZE))
498 goto lose;
499 /* Write back the header, thus touching the archive file. */
500 if (lseek (fd, pos, 0) < 0)
501 goto lose;
502 if (AR_HDR_SIZE != write (fd, (char *) &ar_hdr, AR_HDR_SIZE))
503 goto lose;
504 /* The file's mtime is the time we we want. */
505 #ifdef EINTR
506 while (fstat (fd, &statbuf) < 0 && errno == EINTR);
507 #else
508 fstat (fd, &statbuf);
509 #endif
510 #if defined(ARFMAG) || defined(AIAMAG)
511 /* Advance member's time to that time */
512 for (i = 0; i < sizeof ar_hdr.ar_date; i++)
513 ar_hdr.ar_date[i] = ' ';
514 sprintf (ar_hdr.ar_date, "%ld", (long int) statbuf.st_mtime);
515 #ifdef AIAMAG
516 ar_hdr.ar_date[strlen(ar_hdr.ar_date)] = ' ';
517 #endif
518 #else
519 ar_hdr.ar_date = statbuf.st_mtime;
520 #endif
521 /* Write back this member's header */
522 if (lseek (fd, pos, 0) < 0)
523 goto lose;
524 if (AR_HDR_SIZE != write (fd, (char *) &ar_hdr, AR_HDR_SIZE))
525 goto lose;
526 close (fd);
527 return 0;
529 lose:
530 i = errno;
531 close (fd);
532 errno = i;
533 return -3;
536 #ifdef TEST
538 long int
539 describe_member (desc, name, truncated,
540 hdrpos, datapos, size, date, uid, gid, mode)
541 int desc;
542 char *name;
543 int truncated;
544 long int hdrpos, datapos, size, date;
545 int uid, gid, mode;
547 extern char *ctime ();
549 printf ("Member `%s'%s: %ld bytes at %ld (%ld).\n",
550 name, truncated ? " (name might be truncated)" : "",
551 size, hdrpos, datapos);
552 printf (" Date %s", ctime (&date));
553 printf (" uid = %d, gid = %d, mode = 0%o.\n", uid, gid, mode);
555 return 0;
558 main (argc, argv)
559 int argc;
560 char **argv;
562 ar_scan (argv[1], describe_member);
563 return 0;
566 #endif /* TEST. */
568 #endif /* NO_ARCHIVES. */