mirror of
https://github.com/nmap/nmap.git
synced 2026-01-22 22:29:03 +00:00
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.
This commit is contained in:
14
nmap.cc
14
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;
|
||||
|
||||
Reference in New Issue
Block a user