chcp: Use macro S_ISDIR() to test type of file

Testing whether a file is a directory by comparing the struct stat
field st_mode against S_IFDIR is wrong. If st_mode has any access
permission bits set along with the file type code the test will always
fail.

The file type encoded in the struct stat field st_mode is actually an
enumeration. To test whether a file is a directory either extract the
file type from st_mode using the mask S_IFMT and compare it against
S_IFDIR or simply use the macro S_ISDIR().

Fixes: b627b8d8e1 ("Initial s390-tools-2.0.0 import")
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jens Remus
2019-06-07 14:17:13 +02:00
committed by Jan Höppner
parent 8d1a7fecd0
commit 39520546c5

View File

@@ -3,7 +3,7 @@
*
* Provide main function and command line parsing.
*
* Copyright IBM Corp. 2016, 2017
* Copyright IBM Corp. 2016, 2019
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
@@ -236,7 +236,7 @@ static char *get_chp_dir(int css, int id)
char *path;
path = util_path_sysfs("devices/css%x/chp%x.%x", css, css, id);
if ((stat(path, &sb) == 0) && (sb.st_mode == S_IFDIR))
if ((stat(path, &sb) == 0) && S_ISDIR(sb.st_mode))
return path;
free(path);
return util_path_sysfs("devices/css%x/chp%x.%02x", css, css, id);
@@ -296,7 +296,7 @@ static void perform_command(int css, int id)
char *path;
path = get_chp_dir(css, id);
if ((stat(path, &sb) != 0) || ((sb.st_mode & S_IFMT) != S_IFDIR)) {
if ((stat(path, &sb) != 0) || !S_ISDIR(sb.st_mode)) {
printf("Skipping unknown channel-path %x.%02x\n", css, id);
goto out_free_path;
}