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: 109899 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp978375obb; Fri, 11 Aug 2017 07:54:37 -0700 (PDT) X-Received: by 10.98.87.77 with SMTP id l74mr16384070pfb.198.1502463277066; Fri, 11 Aug 2017 07:54:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502463277; cv=none; d=google.com; s=arc-20160816; b=IDl8mG7xhe1EfiZNOua1ej0TyveyLGRlE/DxIiDQAZI93b9k9jmicbbErKi+wsE1CQ QjeDxYD6fs4SMNxKP6cEkaRDkw6m4HQkZsucifKq/Szheua92dCtXwTGilAvnBWSuMp1 0V75BH2A5jAdR8AicjcbybT4QjstO9MPrlNQzJH3KQt35vrfz3IixlOb2DFdTvizx/H+ CU8E7fH3U6swsur9piAAdtBeGuegacHGJyZRh3DIoBQZVM61AO7w2B6YWLxWBFpkNlbi Gu5GtWvp9gFISe5S6sQjYfkz8LP3NdZ0USv/sUu9yffGFlExwYDx/sAlaSznxfk9MqrJ dLZg== 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=PxJa3wnJe1egAqoX2ILbavWRINaBBRTEDgXNmNTfMfM=; b=wkE42Vn4P96NvmOXqzZysLX8PDSoEhPuoPWx7BuBBhjcbEtQeiBmrSiqthq9TRa4Yx Z/SzUzqwftKGLjvWKsBGulnS8sYTIb/enY0pgXBXYYpdYFFIOVz+MuEBTSfBakpyN37w +Xhn6kyUhx1qO15nKsyQeTU5YKnAkEUdd/VyrX/LNrDsjn5Xzmpthx35ivwbrGmHxBui QmFw4qgj9PEZJaBac3K6X3ZYSz8+GczBfMHHnmJrOr/35t1e+14fzO8bf6mhD+SxYIWP uXR86e4S+99UDccUQrwGRYTosqr5RjCEFkGjfnBizBf1ATjNtft/jk2nR3CnBSCZf7db a1EA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Z09DdlXH; spf=pass (google.com: domain of libc-alpha-return-83041-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83041-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 m193si628993pfc.112.2017.08.11.07.54.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Aug 2017 07:54:37 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83041-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 header.s=default header.b=Z09DdlXH; spf=pass (google.com: domain of libc-alpha-return-83041-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83041-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=Dz3HXUxJSbaPuitXjLzsaA/y9OFOC1i KenBEWcIvzAQxdk0jgVcspXVT/8WmFcFJJepctVe0q7607tNsyR0iakwcXm7FgnL 2I0WfcnRdSUtwMg2Z+KAFzniG8ksjvSdY+ewTyl9vqTtBpb3an/rKNX27UK8/bDP MRupnBS7UwI0= 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=0sFoY4nNhjYqR21za0Ptk8hJetw=; b=Z09Dd lXHHguFIm8LJ5sRwKaJ4YHAcpOidl7u73/FyJTCCQ6hkovA97hIo6rZj5oXJ6Cox Nrkn+3YlVwzkC2yIG10NlEhOojUzaSET1Sl6x6tPv3xgKpCXGX6A7zuHBbh4v+gQ nq7OshUzkRy5mMuocnj5igyWdm1Xz9wxMBuVb0= 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(-) -- 2.7.4 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. */ }