X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=binutils%2Far.c;h=10e88890f99ca2ee85e767fd1adb3b86e929252f;hb=42e344a77c6f6f46e98482117e80c3d0ea9db853;hp=585b704287c21919856a1e5dce0c29fc4bda9bb5;hpb=bb14f524002c5b0e93c17e929109f4c735f11f5b;p=deliverable%2Fbinutils-gdb.git diff --git a/binutils/ar.c b/binutils/ar.c index 585b704287..10e88890f9 100644 --- a/binutils/ar.c +++ b/binutils/ar.c @@ -1,13 +1,13 @@ /* ar.c - Archive modify and extract. Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, - 2001, 2002, 2003, 2004 + 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. This file is part of GNU Binutils. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -17,7 +17,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, + MA 02110-1301, USA. */ /* Bugs: should use getopt the way tar does (complete w/optional -) and @@ -26,12 +27,13 @@ when name truncated. No way to specify pos_end. Error messages should be more consistent. */ +#include "sysdep.h" #include "bfd.h" #include "libiberty.h" #include "progress.h" -#include "bucomm.h" #include "aout/ar.h" #include "libbfd.h" +#include "bucomm.h" #include "arsup.h" #include "filenames.h" #include "binemul.h" @@ -49,8 +51,6 @@ #define O_BINARY 0 #endif -#define BUFSIZE 8192 - /* Kludge declaration from BFD! This is ugly! FIXME! XXX */ struct ar_hdr * @@ -70,13 +70,13 @@ static void replace_members (bfd *, char **files_to_replace, bfd_boolean quick); static void print_descr (bfd * abfd); static void write_archive (bfd *); -static void ranlib_only (const char *archname); -static void ranlib_touch (const char *archname); +static int ranlib_only (const char *archname); +static int ranlib_touch (const char *archname); static void usage (int); /** Globals and flags */ -int mri_mode; +static int mri_mode; /* This flag distinguishes between ar and ranlib: 1 means this is 'ranlib'; 0 means this is 'ar'. @@ -155,7 +155,7 @@ map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count) if (count == 0) { - for (head = arch->next; head; head = head->next) + for (head = arch->archive_next; head; head = head->archive_next) { PROGRESS (1); function (head); @@ -174,7 +174,7 @@ map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count) bfd_boolean found = FALSE; match_count = 0; - for (head = arch->next; head; head = head->next) + for (head = arch->archive_next; head; head = head->archive_next) { PROGRESS (1); if (head->filename == NULL) @@ -244,7 +244,8 @@ usage (int help) fprintf (s, _(" [S] - do not build a symbol table\n")); fprintf (s, _(" [v] - be verbose\n")); fprintf (s, _(" [V] - display the version number\n")); - + fprintf (s, _(" @ - read options from \n")); + ar_emul_usage (s); } else @@ -253,13 +254,15 @@ usage (int help) fprintf (s, _("Usage: %s [options] archive\n"), program_name); fprintf (s, _(" Generate an index to speed access to archives\n")); fprintf (s, _(" The options are:\n\ + @ Read options from \n\ + -t Update the archive's symbol map timestamp\n\ -h --help Print this help message\n\ - -V --version Print version information\n")); + -v --version Print version information\n")); } - list_supported_targets (program_name, stderr); + list_supported_targets (program_name, s); - if (help) + if (REPORT_BUGS_TO[0] && help) fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO); xexit (help ? 0 : 1); @@ -362,6 +365,8 @@ main (int argc, char **argv) program_name = argv[0]; xmalloc_set_program_name (program_name); + expandargv (&argc, &argv); + if (is_ranlib < 0) { char *temp; @@ -418,6 +423,7 @@ main (int argc, char **argv) if (is_ranlib) { + int status = 0; bfd_boolean touch = FALSE; if (argc < 2 @@ -427,7 +433,7 @@ main (int argc, char **argv) usage (0); if (strcmp (argv[1], "-V") == 0 || strcmp (argv[1], "-v") == 0 - || strncmp (argv[1], "--v", 3) == 0) + || CONST_STRNEQ (argv[1], "--v")) print_version ("ranlib"); arg_index = 1; if (strcmp (argv[1], "-t") == 0) @@ -438,12 +444,12 @@ main (int argc, char **argv) while (arg_index < argc) { if (! touch) - ranlib_only (argv[arg_index]); + status |= ranlib_only (argv[arg_index]); else - ranlib_touch (argv[arg_index]); + status |= ranlib_touch (argv[arg_index]); ++arg_index; } - xexit (0); + xexit (status); } if (argc == 2 && strcmp (argv[1], "-M") == 0) @@ -595,10 +601,7 @@ main (int argc, char **argv) if ((operation == none || operation == print_table) && write_armap == 1) - { - ranlib_only (argv[arg_index]); - xexit (0); - } + xexit (ranlib_only (argv[arg_index])); if (operation == none) fatal (_("no operation specified")); @@ -758,7 +761,7 @@ open_inarch (const char *archive_filename, const char *file) xexit (1); } - last_one = &(arch->next); + last_one = &(arch->archive_next); /* Read all the contents right away, regardless. */ for (next_one = bfd_openr_next_archived_file (arch, NULL); next_one; @@ -766,7 +769,7 @@ open_inarch (const char *archive_filename, const char *file) { PROGRESS (1); *last_one = next_one; - last_one = &next_one->next; + last_one = &next_one->archive_next; } *last_one = (bfd *) NULL; if (bfd_get_error () != bfd_error_no_more_archived_files) @@ -777,10 +780,10 @@ open_inarch (const char *archive_filename, const char *file) static void print_contents (bfd *abfd) { - int ncopied = 0; + size_t ncopied = 0; char *cbuf = xmalloc (BUFSIZE); struct stat buf; - long size; + size_t size; if (bfd_stat_arch_elt (abfd, &buf) != 0) /* xgettext:c-format */ fatal (_("internal stat error on %s"), bfd_get_filename (abfd)); @@ -795,8 +798,8 @@ print_contents (bfd *abfd) while (ncopied < size) { - int nread; - int tocopy = size - ncopied; + size_t nread; + size_t tocopy = size - ncopied; if (tocopy > BUFSIZE) tocopy = BUFSIZE; @@ -805,7 +808,12 @@ print_contents (bfd *abfd) /* xgettext:c-format */ fatal (_("%s is not a valid archive"), bfd_get_filename (bfd_my_archive (abfd))); - fwrite (cbuf, 1, nread, stdout); + + /* fwrite in mingw32 may return int instead of size_t. Cast the + return value to size_t to avoid comparison between signed and + unsigned values. */ + if ((size_t) fwrite (cbuf, 1, nread, stdout) != nread) + fatal ("stdout: %s", strerror (errno)); ncopied += tocopy; } free (cbuf); @@ -826,9 +834,9 @@ extract_file (bfd *abfd) { FILE *ostream; char *cbuf = xmalloc (BUFSIZE); - int nread, tocopy; - long ncopied = 0; - long size; + size_t nread, tocopy; + size_t ncopied = 0; + size_t size; struct stat buf; if (bfd_stat_arch_elt (abfd, &buf) != 0) @@ -836,10 +844,6 @@ extract_file (bfd *abfd) fatal (_("internal stat error on %s"), bfd_get_filename (abfd)); size = buf.st_size; - if (size < 0) - /* xgettext:c-format */ - fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd)); - if (verbose) printf ("x - %s\n", bfd_get_filename (abfd)); @@ -888,7 +892,12 @@ extract_file (bfd *abfd) output_file = ostream; } - fwrite (cbuf, 1, nread, ostream); + + /* fwrite in mingw32 may return int instead of size_t. Cast + the return value to size_t to avoid comparison between + signed and unsigned values. */ + if ((size_t) fwrite (cbuf, 1, nread, ostream) != nread) + fatal ("%s: %s", output_filename, strerror (errno)); ncopied += tocopy; } @@ -916,12 +925,15 @@ write_archive (bfd *iarch) { bfd *obfd; char *old_name, *new_name; - bfd *contents_head = iarch->next; + bfd *contents_head = iarch->archive_next; old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1); strcpy (old_name, bfd_get_filename (iarch)); new_name = make_tempname (old_name); + if (new_name == NULL) + bfd_fatal ("could not create temporary file whilst writing archive"); + output_filename = new_name; obfd = bfd_openw (new_name, bfd_get_target (iarch)); @@ -985,15 +997,15 @@ get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname) if (realpos == pos_end) { while (*after_bfd) - after_bfd = &((*after_bfd)->next); + after_bfd = &((*after_bfd)->archive_next); } else { - for (; *after_bfd; after_bfd = &(*after_bfd)->next) + for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next) if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0) { if (realpos == pos_after) - after_bfd = &(*after_bfd)->next; + after_bfd = &(*after_bfd)->archive_next; break; } } @@ -1025,7 +1037,7 @@ delete_members (bfd *arch, char **files_to_delete) found = FALSE; match_count = 0; - current_ptr_ptr = &(arch->next); + current_ptr_ptr = &(arch->archive_next); while (*current_ptr_ptr) { if (FILENAME_CMP (normalize (*files_to_delete, arch), @@ -1045,12 +1057,12 @@ delete_members (bfd *arch, char **files_to_delete) if (verbose) printf ("d - %s\n", *files_to_delete); - *current_ptr_ptr = ((*current_ptr_ptr)->next); + *current_ptr_ptr = ((*current_ptr_ptr)->archive_next); goto next_file; } } - current_ptr_ptr = &((*current_ptr_ptr)->next); + current_ptr_ptr = &((*current_ptr_ptr)->archive_next); } if (verbose && !found) @@ -1079,7 +1091,7 @@ move_members (bfd *arch, char **files_to_move) for (; *files_to_move; ++files_to_move) { - current_ptr_ptr = &(arch->next); + current_ptr_ptr = &(arch->archive_next); while (*current_ptr_ptr) { bfd *current_ptr = *current_ptr_ptr; @@ -1089,13 +1101,13 @@ move_members (bfd *arch, char **files_to_move) /* Move this file to the end of the list - first cut from where it is. */ bfd *link; - *current_ptr_ptr = current_ptr->next; + *current_ptr_ptr = current_ptr->archive_next; /* Now glue to end */ - after_bfd = get_pos_bfd (&arch->next, pos_end, NULL); + after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL); link = *after_bfd; *after_bfd = current_ptr; - current_ptr->next = link; + current_ptr->archive_next = link; if (verbose) printf ("m - %s\n", *files_to_move); @@ -1103,7 +1115,7 @@ move_members (bfd *arch, char **files_to_move) goto next_file; } - current_ptr_ptr = &((*current_ptr_ptr)->next); + current_ptr_ptr = &((*current_ptr_ptr)->archive_next); } /* xgettext:c-format */ fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename); @@ -1128,7 +1140,7 @@ replace_members (bfd *arch, char **files_to_move, bfd_boolean quick) { if (! quick) { - current_ptr = &arch->next; + current_ptr = &arch->archive_next; while (*current_ptr) { current = *current_ptr; @@ -1158,24 +1170,24 @@ replace_members (bfd *arch, char **files_to_move, bfd_boolean quick) goto next_file; } - after_bfd = get_pos_bfd (&arch->next, pos_after, + after_bfd = get_pos_bfd (&arch->archive_next, pos_after, current->filename); if (ar_emul_replace (after_bfd, *files_to_move, verbose)) { /* Snip out this entry from the chain. */ - *current_ptr = (*current_ptr)->next; + *current_ptr = (*current_ptr)->archive_next; changed = TRUE; } goto next_file; } - current_ptr = &(current->next); + current_ptr = &(current->archive_next); } } /* Add to the end of the archive. */ - after_bfd = get_pos_bfd (&arch->next, pos_end, NULL); + after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL); if (ar_emul_append (after_bfd, *files_to_move, verbose)) changed = TRUE; @@ -1191,23 +1203,24 @@ replace_members (bfd *arch, char **files_to_move, bfd_boolean quick) output_filename = NULL; } -static void +static int ranlib_only (const char *archname) { bfd *arch; if (get_file_size (archname) < 1) - return; + return 1; write_armap = 1; arch = open_inarch (archname, (char *) NULL); if (arch == NULL) xexit (1); write_archive (arch); + return 0; } /* Update the timestamp of the symbol map of an archive. */ -static void +static int ranlib_touch (const char *archname) { #ifdef __GO32__ @@ -1219,7 +1232,7 @@ ranlib_touch (const char *archname) char **matching; if (get_file_size (archname) < 1) - return; + return 1; f = open (archname, O_RDWR | O_BINARY, 0); if (f < 0) { @@ -1250,6 +1263,7 @@ ranlib_touch (const char *archname) if (! bfd_close (arch)) bfd_fatal (archname); #endif + return 0; } /* Things which are interesting to map over all or some of the files: */