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)
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. */
28 /* SCO Unix's compiler defines both of these. */
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)
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
52 /* Cray's <ar.h> apparently defines this. */
54 #define AR_HDR_SIZE (sizeof (struct ar_hdr))
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,
63 member name might be truncated flag,
64 member header position in file,
65 member data position in file,
70 member protection mode,
73 The descriptor is poised to read the data of the member
74 when FUNCTION is called. It does not matter how much
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. */
85 ar_scan (archive
, function
, arg
)
87 long int (*function
) ();
96 register int desc
= open (archive
, O_RDONLY
, 0);
102 register int nread
= read (desc
, buf
, SARMAG
);
103 if (nread
!= SARMAG
|| bcmp (buf
, ARMAG
, SARMAG
))
112 register int nread
= read (desc
, (char *) &fl_header
, FL_HSZ
);
113 if (nread
!= FL_HSZ
|| bcmp (fl_header
.fl_magic
, AIAMAG
, SAIAMAG
))
124 unsigned short int buf
;
126 register int nread
= read(desc
, &buf
, sizeof (buf
));
127 if (nread
!= sizeof (buf
) || buf
!= ARMAG
)
136 /* Now find the members one by one. */
139 register long int member_offset
= SARMAG
;
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)
156 register long int member_offset
= sizeof (int);
158 register long int member_offset
= sizeof (unsigned short int);
159 #endif /* Not Xenix. */
166 struct ar_hdr member_header
;
172 long int data_offset
;
174 char namebuf
[sizeof member_header
.ar_name
+ 1];
176 int is_namemap
; /* Nonzero if this entry maps long names. */
182 if (lseek (desc
, member_offset
, 0) < 0)
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
)
198 sscanf (member_header
.ar_namlen
, "%4d", &name_len
);
199 nread
= read (desc
, name
, name_len
);
201 if (nread
!= name_len
)
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)
219 (*function
) (desc
, name
, 0,
220 member_offset
, data_offset
, eltsize
,
221 dateval
, uidval
, gidval
,
224 #else /* Not AIAMAG. */
225 nread
= read (desc
, (char *) &member_header
, AR_HDR_SIZE
);
227 /* No data left means end of file; that is OK. */
230 if (nread
!= AR_HDR_SIZE
232 || bcmp (member_header
.ar_fmag
, ARFMAG
, 2)
241 bcopy (member_header
.ar_name
, name
, sizeof member_header
.ar_name
);
243 register char *p
= name
+ sizeof member_header
.ar_name
;
246 while (p
> name
&& *--p
== ' ');
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
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. */
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
269 && (name
[0] == ' ' || name
[0] == '/')
272 name
= namemap
+ atoi (name
+ 1);
275 else if (name
[0] == '#'
279 int namesize
= atoi (name
+ 3);
281 name
= (char *) alloca (namesize
+ 1);
282 nread
= read (desc
, name
, namesize
);
283 if (nread
!= namesize
)
288 name
[namesize
] = '\0';
292 #endif /* Not AIAMAG. */
296 sscanf (member_header
.ar_mode
, "%o", &eltmode
);
297 eltsize
= atol (member_header
.ar_size
);
299 eltmode
= (unsigned short int) member_header
.ar_mode
;
300 eltsize
= member_header
.ar_size
;
301 #endif /* Not Xenix. */
304 (*function
) (desc
, name
, ! long_name
, member_offset
,
305 member_offset
+ AR_HDR_SIZE
, eltsize
,
307 atol (member_header
.ar_date
),
308 atoi (member_header
.ar_uid
),
309 atoi (member_header
.ar_gid
),
311 member_header
.ar_date
,
312 member_header
.ar_uid
,
313 member_header
.ar_gid
,
314 #endif /* Not Xenix. */
326 if (member_offset
== last_member_offset
)
327 /* End of the chain. */
330 sscanf (member_header
.ar_nxtmem
, "%12ld", &member_offset
);
332 if (lseek (desc
, member_offset
, 0) != member_offset
)
339 /* If this member maps archive names, we must read it in. The
340 name map will always precede any members whose names must
347 namemap
= (char *) alloca (eltsize
);
348 nread
= read (desc
, namemap
, eltsize
);
349 if (nread
!= eltsize
)
355 /* The names are separated by newlines. Some formats have
356 a trailing slash. Null terminate the strings for
358 limit
= namemap
+ eltsize
;
359 for (clear
= namemap
; clear
< limit
; clear
++)
364 if (clear
[-1] == '/')
372 member_offset
+= AR_HDR_SIZE
+ eltsize
;
373 if (member_offset
% 2 != 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
)
394 p
= rindex (name
, '/');
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'. */
408 unsigned int max
= sizeof (h
.ar_name
);
409 unsigned int namelen
, memlen
;
411 if (strncmp (name
, mem
, max
- 3))
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')
422 if (namelen
!= memlen
)
425 return (namelen
< max
- 3 || !strcmp (name
+ max
- 3, mem
+ max
- 3));
428 #else /* AIX or APOLLO. */
433 /* TRUNCATED should never be set on this system. */
437 return !strncmp (name
, mem
,
438 sizeof (hdr
.ar_name
) -
439 #if !defined (__hpux) && !defined (cray)
443 #endif /* !__hpux && !cray */
448 return !strcmp (name
, mem
);
455 ar_member_pos (desc
, mem
, truncated
,
456 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
, name
)
460 long int hdrpos
, datapos
, size
, date
;
464 if (!ar_name_equal (name
, mem
, truncated
))
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
);
482 struct ar_hdr ar_hdr
;
491 fd
= open (arname
, O_RDWR
, 0666);
494 /* Read in this member's header */
495 if (lseek (fd
, pos
, 0) < 0)
497 if (AR_HDR_SIZE
!= read (fd
, (char *) &ar_hdr
, AR_HDR_SIZE
))
499 /* Write back the header, thus touching the archive file. */
500 if (lseek (fd
, pos
, 0) < 0)
502 if (AR_HDR_SIZE
!= write (fd
, (char *) &ar_hdr
, AR_HDR_SIZE
))
504 /* The file's mtime is the time we we want. */
506 while (fstat (fd
, &statbuf
) < 0 && errno
== EINTR
);
508 fstat (fd
, &statbuf
);
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
);
516 ar_hdr
.ar_date
[strlen(ar_hdr
.ar_date
)] = ' ';
519 ar_hdr
.ar_date
= statbuf
.st_mtime
;
521 /* Write back this member's header */
522 if (lseek (fd
, pos
, 0) < 0)
524 if (AR_HDR_SIZE
!= write (fd
, (char *) &ar_hdr
, AR_HDR_SIZE
))
539 describe_member (desc
, name
, truncated
,
540 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
)
544 long int hdrpos
, datapos
, size
, date
;
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
);
562 ar_scan (argv
[1], describe_member
);
568 #endif /* NO_ARCHIVES. */