📄 ar.c
字号:
/* ar.c - Archive modify and extract. Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.This file is part of GNU Binutils.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Bugs: should use getopt the way tar does (complete w/optional -) and should have long options too. GNU ar used to check file against filesystem in quick_update and replace operations (would check mtime). Doesn't warn when name truncated. No way to specify pos_end. Error messages should be more consistant.*/#include "bfd.h"#include "libiberty.h"#include "progress.h"#include "bucomm.h"#include "aout/ar.h"#include "libbfd.h"#include "arsup.h"#include "filenames.h"#include <sys/stat.h>#ifdef __GO32___#define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */#else#define EXT_NAME_LEN 6 /* ditto for *NIX */#endif/* We need to open files in binary modes on system where that makes a difference. */#ifndef O_BINARY#define O_BINARY 0#endif#define BUFSIZE 8192/* Kludge declaration from BFD! This is ugly! FIXME! XXX */struct ar_hdr * bfd_special_undocumented_glue PARAMS ((bfd * abfd, const char *filename));/* Static declarations */static voidmri_emul PARAMS ((void));static const char *normalize PARAMS ((const char *, bfd *));static voidremove_output PARAMS ((void));static voidmap_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));static voidprint_contents PARAMS ((bfd * member));static voiddelete_members PARAMS ((bfd *, char **files_to_delete));#if 0static voiddo_quick_append PARAMS ((const char *archive_filename, char **files_to_append));#endifstatic voidmove_members PARAMS ((bfd *, char **files_to_move));static voidreplace_members PARAMS ((bfd *, char **files_to_replace, boolean quick));static voidprint_descr PARAMS ((bfd * abfd));static voidwrite_archive PARAMS ((bfd *));static voidranlib_only PARAMS ((const char *archname));static voidranlib_touch PARAMS ((const char *archname));static voidusage PARAMS ((int));/** Globals and flags */int mri_mode;/* This flag distinguishes between ar and ranlib: 1 means this is 'ranlib'; 0 means this is 'ar'. -1 means if we should use argv[0] to decide. */extern int is_ranlib;/* Nonzero means don't warn about creating the archive file if necessary. */int silent_create = 0;/* Nonzero means describe each action performed. */int verbose = 0;/* Nonzero means preserve dates of members when extracting them. */int preserve_dates = 0;/* Nonzero means don't replace existing members whose dates are more recent than the corresponding files. */int newer_only = 0;/* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF member). -1 means we've been explicitly asked to not write a symbol table; +1 means we've been explictly asked to write it; 0 is the default. Traditionally, the default in BSD has been to not write the table. However, for POSIX.2 compliance the default is now to write a symbol table if any of the members are object files. */int write_armap = 0;/* Nonzero means it's the name of an existing member; position new or moved files with respect to this one. */char *posname = NULL;/* Sez how to use `posname': pos_before means position before that member. pos_after means position after that member. pos_end means always at end. pos_default means default appropriately. For the latter two, `posname' should also be zero. */enum pos { pos_default, pos_before, pos_after, pos_end } postype = pos_default;static bfd **get_pos_bfd PARAMS ((bfd **, enum pos, const char *));/* For extract/delete only. If COUNTED_NAME_MODE is true, we only extract the COUNTED_NAME_COUNTER instance of that name. */static boolean counted_name_mode = 0;static int counted_name_counter = 0;/* Whether to truncate names of files stored in the archive. */static boolean ar_truncate = false;/* Whether to use a full file name match when searching an archive. This is convenient for archives created by the Microsoft lib program. */static boolean full_pathname = false;int interactive = 0;static voidmri_emul (){ interactive = isatty (fileno (stdin)); yyparse ();}/* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero, COUNT is the length of the FILES chain; FUNCTION is called on each entry whose name matches one in FILES. */static voidmap_over_members (arch, function, files, count) bfd *arch; void (*function) PARAMS ((bfd *)); char **files; int count;{ bfd *head; int match_count; if (count == 0) { for (head = arch->next; head; head = head->next) { PROGRESS (1); function (head); } return; } /* This may appear to be a baroque way of accomplishing what we want. However we have to iterate over the filenames in order to notice where a filename is requested but does not exist in the archive. Ditto mapping over each file each time -- we want to hack multiple references. */ for (; count > 0; files++, count--) { boolean found = false; match_count = 0; for (head = arch->next; head; head = head->next) { PROGRESS (1); if (head->filename == NULL) { /* Some archive formats don't get the filenames filled in until the elements are opened. */ struct stat buf; bfd_stat_arch_elt (head, &buf); } if ((head->filename != NULL) && (!FILENAME_CMP (normalize (*files, arch), head->filename))) { ++match_count; if (counted_name_mode && match_count != counted_name_counter) { /* Counting, and didn't match on count; go on to the next one. */ continue; } found = true; function (head); } } if (!found) /* xgettext:c-format */ fprintf (stderr, _("no entry %s in archive\n"), *files); }}boolean operation_alters_arch = false;static voidusage (help) int help;{ FILE *s; s = help ? stdout : stderr; if (! is_ranlib) { /* xgettext:c-format */ fprintf (s, _("Usage: %s [-X32_64] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"), program_name); /* xgettext:c-format */ fprintf (s, _(" %s -M [<mri-script]\n"), program_name); fprintf (s, _(" commands:\n")); fprintf (s, _(" d - delete file(s) from the archive\n")); fprintf (s, _(" m[ab] - move file(s) in the archive\n")); fprintf (s, _(" p - print file(s) found in the archive\n")); fprintf (s, _(" q[f] - quick append file(s) to the archive\n")); fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n")); fprintf (s, _(" t - display contents of archive\n")); fprintf (s, _(" x[o] - extract file(s) from the archive\n")); fprintf (s, _(" command specific modifiers:\n")); fprintf (s, _(" [a] - put file(s) after [member-name]\n")); fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n")); fprintf (s, _(" [N] - use instance [count] of name\n")); fprintf (s, _(" [f] - truncate inserted file names\n")); fprintf (s, _(" [P] - use full path names when matching\n")); fprintf (s, _(" [o] - preserve original dates\n")); fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n")); fprintf (s, _(" generic modifiers:\n")); fprintf (s, _(" [c] - do not warn if the library had to be created\n")); fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n")); 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, _(" [-X32_64] - (ignored)\n")); } else /* xgettext:c-format */ fprintf (s, _("Usage: %s [-vV] archive\n"), program_name); list_supported_targets (program_name, stderr); if (help) fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO); xexit (help ? 0 : 1);}/* Normalize a file name specified on the command line into a file name which we will use in an archive. */static const char *normalize (file, abfd) const char *file; bfd *abfd;{ const char *filename; if (full_pathname) return file; filename = strrchr (file, '/');#ifdef HAVE_DOS_BASED_FILE_SYSTEM { /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ char *bslash = strrchr (file, '\\'); if (filename == NULL || (bslash != NULL && bslash > filename)) filename = bslash; if (filename == NULL && file[0] != '\0' && file[1] == ':') filename = file + 1; }#endif if (filename != (char *) NULL) filename++; else filename = file; if (ar_truncate && abfd != NULL && strlen (filename) > abfd->xvec->ar_max_namelen) { char *s; /* Space leak. */ s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1); memcpy (s, filename, abfd->xvec->ar_max_namelen); s[abfd->xvec->ar_max_namelen] = '\0'; filename = s; } return filename;}/* Remove any output file. This is only called via xatexit. */static const char *output_filename = NULL;static FILE *output_file = NULL;static bfd *output_bfd = NULL;static voidremove_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. It will be when I have getopt working. */intmain (argc, argv) int argc; char **argv;{ char *arg_ptr; char c; enum { none = 0, delete, replace, print_table, print_files, extract, move, quick_append } operation = none; int arg_index; char **files; int file_count; char *inarch_filename; int show_version;#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) setlocale (LC_MESSAGES, "");#endif bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); program_name = argv[0]; xmalloc_set_program_name (program_name); if (is_ranlib < 0) { char *temp; temp = strrchr (program_name, '/');#ifdef HAVE_DOS_BASED_FILE_SYSTEM { /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ char *bslash = strrchr (program_name, '\\'); if (temp == NULL || (bslash != NULL && bslash > temp)) temp = bslash; if (temp == NULL && program_name[0] != '\0' && program_name[1] == ':') temp = program_name + 1; }#endif if (temp == NULL) temp = program_name; else ++temp; if (strlen (temp) >= 6 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0) is_ranlib = 1; else is_ranlib = 0; } if (argc > 1 && argv[1][0] == '-') { if (strcmp (argv[1], "--help") == 0) usage (1); else if (strcmp (argv[1], "--version") == 0) { if (is_ranlib) print_version ("ranlib"); else print_version ("ar"); } } START_PROGRESS (program_name, 0); bfd_init (); set_default_bfd_target (); show_version = 0; xatexit (remove_output); /* Ignored for (partial) AIX compatibility. On AIX, the -X option can be used to ignore certain kinds of object files in the archive (the 64-bit objects or the 32-bit objects). GNU ar always looks at all kinds of objects in an archive. */ while (argc > 1 && strcmp (argv[1], "-X32_64") == 0) { argv++; argc--; } if (is_ranlib) { boolean touch = false; if (argc < 2 || strcmp (argv[1], "--help") == 0) usage (0); if (strcmp (argv[1], "-V") == 0 || strcmp (argv[1], "-v") == 0 || strncmp (argv[1], "--v", 3) == 0) print_version ("ranlib"); arg_index = 1; if (strcmp (argv[1], "-t") == 0) { ++arg_index; touch = true; } while (arg_index < argc) { if (! touch) ranlib_only (argv[arg_index]); else ranlib_touch (argv[arg_index]); ++arg_index; } xexit (0); } if (argc == 2 && strcmp (argv[1], "-M") == 0) { mri_emul (); xexit (0); } if (argc < 2) usage (0); arg_ptr = argv[1]; if (*arg_ptr == '-')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -