]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: consolidate sysconf() usage
authorKarel Zak <kzak@redhat.com>
Tue, 31 Jan 2012 13:31:09 +0000 (14:31 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 31 Jan 2012 13:31:09 +0000 (14:31 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/utils.c

index 823289e5f926f8958de2f934aa22fb560f469b3b..c7a1fd17746cd554b4f5bcc2d908bb99dbf2f4d0 100644 (file)
@@ -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;