From: Nick Clifton Date: Fri, 7 Nov 2003 12:19:34 +0000 (+0000) Subject: Use consistent error messages for missing files. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f24ddbddc5177a930b867626b12d2bb8b3757223;p=binutils-gdb.git Use consistent error messages for missing files. Detect directories where an ordinary file is expected. --- diff --git a/binutils/ChangeLog b/binutils/ChangeLog index f683369a9e5..315a4d4229c 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,18 @@ +2003-11-07 Jonathan R. Grant + + * 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 * ieee.c: Fix "the the" typo. diff --git a/binutils/addr2line.c b/binutils/addr2line.c index b5f5a0c5f15..354153eadb1 100644 --- a/binutils/addr2line.c +++ b/binutils/addr2line.c @@ -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); diff --git a/binutils/ar.c b/binutils/ar.c index 1724171644d..77fa4673977 100644 --- a/binutils/ar.c +++ b/binutils/ar.c @@ -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) { diff --git a/binutils/bucomm.c b/binutils/bucomm.c index a6bb6e483f9..6573e2d9c7f 100644 --- a/binutils/bucomm.c +++ b/binutils/bucomm.c @@ -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; +} diff --git a/binutils/bucomm.h b/binutils/bucomm.h index 9b17791cd52..f604053cec8 100644 --- a/binutils/bucomm.h +++ b/binutils/bucomm.h @@ -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 */ diff --git a/binutils/nm.c b/binutils/nm.c index d2d38c4b54a..bac7d388ce2 100644 --- a/binutils/nm.c +++ b/binutils/nm.c @@ -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) { diff --git a/binutils/objcopy.c b/binutils/objcopy.c index 8a1a67a74ef..8796dd6e020 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -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. */ diff --git a/binutils/objdump.c b/binutils/objdump.c index b1222d2d79b..723a28c0c5e 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -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) diff --git a/binutils/readelf.c b/binutils/readelf.c index fb8d80f8373..9b8b12ee303 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -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; } diff --git a/binutils/size.c b/binutils/size.c index 02f0972c247..98754934a98 100644 --- a/binutils/size.c +++ b/binutils/size.c @@ -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); diff --git a/binutils/strings.c b/binutils/strings.c index 67c601f3b91..68c244cafbd 100644 --- a/binutils/strings.c +++ b/binutils/strings.c @@ -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 diff --git a/gas/ChangeLog b/gas/ChangeLog index 57cf9fffea9..fadb9c3e1d0 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,3 +1,8 @@ +2003-11-07 Jonathan R. Grant + + * input-file.c (input_file_open): Use "No such file" error + message. + 2003-11-06 Pete Gonzalez * config/tc-arm.texi (struct reg_entry): Add new field 'builtin'. diff --git a/gas/input-file.c b/gas/input-file.c index b6c91ed5a52..8e736157067 100644 --- a/gas/input-file.c +++ b/gas/input-file.c @@ -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 #include +#include #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); diff --git a/ld/ChangeLog b/ld/ChangeLog index db08d59cb3a..8c96799d1d8 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,7 @@ +2003-11-07 Jonathan R. Grant + + * ldfile.c (ldfile_open_file): Use "No such file" error message. + 2003-11-06 Bruno Rohee * ls.texinfo: Fix "the the" typo. diff --git a/ld/ldfile.c b/ld/ldfile.c index cdec8ee7690..cb0a3c384ae 100644 --- a/ld/ldfile.c +++ b/ld/ldfile.c @@ -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 {