From patchwork Thu Jun 8 21:13:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 103421 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633164qgd; Thu, 8 Jun 2017 14:16:58 -0700 (PDT) X-Received: by 10.98.76.140 with SMTP id e12mr38083815pfj.78.1496956618787; Thu, 08 Jun 2017 14:16:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956618; cv=none; d=google.com; s=arc-20160816; b=ho2A2W4fRLWXSgrTVRaOIvru5Dl2O7O6r5RRB6RbaVy21YuVCqw2eFTscqNQd5fY7W VEsHhVYT5pGTo7aazXbXze4SbJL8IsJQluOmBZe43MF+gjeDElr9TXOS+h8eJnnaAMWj Mz8TjF9tLKxEP+2RzsH/X208bp1XSNgnx0z592yQDIcJKGBkRsMP0THjYqz2zf6dip7z hpemVokP1LEvuotdHudGcGQB/S4pNexgwT65Oc1YB+xiYks/PhyZfzrSFCEICU3U8YVx EtRQuh3FCPMGNYu2A2+jfdYVYaQ5+gpi/84HzYDR0epRpxD2ItPVStNKM46ChIT10ais bL8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=EWZEnxDhCmAcQZ+KkeTp1qsH0In9i8igrDNbCu97hsc=; b=gbRagYD/SwYMmfMU7mTomviHee4nWDEyEswP/sy02owxbNPJXxoOEEUYixMYAOjLKW lCWRk0MjbhjsB9soAKV6+rAf7yYyJU2j3By/frhTquoa3JjqtOFMhWMEUzifqWXl/j8b WKY727tSiL1nYRfBjsegoBQxV/yzIRsmS5XH3BRxyHqVoYpaeHnHHYPmMjJxLmBet7Fr s5fa4p8rzfhXXURMsn+Mr+f6ftG/OBXlTZnEwUqokZoRIkaiVrnjx6OG58k6tFblmAmG zuRu/UY7iksAeKUUen6QnzoKQoGVFT4srkk3zQp6R9azTuMB2NO/jpnjyjj7Tg6J/elS h+NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80215-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80215-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id r90si2909961pfk.14.2017.06.08.14.16.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:58 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80215-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80215-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80215-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=cuxJ6jt3ggnlymBLxBLRrcWQVxaYKmw A6eDV4fToHG6vI66XH+o/A8mChqiLjdsrD2H5r+0B7FjzcElpuSWRycEn+UoHpC8 6XcFPUFpRmAnahvbkSTIp9mhgbNrdl43pueLHBM1YYIt6pIGKUDhZakOF815HVTH fUwHVsLiB3JY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=4kgH+LovoMsbwCZVSmNQrt1Q5+g=; b=HpG4b wokeK2GAcv+YD3uTl/pHdKe8UH0ie5rennOh1QijhDFgJ7A+4g5Z5tJvPKR7m7q8 2zCggbTDHlIa+BEptMlEkheY5V8yF/OSwrSG07aIA43nSdQBu/jlnLas7a28l44G t4rN9PdRW4YXnIl4ehSNg2dpNIV0Z0P3pokV4I= Received: (qmail 41825 invoked by alias); 8 Jun 2017 21:14:29 -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 41119 invoked by uid 89); 8 Jun 2017 21:14:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f173.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=EWZEnxDhCmAcQZ+KkeTp1qsH0In9i8igrDNbCu97hsc=; b=aLk1GMSCYSQKDcDxEZpSndUr2RWD3/Kt4/MPho1haeVbCvl4XmjnHexJhYaCl5JaG1 XD76uMpx7D1m1XXVokAZyMrAgOwu+L5G96T/KVwHyki2wNfiSxxmhyrZ7WDit71THUwr 2uqTtsSHbU1kfjrSW+2Im9hZ9xTPQVDHNQPusWzideWzxK3HN15BWleUMisd5PKTtDuJ 5cUvkNFHBzRrjWoX0b1DpTxEoX8mt1cH4o8CGccMq1iV6YI92dh/oEINNMKIwlXAUX6V XgY6J1YB8GxQp5F4I4aycX6bsBEFewqdiPkuNkM/oMlra2j/eu4PDtznmembwyAyfFEU ZfMg== X-Gm-Message-State: AODbwcBEPKUeDdSckmyIVkF2ZM8Y31D68iXvGPACj+qgYzhxwdOHHczD yaaN8LFgM2E6Rz6oLX0VGA== X-Received: by 10.237.44.7 with SMTP id f7mr21279305qtd.142.1496956461641; Thu, 08 Jun 2017 14:14:21 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 16/17] posix: Add common function to get home directory Date: Thu, 8 Jun 2017 18:13:30 -0300 Message-Id: <1496956411-25594-17-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-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(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 4cefdca..2b11e45 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -264,6 +264,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. @@ -649,38 +687,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)) @@ -693,10 +701,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 */ @@ -777,59 +782,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. */ }