1 /* Library function for scanning an archive file.
2 Copyright (C) 1987, 89, 91, 92, 93, 94 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
);
149 register long int member_offset
= sizeof (int);
151 register long int member_offset
= sizeof (unsigned short int);
152 #endif /* Not Xenix. */
159 struct ar_hdr member_header
;
165 long int data_offset
;
167 char namebuf
[sizeof member_header
.ar_name
+ 1];
169 int is_namemap
; /* Nonzero if this entry maps long names. */
175 if (lseek (desc
, member_offset
, 0) < 0)
182 #define AR_MEMHDR (AR_HDR_SIZE - sizeof (member_header._ar_name))
183 nread
= read (desc
, (char *) &member_header
, AR_MEMHDR
);
185 if (nread
!= AR_MEMHDR
)
191 sscanf (member_header
.ar_namlen
, "%4d", &name_len
);
192 nread
= read (desc
, name
, name_len
);
194 if (nread
!= name_len
)
202 sscanf (member_header
.ar_date
, "%12ld", &dateval
);
203 sscanf (member_header
.ar_uid
, "%12d", &uidval
);
204 sscanf (member_header
.ar_gid
, "%12d", &gidval
);
205 sscanf (member_header
.ar_mode
, "%12o", &eltmode
);
206 sscanf (member_header
.ar_size
, "%12ld", &eltsize
);
208 if ((data_offset
= member_offset
+ AR_MEMHDR
+ name_len
+ 2) % 2)
212 (*function
) (desc
, name
, 0,
213 member_offset
, data_offset
, eltsize
,
214 dateval
, uidval
, gidval
,
217 #else /* Not AIAMAG. */
218 nread
= read (desc
, (char *) &member_header
, AR_HDR_SIZE
);
220 /* No data left means end of file; that is OK. */
223 if (nread
!= AR_HDR_SIZE
225 || bcmp (member_header
.ar_fmag
, ARFMAG
, 2)
234 bcopy (member_header
.ar_name
, name
, sizeof member_header
.ar_name
);
236 register char *p
= name
+ sizeof member_header
.ar_name
;
239 while (p
> name
&& *--p
== ' ');
242 /* If the member name is "//" or "ARFILENAMES/" this may be
243 a list of file name mappings. The maximum file name
244 length supported by the standard archive format is 14
245 characters. This member will actually always be the
246 first or second entry in the archive, but we don't check
248 is_namemap
= (!strcmp (name
, "//")
249 || !strcmp (name
, "ARFILENAMES/"));
250 #endif /* Not AIAMAG. */
251 /* On some systems, there is a slash after each member name. */
256 /* If the member name starts with a space or a slash, this
257 is an index into the file name mappings (used by GNU ar).
258 Otherwise if the member name looks like #1/NUMBER the
259 real member name appears in the element data (used by
262 && (name
[0] == ' ' || name
[0] == '/')
265 name
= namemap
+ atoi (name
+ 1);
268 else if (name
[0] == '#'
272 int namesize
= atoi (name
+ 3);
274 name
= (char *) alloca (namesize
+ 1);
275 nread
= read (desc
, name
, namesize
);
276 if (nread
!= namesize
)
281 name
[namesize
] = '\0';
285 #endif /* Not AIAMAG. */
289 sscanf (member_header
.ar_mode
, "%o", &eltmode
);
290 eltsize
= atol (member_header
.ar_size
);
292 eltmode
= (unsigned short int) member_header
.ar_mode
;
293 eltsize
= member_header
.ar_size
;
294 #endif /* Not Xenix. */
297 (*function
) (desc
, name
, ! long_name
, member_offset
,
298 member_offset
+ AR_HDR_SIZE
, eltsize
,
300 atol (member_header
.ar_date
),
301 atoi (member_header
.ar_uid
),
302 atoi (member_header
.ar_gid
),
304 member_header
.ar_date
,
305 member_header
.ar_uid
,
306 member_header
.ar_gid
,
307 #endif /* Not Xenix. */
319 if (member_offset
== last_member_offset
)
320 /* End of the chain. */
323 sscanf (member_header
.ar_nxtmem
, "%12ld", &member_offset
);
325 if (lseek (desc
, member_offset
, 0) != member_offset
)
332 /* If this member maps archive names, we must read it in. The
333 name map will always precede any members whose names must
340 namemap
= (char *) alloca (eltsize
);
341 nread
= read (desc
, namemap
, eltsize
);
342 if (nread
!= eltsize
)
348 /* The names are separated by newlines. Some formats have
349 a trailing slash. Null terminate the strings for
351 limit
= namemap
+ eltsize
;
352 for (clear
= namemap
; clear
< limit
; clear
++)
357 if (clear
[-1] == '/')
365 member_offset
+= AR_HDR_SIZE
+ eltsize
;
366 if (member_offset
% 2 != 0)
376 /* Return nonzero iff NAME matches MEM.
377 If TRUNCATED is nonzero, MEM may be truncated to
378 sizeof (struct ar_hdr.ar_name) - 1. */
381 ar_name_equal (name
, mem
, truncated
)
387 p
= rindex (name
, '/');
391 /* We no longer use this kludge, since we
392 now support long archive member names. */
394 #if 0 && !defined (AIAMAG) && !defined (APOLLO)
397 /* `reallylongname.o' matches `reallylongnam.o'.
398 If member names have a trailing slash, that's `reallylongna.o'. */
401 unsigned int max
= sizeof (h
.ar_name
);
402 unsigned int namelen
, memlen
;
404 if (strncmp (name
, mem
, max
- 3))
407 namelen
= strlen (name
);
408 memlen
= strlen (mem
);
410 if (namelen
> memlen
&& memlen
>= max
- 1
411 && name
[namelen
- 2] == '.' && name
[namelen
- 1] == 'o'
412 && mem
[memlen
- 2] == '.' && mem
[memlen
- 1] == 'o')
415 if (namelen
!= memlen
)
418 return (namelen
< max
- 3 || !strcmp (name
+ max
- 3, mem
+ max
- 3));
421 #else /* AIX or APOLLO. */
426 /* TRUNCATED should never be set on this system. */
430 return !strncmp (name
, mem
,
431 sizeof (hdr
.ar_name
) -
432 #if !defined (__hpux) && !defined (cray)
436 #endif /* !__hpux && !cray */
441 return !strcmp (name
, mem
);
448 ar_member_pos (desc
, mem
, truncated
,
449 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
, name
)
453 long int hdrpos
, datapos
, size
, date
;
457 if (!ar_name_equal (name
, mem
, truncated
))
462 /* Set date of member MEMNAME in archive ARNAME to current time.
463 Returns 0 if successful,
464 -1 if file ARNAME does not exist,
465 -2 if not a valid archive,
466 -3 if other random system call error (including file read-only),
467 1 if valid but member MEMNAME does not exist. */
470 ar_member_touch (arname
, memname
)
471 char *arname
, *memname
;
473 register long int pos
= ar_scan (arname
, ar_member_pos
, (long int) memname
);
475 struct ar_hdr ar_hdr
;
484 fd
= open (arname
, O_RDWR
, 0666);
487 /* Read in this member's header */
488 if (lseek (fd
, pos
, 0) < 0)
490 if (AR_HDR_SIZE
!= read (fd
, (char *) &ar_hdr
, AR_HDR_SIZE
))
492 /* Write back the header, thus touching the archive file. */
493 if (lseek (fd
, pos
, 0) < 0)
495 if (AR_HDR_SIZE
!= write (fd
, (char *) &ar_hdr
, AR_HDR_SIZE
))
497 /* The file's mtime is the time we we want. */
499 while (fstat (fd
, &statbuf
) < 0 && errno
== EINTR
);
501 fstat (fd
, &statbuf
);
503 #if defined(ARFMAG) || defined(AIAMAG)
504 /* Advance member's time to that time */
505 for (i
= 0; i
< sizeof ar_hdr
.ar_date
; i
++)
506 ar_hdr
.ar_date
[i
] = ' ';
507 sprintf (ar_hdr
.ar_date
, "%ld", (long int) statbuf
.st_mtime
);
509 ar_hdr
.ar_date
[strlen(ar_hdr
.ar_date
)] = ' ';
512 ar_hdr
.ar_date
= statbuf
.st_mtime
;
514 /* Write back this member's header */
515 if (lseek (fd
, pos
, 0) < 0)
517 if (AR_HDR_SIZE
!= write (fd
, (char *) &ar_hdr
, AR_HDR_SIZE
))
532 describe_member (desc
, name
, truncated
,
533 hdrpos
, datapos
, size
, date
, uid
, gid
, mode
)
537 long int hdrpos
, datapos
, size
, date
;
540 extern char *ctime ();
542 printf ("Member `%s'%s: %ld bytes at %ld (%ld).\n",
543 name
, truncated
? " (name might be truncated)" : "",
544 size
, hdrpos
, datapos
);
545 printf (" Date %s", ctime (&date
));
546 printf (" uid = %d, gid = %d, mode = 0%o.\n", uid
, gid
, mode
);
555 ar_scan (argv
[1], describe_member
);
561 #endif /* NO_ARCHIVES. */