openp: Report a proper error message when attempting to open a directory or special file

Message ID CAO4GU+D=mwWQ-=yh6gSQkGPZiMnogFaJT9s9E+2TaYb39uwdyQ@mail.gmail.com
State New, archived
Headers

Commit Message

Nicolai Hähnle-Montoro Feb. 12, 2015, 11:04 a.m. UTC
  I have attached this patch also to bug #17911, but sending it here
also since it may be the more relevant place.

Cheers,
Nicolai
  

Patch

commit 5238e6871515e591feb93cbc90df2b64b4ab67c9
Author: Nicolai Hähnle <haehnle@or.uni-bonn.de>
Date:   Sat Jan 31 15:21:14 2015 +0100

    openp: Report a proper error message when attempting to open a directory or special file

diff --git a/gdb/source.c b/gdb/source.c
index 14b1f71..20e2531 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -679,7 +679,9 @@  source_info (char *ignore, int from_tty)
 }
 
 
-/* Return True if the file NAME exists and is a regular file.  */
+/* Return True if the file NAME exists and is a regular file.
+
+   Ensures that errno is set to an appropriate value. */
 static int
 is_regular_file (const char *name)
 {
@@ -694,7 +696,18 @@  is_regular_file (const char *name)
   if (status != 0)
     return (errno != ENOENT);
 
-  return S_ISREG (st.st_mode);
+  if (S_ISDIR (st.st_mode))
+    {
+      errno = EISDIR;
+      return 0;
+    }
+  else if (!S_ISREG (st.st_mode))
+    {
+      errno = ENOEXEC;
+      return 0;
+    }
+  else
+    return 1;
 }
 
 /* Open a file named STRING, searching path PATH (dir names sep by some char)