diff options
author | Karel Zak | 2012-01-31 14:31:09 +0100 |
---|---|---|
committer | Karel Zak | 2012-01-31 14:31:09 +0100 |
commit | 2af0a8390d824e811a176b0f63a13694889cfe9f (patch) | |
tree | 9d5db47b91b0b29f86f510bce542ca6154e7292e /libmount/src/utils.c | |
parent | login: more robust sysconf() usage [coverity scan] (diff) | |
download | kernel-qcow2-util-linux-2af0a8390d824e811a176b0f63a13694889cfe9f.tar.gz kernel-qcow2-util-linux-2af0a8390d824e811a176b0f63a13694889cfe9f.tar.xz kernel-qcow2-util-linux-2af0a8390d824e811a176b0f63a13694889cfe9f.zip |
libmount: consolidate sysconf() usage
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/src/utils.c')
-rw-r--r-- | libmount/src/utils.c | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 823289e5f..c7a1fd177 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -467,6 +467,16 @@ int mnt_get_filesystems(char ***filesystems, const char *pattern) return get_filesystems(_PATH_PROC_FILESYSTEMS, filesystems, pattern); } +static size_t get_pw_record_size(void) +{ +#ifdef _SC_GETPW_R_SIZE_MAX + long sz = sysconf(_SC_GETPW_R_SIZE_MAX); + if (sz > 0) + return sz; +#endif + return 16384; +} + /* * Returns allocated string with username or NULL. */ @@ -474,16 +484,9 @@ char *mnt_get_username(const uid_t uid) { struct passwd pwd; struct passwd *res; -#ifdef _SC_GETPW_R_SIZE_MAX - size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); -#else - size_t sz = 0; -#endif + size_t sz = get_pw_record_size(); char *buf, *username = NULL; - if (sz <= 0) - sz = 16384; /* Should be more than enough */ - buf = malloc(sz); if (!buf) return NULL; @@ -500,15 +503,12 @@ int mnt_get_uid(const char *username, uid_t *uid) int rc = -1; struct passwd pwd; struct passwd *pw; - size_t sz; - long xsz = sysconf(_SC_GETPW_R_SIZE_MAX); + size_t sz = get_pw_record_size(); char *buf; if (!username || !uid) return -EINVAL; - sz = xsz <= 0 ? 16384 : (size_t) xsz; - buf = malloc(sz); if (!buf) return -ENOMEM; @@ -530,15 +530,12 @@ int mnt_get_gid(const char *groupname, gid_t *gid) int rc = -1; struct group grp; struct group *gr; - size_t sz; - long xsz = sysconf(_SC_GETGR_R_SIZE_MAX); + size_t sz = get_pw_record_size(); char *buf; if (!groupname || !gid) return -EINVAL; - sz = xsz <= 0 ? 16384 : (size_t) xsz; - buf = malloc(sz); if (!buf) return -ENOMEM; |