From 4e4924b40800f36a0afa1d640e1bebe927166363 Mon Sep 17 00:00:00 2001 From: kris Date: Mon, 15 Jan 2007 18:24:47 +0000 Subject: [PATCH] A few changes to fileexistsandisreadable() (I can't leave this thing alone, can I? :)). First, 'status' is initialized to 0 instead of -1 because if stat() succeeds, but 'pathname_buf' isn't readable at all, this function was returning that -1 (which it shouldn't because the comment says 0, and nmap_fetchfile() checks for a non-zero return value and assumes it found something, but it wasn't breaking anything). Also, access() is now only called once and then the directory-check is done. And finally the comment now explicitly states that 1 is returned if it's readable and not a directory instead of just non-zero. This also just uses S_ISDIR() for testing for a directory, so it might actually be a portability enhancement because WIN32 apparently doesn't have S_ISDIR() and in nbase.h it's defined to something different just than ANDing with S_IFDIR. --- nmap.cc | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/nmap.cc b/nmap.cc index ad7b53475..5c3e1584a 100644 --- a/nmap.cc +++ b/nmap.cc @@ -2325,14 +2325,14 @@ void sigdie(int signo) { exit(1); } -/* Returns true (nonzero) if the file pathname given exists, is not a - * directory and is readable by the executing process. Returns two if - * it is readable and is a directory. Otherwise returns 0. +/* Returns one if the file pathname given exists, is not a directory and + * is readable by the executing process. Returns two if it is readable + * and is a directory. Otherwise returns 0. */ int fileexistsandisreadable(char *pathname) { char *pathname_buf = strdup(pathname); - int status = -1; + int status = 0; #ifdef WIN32 // stat on windows only works for "dir_name" not for "dir_name/" or "dir_name\\" @@ -2349,10 +2349,8 @@ int fileexistsandisreadable(char *pathname) { if (stat(pathname_buf, &st) == -1) status = 0; - else if (!S_ISDIR(st.st_mode) && (access(pathname_buf, R_OK) != -1)) - status = 1; - else if ((st.st_mode & S_IFDIR) && (access(pathname_buf, R_OK) != -1)) - status = 2; + else if (access(pathname_buf, R_OK) != -1) + status = S_ISDIR(st.st_mode) ? 2 : 1; free(pathname_buf); return status;