From 39520546c5678bccc1505612787050cc47cdc745 Mon Sep 17 00:00:00 2001 From: Jens Remus Date: Fri, 7 Jun 2019 14:17:13 +0200 Subject: [PATCH] chcp: Use macro S_ISDIR() to test type of file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: b627b8d8e1ab ("Initial s390-tools-2.0.0 import") Signed-off-by: Jens Remus Reviewed-by: Peter Oberparleiter Signed-off-by: Jan Höppner --- zconf/chp/chchp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zconf/chp/chchp.c b/zconf/chp/chchp.c index d7e7af24..01ef4554 100644 --- a/zconf/chp/chchp.c +++ b/zconf/chp/chchp.c @@ -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; }