From patchwork Fri Aug 11 14:50:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 22098 Received: (qmail 91403 invoked by alias); 11 Aug 2017 14:51:36 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 79945 invoked by uid 89); 11 Aug 2017 14:51:17 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=Obtain X-HELO: mail-qk0-f174.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=PxJa3wnJe1egAqoX2ILbavWRINaBBRTEDgXNmNTfMfM=; b=cN7hhqoO/1hCZ5wb9aQkFsqaG4SOsLnycwtj9mdtmy/o2GzzEhVRiBO/JqDjPUDZjx pqWW/LtAjcLciJzFOKGVw4HL63U7MvAE6uR+uOhnvEJxswRAsWGtEdaKJxTdwSzHQnYX WB0+vBkzEDjs6j0letm17JPyWtZL2M0SYBoGu+byyPTRaoy2EM9zJHtf47dc+zYW5tgO X/txt3nhooBwuihMuB5demS6P8JZbJ9/5kULd9zi1GmpJIGJrO1xDODM8uPfAWzJe31y 35VLXF7g9Ecw0ih6H+W3fjYuSDXhOuMS+0aoT4JvixgEX0r4kt2P9w0TMfjh3CjPfT/C nwjQ== X-Gm-Message-State: AHYfb5iy0PEBo+V/+zzAT9gVyYWQ3sk+66KIQ0+/gqvgFwjVhTSVhJEF BmG2PuIgxwHkItq3TiUqGw== X-Received: by 10.233.235.19 with SMTP id b19mr10686681qkg.77.1502463070974; Fri, 11 Aug 2017 07:51:10 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 15/18] posix: Add common function to get home directory Date: Fri, 11 Aug 2017 11:50:41 -0300 Message-Id: <1502463044-4042-16-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1502463044-4042-1-git-send-email-adhemerval.zanella@linaro.org> References: <1502463044-4042-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds a common function to get the full home directory from a user. No functional changes expected. Checked on x86_64-linux-gnu. * posix/glob.c (get_home_directory): New function. (glob): Use get_home_directory. --- posix/glob.c | 145 +++++++++++++++++++++++------------------------------------ 1 file changed, 56 insertions(+), 89 deletions(-) diff --git a/posix/glob.c b/posix/glob.c index d2cb871..c85342a 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -265,6 +265,44 @@ next_brace_sub (const char *cp, int flags) return *cp != '\0' ? cp : NULL; } +/* Obtain the full home directory path from user 'user_name' and writes it + on char_array 'home_dir'. */ +static bool +get_home_directory (const char *user_name, struct char_array *home_dir) +{ + struct passwd *p; +#if defined HAVE_GETPWNAM_R || defined _LIBC + struct passwd pwbuf; + int save = errno; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&pwtmpbuf); + + while (getpwnam_r (user_name, &pwbuf, pwtmpbuf.data, pwtmpbuf.length, &p) + != 0) + { + if (errno != ERANGE) + { + p = NULL; + break; + } + if (!scratch_buffer_grow (&pwtmpbuf)) + return false; + __set_errno (save); + } +#else + p = getpwnam (pwtmpbuf.data); +#endif + + bool retval = false; + if (p != NULL) + { + if (char_array_set_str (home_dir, p->pw_dir)) + retval = true; + } + scratch_buffer_free (&pwtmpbuf); + return retval; +} + /* Do glob searching for PATTERN, placing results in PGLOB. The bits defined above may be set in FLAGS. @@ -650,38 +688,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), success = __getlogin_r (user_name, sizeof (user_name)) == 0; if (success) { - struct passwd *p; -# if defined HAVE_GETPWNAM_R || defined _LIBC - struct passwd pwbuf; - int save = errno; - struct scratch_buffer pwtmpbuf; - scratch_buffer_init (&pwtmpbuf); - - while (getpwnam_r (user_name, &pwbuf, - pwtmpbuf.data, pwtmpbuf.length, &p) - != 0) - { - if (errno != ERANGE) - { - p = NULL; - break; - } - if (!scratch_buffer_grow (&pwtmpbuf)) - goto err_nospace; - __set_errno (save); - } -# else - p = getpwnam (pwtmpbuf.data); -# endif - if (p != NULL) - { - if (!char_array_set_str (&home_dir, p->pw_dir)) - { - scratch_buffer_free (&pwtmpbuf); - goto err_nospace; - } - } - scratch_buffer_free (&pwtmpbuf); + if (!get_home_directory (user_name, &home_dir)) + goto err_nospace; } } if (char_array_is_empty (&home_dir)) @@ -694,10 +702,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), else { if (!char_array_set_str (&home_dir, "~")) - { - retval = GLOB_NOSPACE; - goto out; - } + goto err_nospace; } } # endif /* WINDOWS32 */ @@ -778,59 +783,21 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } /* Look up specific user's home directory. */ - { - struct passwd *p; - struct scratch_buffer pwtmpbuf; - scratch_buffer_init (&pwtmpbuf); - -# if defined HAVE_GETPWNAM_R || defined _LIBC - struct passwd pwbuf; - int save = errno; - - while (getpwnam_r (user_name, &pwbuf, - pwtmpbuf.data, pwtmpbuf.length, &p) != 0) - { - if (errno != ERANGE) - { - p = NULL; - break; - } - if (!scratch_buffer_grow (&pwtmpbuf)) - { - retval = GLOB_NOSPACE; - goto out; - } - __set_errno (save); - } -# else - p = getpwnam (user_name); -# endif - - /* If we found a home directory use this. */ - if (p != NULL) - { - if (!char_array_set_str (&dirname, p->pw_dir)) - { - scratch_buffer_free (&pwtmpbuf); - retval = GLOB_NOSPACE; - goto out; - } - - dirlen = strlen (p->pw_dir); - dirname_modified = true; - } - else - { - if (flags & GLOB_TILDE_CHECK) - { - /* We have to regard it as an error if we cannot find the - home directory. */ - retval = GLOB_NOMATCH; - goto out; - } - } - scratch_buffer_free (&pwtmpbuf); - } + if (get_home_directory (user_name, &dirname)) + { + dirlen = char_array_size (&dirname) - 1; + dirname_modified = true; + } + else + { + if (flags & GLOB_TILDE_CHECK) + /* We have to regard it as an error if we cannot find the + home directory. */ + { + retval = GLOB_NOMATCH; + goto out; + } + } } # endif /* Not Amiga && not WINDOWS32. */ }