]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Use consistent error messages for missing files.
authorNick Clifton <nickc@redhat.com>
Fri, 7 Nov 2003 12:19:34 +0000 (12:19 +0000)
committerNick Clifton <nickc@redhat.com>
Fri, 7 Nov 2003 12:19:34 +0000 (12:19 +0000)
Detect directories where an ordinary file is expected.

15 files changed:
binutils/ChangeLog
binutils/addr2line.c
binutils/ar.c
binutils/bucomm.c
binutils/bucomm.h
binutils/nm.c
binutils/objcopy.c
binutils/objdump.c
binutils/readelf.c
binutils/size.c
binutils/strings.c
gas/ChangeLog
gas/input-file.c
ld/ChangeLog
ld/ldfile.c

index f683369a9e57a7d73ad1aaf3199266d0ec565759..315a4d4229c08975afbaa6e80e5c66cc76de7c3e 100644 (file)
@@ -1,3 +1,18 @@
+2003-11-07  Jonathan R. Grant  <jg-binutils@jguk.org>
+
+       * bucomm,c (get_file_size): New function.  Returns the size of a
+         file.
+       * bucomm.h: Add prototype for get_file_size.
+       * addr2line.c (process_file): Use new function.
+       * ar.c (main, ranlib_only, ranlib_touch): Likewise.
+       * nm.c (display_file): Likewise.
+       * objcopy.c (add_specific_symbols, copy_file, strip_main,
+         copy_main): Likewise.
+       * objdump.c (display_file): Likewise.
+       * size.c (display_file): Likewise.
+       * strings.c (strings_file): Likewise.
+       * readelf.c (process_file): Use similar code to get_file_size.
+
 2003-11-06  Bruno Rohee  <bruno@rohee.com>
 
        * ieee.c: Fix "the the" typo.
index b5f5a0c5f15e615b0738e629fcad0938342fd857..354153eadb13c3e87e2c3c4432cca55eb1ea4550 100644 (file)
@@ -230,6 +230,9 @@ process_file (const char *file_name, const char *target)
   bfd *abfd;
   char **matching;
 
+  if (get_file_size (file_name) < 1)
+    return;
+
   abfd = bfd_openr (file_name, target);
   if (abfd == NULL)
     bfd_fatal (file_name);
index 1724171644dc47b0c4e1c8307baa88bf2b09d1b1..77fa4673977d5a65e1c7d70e3b6eb72f370b37e1 100644 (file)
@@ -1305,7 +1305,9 @@ replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
 
       /* Add to the end of the archive.  */
       after_bfd = get_pos_bfd (&arch->next, pos_end, NULL);
-      if (ar_emul_append (after_bfd, *files_to_move, verbose))
+
+      if (get_file_size (* files_to_move) > 0
+         && ar_emul_append (after_bfd, *files_to_move, verbose))
        changed = TRUE;
 
     next_file:;
@@ -1324,6 +1326,8 @@ ranlib_only (const char *archname)
 {
   bfd *arch;
 
+  if (get_file_size (archname) < 1)
+    return;
   write_armap = 1;
   arch = open_inarch (archname, (char *) NULL);
   if (arch == NULL)
@@ -1344,6 +1348,8 @@ ranlib_touch (const char *archname)
   bfd *arch;
   char **matching;
 
+  if (get_file_size (archname) < 1)
+    return;
   f = open (archname, O_RDWR | O_BINARY, 0);
   if (f < 0)
     {
index a6bb6e483f9d72effe3f37799614510929a44922..6573e2d9c7fa09ff610cd869bd2937048f706303 100644 (file)
@@ -450,3 +450,28 @@ parse_vma (const char *s, const char *arg)
 
   return ret;
 }
+
+/* Returns the size of the named file.  If the file does not
+   exist, or if it is not a real file, then a suitable non-fatal
+   error message is printed and zero is returned.  */
+
+off_t
+get_file_size (const char * file_name)
+{
+  struct stat statbuf;
+  
+  if (stat (file_name, &statbuf) < 0)
+    {
+      if (errno == ENOENT)
+       non_fatal (_("'%s': No such file"), file_name);
+      else
+       non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
+                  file_name, strerror (errno));
+    }  
+  else if (! S_ISREG (statbuf.st_mode))
+    non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
+  else
+    return statbuf.st_size;
+
+  return 0;
+}
index 9b17791cd52198fe532fb4b5396e06d5b716331d..f604053cec80c1a526a21887dae75b7ffea43e82 100644 (file)
@@ -174,6 +174,8 @@ char *make_tempname (char *);
 
 bfd_vma parse_vma (const char *, const char *);
 
+off_t get_file_size (const char *);
+
 extern char *program_name;
 
 /* filemode.c */
index d2d38c4b54ad41b82382759231b912bbe3f99447..bac7d388ce29c342e2c50ce73eaed165362a20e1 100644 (file)
@@ -602,6 +602,9 @@ display_file (char *filename)
   bfd *file;
   char **matching;
 
