+ fprintf (file, "%s", bfd_get_filename (abfd));
+
+ if (offsets)
+ {
+ if (bfd_is_thin_archive (abfd) && abfd->proxy_origin)
+ fprintf (file, " 0x%lx", (unsigned long) abfd->proxy_origin);
+ else if (!bfd_is_thin_archive (abfd) && abfd->origin)
+ fprintf (file, " 0x%lx", (unsigned long) abfd->origin);
+ }
+
+ fprintf (file, "\n");
+}
+
+/* Return a path for a new temporary file in the same directory
+ as file PATH. */
+
+static char *
+template_in_dir (const char *path)
+{
+#define template "stXXXXXX"
+ const char *slash = strrchr (path, '/');
+ char *tmpname;
+ size_t len;
+
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+ {
+ /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
+ char *bslash = strrchr (path, '\\');
+
+ if (slash == NULL || (bslash != NULL && bslash > slash))
+ slash = bslash;
+ if (slash == NULL && path[0] != '\0' && path[1] == ':')
+ slash = path + 1;
+ }
+#endif
+
+ if (slash != (char *) NULL)
+ {
+ len = slash - path;
+ tmpname = (char *) xmalloc (len + sizeof (template) + 2);
+ memcpy (tmpname, path, len);
+
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+ /* If tmpname is "X:", appending a slash will make it a root
+ directory on drive X, which is NOT the same as the current
+ directory on drive X. */
+ if (len == 2 && tmpname[1] == ':')
+ tmpname[len++] = '.';
+#endif
+ tmpname[len++] = '/';
+ }
+ else
+ {
+ tmpname = (char *) xmalloc (sizeof (template));
+ len = 0;
+ }
+
+ memcpy (tmpname + len, template, sizeof (template));
+ return tmpname;
+#undef template
+}
+
+/* Return the name of a created temporary file in the same directory
+ as FILENAME. */
+
+char *
+make_tempname (char *filename)
+{
+ char *tmpname = template_in_dir (filename);
+ int fd;
+
+#ifdef HAVE_MKSTEMP
+ fd = mkstemp (tmpname);
+#else
+ tmpname = mktemp (tmpname);
+ if (tmpname == NULL)
+ return NULL;
+ fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
+#endif
+ if (fd == -1)
+ {
+ free (tmpname);
+ return NULL;
+ }
+ close (fd);
+ return tmpname;
+}
+
+/* Return the name of a created temporary directory inside the
+ directory containing FILENAME. */
+
+char *
+make_tempdir (char *filename)
+{
+ char *tmpname = template_in_dir (filename);
+
+#ifdef HAVE_MKDTEMP
+ return mkdtemp (tmpname);
+#else
+ tmpname = mktemp (tmpname);
+ if (tmpname == NULL)
+ return NULL;
+#if defined (_WIN32) && !defined (__CYGWIN32__)
+ if (mkdir (tmpname) != 0)
+ return NULL;
+#else
+ if (mkdir (tmpname, 0700) != 0)
+ return NULL;
+#endif
+ return tmpname;
+#endif
+}
+
+/* Parse a string into a VMA, with a fatal error if it can't be
+ parsed. */
+
+bfd_vma
+parse_vma (const char *s, const char *arg)
+{
+ bfd_vma ret;
+ const char *end;
+
+ ret = bfd_scan_vma (s, &end, 0);
+
+ if (*end != '\0')
+ fatal (_("%s: bad number: %s"), arg, s);
+
+ return ret;
+}
+
+/* Returns the size of the named file. If the file does not
+ exist, or if it is not a real file, then a suitable non-fatal
+ error message is printed and (off_t) -1 is returned. */
+
+off_t
+get_file_size (const char * file_name)
+{
+ struct stat statbuf;
+
+ if (file_name == NULL)
+ return (off_t) -1;
+
+ if (stat (file_name, &statbuf) < 0)
+ {
+ if (errno == ENOENT)
+ non_fatal (_("'%s': No such file"), file_name);
+ else
+ non_fatal (_("Warning: could not locate '%s'. reason: %s"),
+ file_name, strerror (errno));
+ }
+ else if (S_ISDIR (statbuf.st_mode))
+ non_fatal (_("Warning: '%s' is a directory"), file_name);
+ else if (! S_ISREG (statbuf.st_mode))
+ non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
+ else if (statbuf.st_size < 0)
+ non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
+ file_name);
+ else
+ return statbuf.st_size;
+
+ return (off_t) -1;
+}
+
+/* Return the filename in a static buffer. */
+
+const char *
+bfd_get_archive_filename (const bfd *abfd)
+{
+ static size_t curr = 0;
+ static char *buf;
+ size_t needed;
+
+ assert (abfd != NULL);
+
+ if (abfd->my_archive == NULL
+ || bfd_is_thin_archive (abfd->my_archive))
+ return bfd_get_filename (abfd);
+
+ needed = (strlen (bfd_get_filename (abfd->my_archive))
+ + strlen (bfd_get_filename (abfd)) + 3);
+ if (needed > curr)
+ {
+ if (curr)
+ free (buf);
+ curr = needed + (needed >> 1);
+ buf = (char *) xmalloc (curr);
+ }
+ sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
+ bfd_get_filename (abfd));
+ return buf;
+}
+
+/* Returns TRUE iff PATHNAME, a filename of an archive member,
+ is valid for writing. For security reasons absolute paths
+ and paths containing /../ are not allowed. See PR 17533. */
+
+bfd_boolean
+is_valid_archive_path (char const * pathname)
+{
+ const char * n = pathname;
+
+ if (IS_ABSOLUTE_PATH (n))
+ return FALSE;
+
+ while (*n)
+ {
+ if (*n == '.' && *++n == '.' && ( ! *++n || IS_DIR_SEPARATOR (*n)))
+ return FALSE;
+
+ while (*n && ! IS_DIR_SEPARATOR (*n))
+ n++;
+ while (IS_DIR_SEPARATOR (*n))
+ n++;
+ }
+
+ return TRUE;