diff options
author | Karel Zak | 2019-05-23 13:53:00 +0200 |
---|---|---|
committer | Karel Zak | 2019-05-23 13:53:00 +0200 |
commit | a2761af42be7a8a9e611e3188e58b6340c822282 (patch) | |
tree | 6c470c59debe111491fe41ac067fbb930e5709e6 /misc-utils | |
parent | lsblk: make sure __process_one_device() has proper arguments [coverity scan] (diff) | |
download | kernel-qcow2-util-linux-a2761af42be7a8a9e611e3188e58b6340c822282.tar.gz kernel-qcow2-util-linux-a2761af42be7a8a9e611e3188e58b6340c822282.tar.xz kernel-qcow2-util-linux-a2761af42be7a8a9e611e3188e58b6340c822282.zip |
whereis: make subdirs scan more robust
* call strchr() only once
* avoid things like strcat(buf, strchr(dir, '*') + 1)
* make it more readable
* improve debug messages
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'misc-utils')
-rw-r--r-- | misc-utils/whereis.c | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c index debd5da55..48361f598 100644 --- a/misc-utils/whereis.c +++ b/misc-utils/whereis.c @@ -263,30 +263,47 @@ static void dirlist_add_subdir(struct wh_dirlist **ls, int type, const char *dir char buf[PATH_MAX], *d; DIR *dirp; struct dirent *dp; + char *postfix; + size_t len; - xstrncpy(buf, dir, PATH_MAX); + postfix = strchr(dir, '*'); + if (!postfix) + goto ignore; - d = strchr(buf, '*'); - if (!d) - return; - *d = 0; + /* copy begin of the path to the buffer (part before '*') */ + len = (postfix - dir) + 1; + xstrncpy(buf, dir, len); + + /* remember place where to append subdirs */ + d = buf + len - 1; + + /* skip '*' */ + postfix++; + if (!*postfix) + postfix = NULL; + /* open parental dir t scan */ dirp = opendir(buf); if (!dirp) - return; + goto ignore; - DBG(LIST, ul_debugobj(*ls, " scanning subdir: %s", dir)); + DBG(LIST, ul_debugobj(*ls, " scanning subdirs: %s [%s<subdir>%s]", + dir, buf, postfix ? postfix : "")); while ((dp = readdir(dirp)) != NULL) { if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; - snprintf(d, PATH_MAX - (d - buf), "%s", dp->d_name); - /* a dir definition can have a star in middle of path */ - strcat(buf, strchr(dir, '*') + 1); + if (postfix) + snprintf(d, PATH_MAX - len, "%s%s", dp->d_name, postfix); + else + snprintf(d, PATH_MAX - len, "%s", dp->d_name); + dirlist_add_dir(ls, type, buf); } closedir(dirp); return; +ignore: + DBG(LIST, ul_debugobj(*ls, " ignore path: %s", dir)); } static void construct_dirlist_from_env(const char *env, |