/* Forward declarations */
+static void
+remove_output PARAMS ((void));
+
static void
map_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));
do_show_version ()
{
printf ("GNU %s version %s\n", program_name, program_version);
- exit (0);
+ xexit (0);
}
void
else
fprintf (stderr, "\
Usage: %s [-vV] archive\n", program_name);
- exit (1);
+ xexit (1);
+}
+
+/* Normalize a file name specified on the command line into a file
+ name which we will use in an archive. */
+
+static char *
+normalize (file)
+ char *file;
+{
+ char *filename = strrchr (file, '/');
+ if (filename != (char *) NULL)
+ {
+ filename++;
+ }
+ else
+ {
+ filename = file;
+ }
+ return filename;
+}
+
+/* Remove any output file. This is only called via xatexit. */
+
+static char *output_filename = NULL;
+static FILE *output_file = NULL;
+static bfd *output_bfd = NULL;
+
+static void
+remove_output ()
+{
+ if (output_filename != NULL)
+ {
+ if (output_bfd != NULL && output_bfd->iostream != NULL)
+ fclose ((FILE *) (output_bfd->iostream));
+ if (output_file != NULL)
+ fclose (output_file);
+ unlink (output_filename);
+ }
}
/* The option parsing should be in its own function.
bfd_init ();
show_version = 0;
+ xatexit (remove_output);
+
temp = strrchr (program_name, '/');
if (temp == (char *) NULL)
temp = program_name; /* shouldn't happen, but... */
ranlib_touch (argv[arg_index]);
++arg_index;
}
- exit (0);
+ xexit (0);
}
else
is_ranlib = 0;
if (argc == 2 && strcmp (argv[1], "-M") == 0)
{
mri_emul ();
- exit (0);
+ xexit (0);
}
if (argc < 2)
{
bfd *arch;
+ /* We can't write an armap when using ar q, so just do ar r
+ instead. */
+ if (operation == quick_append && write_armap)
+ operation = replace;
+
if ((operation == none || operation == print_table)
&& write_armap == 1)
{
ranlib_only (argv[2]);
- exit (0);
+ xexit (0);
}
if (operation == none)
files = arg_index < argc ? argv + arg_index : NULL;
+ /* We can't do a quick append if we need to construct an
+ extended name table, because do_quick_append won't be able to
+ rebuild the name table. Unfortunately, at this point we
+ don't actually know the maximum name length permitted by this
+ object file format. So, we guess. FIXME. */
+ if (operation == quick_append)
+ {
+ char **chk;
+
+ for (chk = files; chk != NULL && *chk != '\0'; chk++)
+ {
+ if (strlen (normalize (*chk)) > 14)
+ {
+ operation = replace;
+ break;
+ }
+ }
+ }
+
if (operation == quick_append)
{
/* Note that quick appending to a non-existent archive creates it,
even if there are no files to append. */
do_quick_append (inarch_filename, files);
- exit (0);
+ xexit (0);
}
arch = open_inarch (inarch_filename);
default:
fprintf (stderr, "%s: internal error -- this option not implemented\n",
program_name);
- exit (1);
+ xexit (1);
}
}
- return 0;
-}
-static char *
-normalize (file)
- char *file;
-{
- char *filename = strrchr (file, '/');
- if (filename != (char *) NULL)
- {
- filename++;
- }
- else
- {
- filename = file;
- }
- return filename;
+ xexit (0);
+ return 0;
}
bfd *
if (size == 0)
{
/* Seems like an abstraction violation, eh? Well it's OK! */
+ output_filename = bfd_get_filename (abfd);
+
ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
if (!ostream)
{
perror (bfd_get_filename (abfd));
- exit (1);
+ xexit (1);
}
+
+ output_file = ostream;
}
else
while (ncopied < size)
if (!ostream)
{
/* Seems like an abstraction violation, eh? Well it's OK! */
+ output_filename = bfd_get_filename (abfd);
+
ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
if (!ostream)
{
perror (bfd_get_filename (abfd));
- exit (1);
+ xexit (1);
}
+
+ output_file = ostream;
}
fwrite (cbuf, 1, nread, ostream);
ncopied += tocopy;
}
fclose (ostream);
+
+ output_file = NULL;
+ output_filename = NULL;
+
chmod (bfd_get_filename (abfd), buf.st_mode);
if (preserve_dates)
if (ofile == NULL)
{
perror (program_name);
- exit (1);
+ xexit (1);
}
temp = bfd_openr (archive_filename, NULL);
strcpy (new_name + namelen, "-art");
#endif
+ output_filename = new_name;
+
obfd = bfd_openw (new_name, bfd_get_target (iarch));
if (obfd == NULL)
bfd_fatal (old_name);
+ output_bfd = obfd;
+
bfd_set_format (obfd, bfd_archive);
/* Request writing the archive symbol table unless we've
if (!bfd_close (obfd))
bfd_fatal (old_name);
+ output_bfd = NULL;
+ output_filename = NULL;
+
/* We don't care if this fails; we might be creating the archive. */
bfd_close (iarch);
unlink (old_name);
}
fprintf (stderr, "%s: no entry %s in archive %s!\n",
program_name, *files_to_move, arch->filename);
- exit (1);
+ xexit (1);
next_file:;
}
write_armap = 1;
arch = open_inarch (archname);
if (arch == NULL)
- exit (1);
+ xexit (1);
write_archive (arch);
}
|| ! bfd_check_format (arch, bfd_archive))
bfd_fatal (archname);
+ if (! bfd_has_map (arch))
+ fatal ("%s: no archive map to update", archname);
+
bfd_update_armap_timestamp (arch);
if (! bfd_close (arch))