+#endif
+ if (filename != (char *) NULL)
+ filename++;
+ else
+ filename = file;
+ return filename;
+}
+#endif
+
+/* Build a BFD style extended name table. */
+
+boolean
+_bfd_archive_bsd_construct_extended_name_table (abfd, tabloc, tablen, name)
+ bfd *abfd;
+ char **tabloc;
+ bfd_size_type *tablen;
+ const char **name;
+{
+ *name = "ARFILENAMES/";
+ return _bfd_construct_extended_name_table (abfd, false, tabloc, tablen);
+}
+
+/* Build an SVR4 style extended name table. */
+
+boolean
+_bfd_archive_coff_construct_extended_name_table (abfd, tabloc, tablen, name)
+ bfd *abfd;
+ char **tabloc;
+ bfd_size_type *tablen;
+ const char **name;
+{
+ *name = "//";
+ return _bfd_construct_extended_name_table (abfd, true, tabloc, tablen);
+}
+
+/* Follows archive_head and produces an extended name table if
+ necessary. Returns (in tabloc) a pointer to an extended name
+ table, and in tablen the length of the table. If it makes an entry
+ it clobbers the filename so that the element may be written without
+ further massage. Returns true if it ran successfully, false if
+ something went wrong. A successful return may still involve a
+ zero-length tablen! */
+
+boolean
+_bfd_construct_extended_name_table (abfd, trailing_slash, tabloc, tablen)
+ bfd *abfd;
+ boolean trailing_slash;
+ char **tabloc;
+ bfd_size_type *tablen;
+{
+ unsigned int maxname = abfd->xvec->ar_max_namelen;
+ bfd_size_type total_namelen = 0;
+ bfd *current;
+ char *strptr;
+
+ *tablen = 0;
+
+ /* Figure out how long the table should be. */
+ for (current = abfd->archive_head; current != NULL; current = current->next)
+ {
+ const char *normal;
+ unsigned int thislen;
+
+ normal = normalize (current, current->filename);
+ if (normal == NULL)
+ return false;
+
+ thislen = strlen (normal);
+
+ if (thislen > maxname
+ && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
+ thislen = maxname;
+
+ if (thislen > maxname)
+ {
+ /* Add one to leave room for \n. */
+ total_namelen += thislen + 1;
+ if (trailing_slash)
+ {
+ /* Leave room for trailing slash. */
+ ++total_namelen;
+ }
+ }
+ else
+ {
+ struct ar_hdr *hdr = arch_hdr (current);
+ if (strncmp (normal, hdr->ar_name, thislen) != 0
+ || (thislen < sizeof hdr->ar_name
+ && hdr->ar_name[thislen] != ar_padchar (current)))
+ {
+ /* Must have been using extended format even though it
+ didn't need to. Fix it to use normal format. */
+ memcpy (hdr->ar_name, normal, thislen);
+ if (thislen < maxname
+ || (thislen == maxname && thislen < sizeof hdr->ar_name))
+ hdr->ar_name[thislen] = ar_padchar (current);
+ }
+ }
+ }
+
+ if (total_namelen == 0)
+ return true;
+
+ *tabloc = bfd_zalloc (abfd, total_namelen);
+ if (*tabloc == NULL)
+ return false;
+
+ *tablen = total_namelen;
+ strptr = *tabloc;
+
+ for (current = abfd->archive_head; current != NULL; current =
+ current->next)
+ {
+ const char *normal;
+ unsigned int thislen;
+
+ normal = normalize (current, current->filename);
+ if (normal == NULL)
+ return false;
+
+ thislen = strlen (normal);
+ if (thislen > maxname)
+ {
+ /* Works for now; may need to be re-engineered if we
+ encounter an oddball archive format and want to
+ generalise this hack. */
+ struct ar_hdr *hdr = arch_hdr (current);
+ strcpy (strptr, normal);
+ if (! trailing_slash)
+ strptr[thislen] = '\012';
+ else
+ {
+ strptr[thislen] = '/';
+ strptr[thislen + 1] = '\012';
+ }
+ hdr->ar_name[0] = ar_padchar (current);
+ /* We know there will always be enough room (one of the few
+ cases where you may safely use sprintf). */
+ sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
+ /* Kinda Kludgy. We should just use the returned value of
+ sprintf but not all implementations get this right. */
+ {
+ char *temp = hdr->ar_name + 2;
+ for (; temp < hdr->ar_name + maxname; temp++)
+ if (*temp == '\0')
+ *temp = ' ';
+ }
+ strptr += thislen + 1;
+ if (trailing_slash)
+ ++strptr;
+ }
+ }
+
+ return true;
+}
+\f
+/** A couple of functions for creating ar_hdrs */
+
+#ifdef HPUX_LARGE_AR_IDS
+/* Function to encode large UID/GID values according to HP. */
+
+static void
+hpux_uid_gid_encode (str, id)
+ char str[6];
+ long int id;
+{
+ int cnt;
+
+ str[5] = '@' + (id & 3);
+ id >>= 2;
+
+ for (cnt = 4; cnt >= 0; ++cnt, id >>= 6)
+ str[cnt] = ' ' + (id & 0x3f);
+}
+#endif /* HPUX_LARGE_AR_IDS */
+
+#ifndef HAVE_GETUID
+#define getuid() 0
+#endif
+
+#ifndef HAVE_GETGID
+#define getgid() 0
+#endif
+
+/* Takes a filename, returns an arelt_data for it, or NULL if it can't
+ make one. The filename must refer to a filename in the filesystem.
+ The filename field of the ar_hdr will NOT be initialized. If member
+ is set, and it's an in-memory bfd, we fake it. */
+
+static struct areltdata *
+bfd_ar_hdr_from_filesystem (abfd, filename, member)
+ bfd *abfd;
+ const char *filename;
+ bfd *member;
+{
+ struct stat status;
+ struct areltdata *ared;
+ struct ar_hdr *hdr;
+ char *temp, *temp1;
+ bfd_size_type amt;
+
+ if (member && (member->flags & BFD_IN_MEMORY) != 0)
+ {
+ /* Assume we just "made" the member, and fake it. */
+ struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream;
+ time (&status.st_mtime);
+ status.st_uid = getuid ();
+ status.st_gid = getgid ();
+ status.st_mode = 0644;
+ status.st_size = bim->size;
+ }
+ else if (stat (filename, &status) != 0)
+ {
+ bfd_set_error (bfd_error_system_call);
+ return NULL;
+ }
+
+ amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
+ ared = (struct areltdata *) bfd_zalloc (abfd, amt);
+ if (ared == NULL)
+ return NULL;