+
+static FILE * open_output_file (bfd *) ATTRIBUTE_RETURNS_NONNULL;
+
+static FILE *
+open_output_file (bfd * abfd)
+{
+ output_filename = bfd_get_filename (abfd);
+
+ /* PR binutils/17533: Do not allow directory traversal
+ outside of the current directory tree - unless the
+ user has explicitly specified an output directory. */
+ if (! is_valid_archive_path (output_filename))
+ {
+ char * base = (char *) lbasename (output_filename);
+
+ non_fatal (_("illegal output pathname for archive member: %s, using '%s' instead"),
+ output_filename, base);
+ output_filename = base;
+ }
+
+ if (output_dir)
+ {
+ size_t len = strlen (output_dir);
+
+ if (len > 0)
+ {
+ /* FIXME: There is a memory leak here, but it is not serious. */
+ if (IS_DIR_SEPARATOR (output_dir [len - 1]))
+ output_filename = concat (output_dir, output_filename, NULL);
+ else
+ output_filename = concat (output_dir, "/", output_filename, NULL);
+ }
+ }
+
+ if (verbose)
+ printf ("x - %s\n", output_filename);
+
+ FILE * ostream = fopen (output_filename, FOPEN_WB);
+ if (ostream == NULL)
+ {
+ perror (output_filename);
+ xexit (1);
+ }
+
+ return ostream;
+}
+