]> Git Repo - binutils.git/blobdiff - binutils/ar.c
* ar.c: added "b" to fopens for dos
[binutils.git] / binutils / ar.c
index 0011dd32aa87bda10892594cf6242fe034307052..cdee7589c7f2011dfa826024455e9c484872bf07 100644 (file)
@@ -1,4 +1,22 @@
-/* ar.c - Archive modify and extract. */
+/* ar.c - Archive modify and extract.
+   Copyright (C) 1991 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
+(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 of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+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., 675 Mass Ave, Cambridge, MA 02139, 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
    when name truncated. No way to specify pos_end. Error messages should be
    more consistant.
 */
-#include "sysdep.h"
 #include "bfd.h"
-#include "ar.h"
+#include "sysdep.h"
+#include "bucomm.h"
+#include "aout/ar.h"
+#include "../bfd/libbfd.h"
 #include <stdio.h>
 #include <sys/time.h>
 #include <errno.h>
 #define BUFSIZE 8192
-/* Not great to have these here.  Should they be exported or not? */
-PROTO(size_t, bfd_read, (void *ptr, size_t size, size_t nitems, bfd * abfd));
-PROTO(size_t, bfd_write, (void *ptr, size_t size, size_t nitems, bfd * abfd));
 /* PROTO (void, open_inarch, (char *archive_filename)); */
 #ifdef __STDC__
 static void     open_inarch(char *archive_filename);
@@ -39,6 +56,10 @@ char           *program_name = NULL;
 bfd             bogus_archive;
 bfd            *inarch;                /* The input arch we're manipulating */
 
+/* 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.  */
@@ -116,6 +137,8 @@ main(argc, argv)
     char           *inarch_filename;
     char           *temp;
 
+    bfd_init();
+
 #ifdef GNU960
     check_v960( argc, argv );
     default_target = bfd_make_targ_name(BFD_COFF_FORMAT,HOST_BYTE_ORDER_BIG_P);
@@ -128,15 +151,15 @@ main(argc, argv)
        temp = program_name;    /* shouldn't happen, but... */
     else
        ++temp;
-    if (!strcmp(temp, "ranlib")) {
+    if (is_ranlib > 0 || (is_ranlib < 0 && strcmp(temp, "ranlib") == 0)) {
        if (argc < 2)
-           fatal("Too few command arguments.");
+           bfd_fatal("Too few command arguments.");
        ranlib_only(argv[1]);
     }
 
 
     if (argc < 3)
-       fatal("Too few command arguments.");
+       bfd_fatal("Too few command arguments.");
 
     arg_ptr = argv[1];
 
@@ -212,7 +235,8 @@ main(argc, argv)
        }
     }
 
-    if (operation == none && write_armap)
+    if ((operation == none || operation == print_table) 
+       && write_armap == true)
        ranlib_only(argv[2]);
 
     if (operation == none)
@@ -409,15 +433,6 @@ map_over_members(function, files, count)
 }
 
 
-/* Things which are interesting to map over all or some of the files: */
-
-void
-print_descr(abfd)
-    bfd            *abfd;
-{
-    print_arelt_descr(abfd, verbose);
-}
-
 void
 print_contents(abfd)
     bfd            *abfd;
@@ -487,7 +502,7 @@ extract_file(abfd)
     ostream = 0;
     if (size == 0) {
       /* Seems like an abstraction violation, eh?  Well it's OK! */
-      ostream = fopen(abfd->filename, "w");
+      ostream = fopen(abfd->filename, "wb");
       if (!ostream) {
        perror(abfd->filename);
        exit(1);
@@ -505,7 +520,7 @@ extract_file(abfd)
        /* See comment above; this saves disk arm motion */
        if (!ostream) {
            /* Seems like an abstraction violation, eh?  Well it's OK! */
-           ostream = fopen(abfd->filename, "w");
+           ostream = fopen(abfd->filename, "wb");
            if (!ostream) {
                perror(abfd->filename);
                exit(1);
@@ -565,7 +580,7 @@ do_quick_append(archive_filename, files_to_append)
     }
 
 
-    ofile = fopen(archive_filename, "a+");
+    ofile = fopen(archive_filename, "a+b");
     if (ofile == NULL) {
        perror(program_name);
        exit(1);
@@ -606,7 +621,7 @@ do_quick_append(archive_filename, files_to_append)
 
        BFD_SEND(temp, _bfd_truncate_arname, (temp, *files_to_append, (char *) hdr));
 
-       ifile = fopen(*files_to_append, "r");
+       ifile = fopen(*files_to_append, "rb");
        if (ifile == NULL)
            bfd_perror(program_name);
 
@@ -640,7 +655,6 @@ void
 write_archive()
 {
     bfd            *obfd;
-    char           *xmalloc();
     int             namelen = strlen(inarch->filename);
     char           *new_name = xmalloc(namelen + 6);
     bfd            *contents_head = inarch->next;
@@ -652,9 +666,9 @@ write_archive()
     else {
 #endif
        strcpy(new_name, inarch->filename);
-       strcpy(new_name + namelen, ".art");
+       strcpy(new_name + namelen, "-art");
        obfd = bfd_openw(new_name,
-                        /* violates abstraction; need a better protocol */
+                /* FIXME: violates abstraction; need a better protocol */
                         (inarch->xvec ? bfd_get_target(inarch) : NULL));
 
        if (obfd == NULL)
@@ -668,6 +682,11 @@ write_archive()
 
        if (!bfd_close(obfd))
            bfd_fatal(inarch->filename);
+
+       /* We don't care if this fails, we might be creating the
+          archive */
+       (void) unlink(inarch->filename);
+
        if (rename(new_name, inarch->filename) != 0)
            bfd_fatal(inarch->filename);
 #if 0
@@ -688,31 +707,21 @@ get_pos_bfd(contents, default_pos)
     bfd           **contents;
     enum pos        default_pos;
 {
-    bfd           **after_bfd;
-
+    bfd           **after_bfd = contents;
     enum pos        realpos = (postype == pos_default ? default_pos : postype);
-    switch (realpos) {
 
-    case pos_end:
-       after_bfd = contents;
-       while (*after_bfd) {
+    if (realpos == pos_end) {
+       while (*after_bfd)
            after_bfd = &((*after_bfd)->next);
-       }
-
-       break;
-#if 0
-    case pos_after:
-       for (after_bfd = contents; after_bfd; after_bfd = after_bfd->next)
-           if (!strcpy(after_bfd->filename, posname))
-               break;
-       break;
-    case pos_before:
-       for (after_bfd = contents; after_bfd; after_bfd = after_bfd->next)
-           if (after_bfd->next && (!strcpy(after_bfd->next->filename, posname)))
+    }
+    else {
+       for ( ; *after_bfd; after_bfd = &(*after_bfd)->next)
+           if (!strcmp((*after_bfd)->filename, posname)) {
+               if (realpos == pos_after)
+                   after_bfd = &(*after_bfd)->next;
                break;
-#endif
+       }
     }
-
     return after_bfd;
 }
 
@@ -924,3 +933,12 @@ ranlib_only(archname)
 }
 
 
+
+/* Things which are interesting to map over all or some of the files: */
+
+void
+print_descr(abfd)
+    bfd            *abfd;
+{
+    print_arelt_descr(abfd, verbose);
+}
This page took 0.029696 seconds and 4 git commands to generate.