+  if (get_file_size (filename) < 1)
+    return FALSE;
+
   file = bfd_openr (filename, target);
   if (file == NULL)
     {
index 8a1a67a74ef5e21976177f30e215ec722d140803..8796dd6e0208bb0d823a54c9e6b0caabae5bbbbc 100644 (file)
@@ -583,28 +583,27 @@ add_specific_symbol (const char *name, struct symlist **list)
 static void
 add_specific_symbols (const char *filename, struct symlist **list)
 {
-  struct stat st;
+  off_t  size;
   FILE * f;
   char * line;
   char * buffer;
   unsigned int line_count;
 
-  if (stat (filename, & st) < 0)
-    fatal (_("cannot stat: %s: %s"), filename, strerror (errno));
-  if (st.st_size == 0)
+  size = get_file_size (filename);
+  if (size == 0)
     return;
 
-  buffer = xmalloc (st.st_size + 2);
+  buffer = xmalloc (size + 2);
   f = fopen (filename, FOPEN_RT);
   if (f == NULL)
-    fatal (_("cannot open: %s: %s"), filename, strerror (errno));
+    fatal (_("cannot open '%s': %s"), filename, strerror (errno));
 
-  if (fread (buffer, 1, st.st_size, f) == 0 || ferror (f))
+  if (fread (buffer, 1, size, f) == 0 || ferror (f))
     fatal (_("%s: fread failed"), filename);
 
   fclose (f);
-  buffer [st.st_size] = '\n';
-  buffer [st.st_size + 1] = '\0';
+  buffer [size] = '\n';
+  buffer [size + 1] = '\0';
 
   line_count = 1;
 
@@ -1571,6 +1570,12 @@ copy_file (const char *input_filename, const char *output_filename,
   char **obj_matching;
   char **core_matching;
 
+  if (get_file_size (input_filename) < 1)
+    {
+      status = 1;
+      return;
+    }
+
   /* To allow us to do "strip *" without dying on the first
      non-object file, failures are nonfatal.  */
   ibfd = bfd_openr (input_filename, input_target);
@@ -2246,14 +2251,13 @@ strip_main (int argc, char *argv[])
       struct stat statbuf;
       char *tmpname;
 
+      if (get_file_size (argv[i]) < 1)
+       continue;
+
       if (preserve_dates)
-       {
-         if (stat (argv[i], &statbuf) < 0)
-           {
-             non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
-             continue;
-           }
-       }
+       /* No need to check the return value of stat().
+          It has already been checked in get_file_size().  */
+       stat (argv[i], &statbuf);
 
       if (output_file != NULL)
        tmpname = output_file;
@@ -2416,7 +2420,7 @@ copy_main (int argc, char *argv[])
        case OPTION_ADD_SECTION:
          {
            const char *s;
-           struct stat st;
+           off_t size;
            struct section_add *pa;
            int len;
            char *name;
@@ -2427,8 +2431,9 @@ copy_main (int argc, char *argv[])
            if (s == NULL)
              fatal (_("bad format for %s"), "--add-section");
 
-           if (stat (s + 1, & st) < 0)
-             fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
+           size = get_file_size (s + 1);
+           if (size < 1)
+             break;
 
            pa = xmalloc (sizeof (struct section_add));
 
@@ -2439,10 +2444,9 @@ copy_main (int argc, char *argv[])
            pa->name = name;
 
            pa->filename = s + 1;
+           pa->size = size;
+           pa->contents = xmalloc (size);
 
-           pa->size = st.st_size;
-
-           pa->contents = xmalloc (pa->size);
            f = fopen (pa->filename, FOPEN_RB);
 
            if (f == NULL)
@@ -2800,7 +2804,8 @@ copy_main (int argc, char *argv[])
 
   if (preserve_dates)
     if (stat (input_filename, & statbuf) < 0)
-      fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
+      fatal (_("warning: could not locate '%s'.  System error message: %s"),
+            input_filename, strerror (errno));
 
   /* If there is no destination file, or the source and destination files
      are the same, then create a temp and rename the result into the input.  */
index b1222d2d79b565093e3a67d76ed4425a7e2ebd14..723a28c0c5eea7fbc4f729b0f8887492c6c657b8 100644 (file)
@@ -2623,7 +2623,11 @@ display_bfd (bfd *abfd)
 static void
 display_file (char *filename, char *target)
 {
-  bfd *file, *arfile = NULL;
+  bfd *file;
+  bfd *arfile = NULL;
+
+  if (get_file_size (filename) < 1)
+    return;
 
   file = bfd_openr (filename, target);
   if (file == NULL)
index fb8d80f83739589368c3042465e756a8d481971f..9b8b12ee303619ef3be742a1dbae43a3aa4774b6 100644 (file)
@@ -10457,14 +10457,24 @@ process_file (char *file_name)
 
   if (stat (file_name, &statbuf) < 0)
     {
-      error (_("Cannot stat input file %s.\n"), file_name);
+      if (errno == ENOENT)
+       error (_("'%s': No such file\n"), file_name);
+      else
+       error (_("Could not locate '%s'.  System error message: %s\n"),
+              file_name, strerror (errno));
+      return 1;
+    }
+
+  if (! S_ISREG (statbuf.st_mode))
+    {
+      error (_("'%s' is not an ordinary file\n"), file_name);
       return 1;
     }
 
   file = fopen (file_name, "rb");
   if (file == NULL)
     {
-      error (_("Input file %s not found.\n"), file_name);
+      error (_("Input file '%s' is not readable.\n"), file_name);
       return 1;
     }
 
index 02f0972c2476034233c45e1fb51d7f8cbb0ab626..98754934a98b813d7b040d4fb43686382b0e6e38 100644 (file)
@@ -341,8 +341,12 @@ display_archive (bfd *file)
 static void
 display_file (char *filename)
 {
-  bfd *file = bfd_openr (filename, target);
+  bfd *file;
 
+  if (get_file_size (filename) < 1)
+    return;
+
+  file = bfd_openr (filename, target);
   if (file == NULL)
     {
       bfd_nonfatal (filename);
index 67c601f3b91b9ea16f94c937fa0c68e7ac37c915..68c244cafbd816b578bd008108e5fe811342559a 100644 (file)
@@ -370,6 +370,9 @@ strings_object_file (const char *file)
 static bfd_boolean
 strings_file (char *file)
 {
+  if (get_file_size (file) < 1)
+    return FALSE;
+
   /* If we weren't told to scan the whole file,
      try to open it as an object file and only look at
      initialized data sections.  If that fails, fall back to the
index 57cf9fffea996ca3deb69b271fcc68ab799158f8..fadb9c3e1d061b292fb6f12e266fb8167955de53 100644 (file)
@@ -1,3 +1,8 @@
+2003-11-07  Jonathan R. Grant  <jg-binutils@jguk.org>
+
+       * input-file.c (input_file_open): Use "No such file" error
+       message.
+
 2003-11-06  Pete Gonzalez  <pgonzalez@bluel.com>
 
        * config/tc-arm.texi (struct reg_entry): Add new field 'builtin'.
index b6c91ed5a52b66710344f7874099c215331523c3..8e73615706783f1f297614cb3cb1c866f46e91a7 100644 (file)
@@ -1,5 +1,5 @@
 /* input_file.c - Deal with Input Files -
-   Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001
+   Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2003
    Free Software Foundation, Inc.
 
    This file is part of GAS, the GNU Assembler.
@@ -26,6 +26,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <sys/stat.h>
 #include "as.h"
 #include "input-file.h"
 #include "safe-ctype.h"
@@ -135,15 +136,31 @@ input_file_open (filename, pre)
 
   assert (filename != 0);      /* Filename may not be NULL.  */
   if (filename[0])
-    {                          /* We have a file name. Suck it and see.  */
+    {
+      struct stat statbuf;
+
+      if (stat (filename, &statbuf) < 0)
+       {
+         as_bad (_("%s: No such file"), filename);
+         return;
+       }
+      else if (! S_ISREG (statbuf.st_mode))
+       {
+         as_bad (_("'%s' is not an ordinary file"), filename);
+         return;
+       }
+
       f_in = fopen (filename, FOPEN_RT);
       file_name = filename;
     }
   else
-    {                          /* use stdin for the input file.  */
+    {
+      /* Use stdin for the input file.  */
       f_in = stdin;
-      file_name = _("{standard input}");       /* For error messages.  */
+      /* For error messages.  */
+      file_name = _("{standard input}");
     }
+
   if (f_in == (FILE *) 0)
     {
       as_bad (_("can't open %s for reading"), file_name);
index db08d59cb3ab9b7e38ae8112f68257c565259c6a..8c96799d1d8cd1a81049d77907cc7dbc28d6bcb6 100644 (file)
@@ -1,3 +1,7 @@
+2003-11-07  Jonathan R. Grant  <jg-binutils@jguk.org>
+       
+       * ldfile.c (ldfile_open_file): Use "No such file" error message.
+
 2003-11-06  Bruno Rohee  <bruno@rohee.com>
 
        * ls.texinfo: Fix "the the" typo.
index cdec8ee7690ba260628269799608440b7cb7a948..cb0a3c384ae916e9ad7e02ec5a98a956a01eab25 100644 (file)
@@ -374,10 +374,10 @@ ldfile_open_file (lang_input_statement_type *entry)
       if (ldfile_try_open_bfd (entry->filename, entry))
        return;
       if (strcmp (entry->filename, entry->local_sym_name) != 0)
-       einfo (_("%F%P: cannot open %s for %s: %E\n"),
+       einfo (_("%F%P: %s (%s): No such file: %E\n"),
               entry->filename, entry->local_sym_name);
       else
-       einfo (_("%F%P: cannot open %s: %E\n"), entry->local_sym_name);
+       einfo (_("%F%P: %s: No such file: %E\n"), entry->local_sym_name);
     }
   else
     {