From patchwork Fri Aug 11 14:50:40 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: 109897 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp977956obb; Fri, 11 Aug 2017 07:54:13 -0700 (PDT) X-Received: by 10.84.169.67 with SMTP id g61mr17916452plb.109.1502463253245; Fri, 11 Aug 2017 07:54:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502463253; cv=none; d=google.com; s=arc-20160816; b=z7LiwVPEqbleKOfHJavvBWbDt5fY/jKZkYHgmQcUzeMThcm5JnUbAKC0GGU7y7l50S BQxOz+gRzeYCWfNMre/z0d3AqsNYR0J0vW5fznTDDRI6+3/Jc/2cSOjXVUb67FoTCu1b sl7PkqE9e893s02BzYcTNiKePSPyy1lq7CriWd1HIh40V2ZQigPKLIgmUvJsMAKgyG7A nDPScx5FWfBoBXAc2r8y4nWWzpJF/MRQtategRNl3fxwQgvcBCAt+00bvCYHGySFiClU I1yXvHgilM2u14enwV2tvLQ5VbFRd53z3mjJdvXXe4/oI9kKQVLQckVbcO4yhmGfZcW6 hDuQ== 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=JVXvrzwfrGBOGG38Iy/tvIAL95guDZt9+MofFKH6iU0=; b=rtdQ1oKaxfO7hlbXO1rkEWVvVvJ87ZGJpyEp/RSRwbh2/TUYDA7uEmgIkrzrizMjNq s0cdZmw4QnSLykWpL3eMhGQRHZ9WzGaV3x0ShH+L+u17WPcPB6PSOt07DwQ8yeozpOG5 dSVjwUjiqcYm3oxiVus0E2e5o02px5gDodjzQqT0fte+1u9iJuoqKeCy3x65dTSBTOfV AatR4gWNitIzkKUB/hyWZYCimL1nlSHVTUHRMvG/TPc0soykfVL6hr9JfOXRA6ZUonAa FdNPLQvFpMxtqSepPDjdANWZ+slw9melyTvxKXFUL+TqWmJCeGx9z7ov1nZb66wtIgwU PzrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=lwrnU5On; spf=pass (google.com: domain of libc-alpha-return-83039-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83039-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 u67si583238pgb.573.2017.08.11.07.54.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Aug 2017 07:54:13 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83039-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=lwrnU5On; spf=pass (google.com: domain of libc-alpha-return-83039-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83039-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=GZYp4B+JXzEEYC4xK9uGQ25AtimgSGV YhJ+wpEcMQhQM6XiLF/qVVgklT527DFW8VKGaON93YwFg0AcBhV1n+DS+lT73ihk ZeLw8Rws6Vbwxw+X12pefg1ALU0UiGFyC3B1l+k1H1FS/WCZ24EGcSWitafZ+w6K K0D4KS1EfZh4= 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=r3Jhyi9mIK18c63aBsQza9xwAV4=; b=lwrnU 5OnQspKeZUq3ndXoIs5Gbf1OEVn64VJFQ4sMqktZ6B0R1ryie5E9PJSG2cMdSxgP LTJH8HeMSW+sZtYBWqbV8PVN67qMaVhNwtHpRMKQcWXgfyhLUXpfyB4/iepd5UU8 GYzozSok2wh9fr5kU9Dsc1Eq/lMxQRLT4wGHOY= Received: (qmail 90609 invoked by alias); 11 Aug 2017 14:51:34 -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 80468 invoked by uid 89); 11 Aug 2017 14:51:18 -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=Hx-languages-length:2978 X-HELO: mail-qt0-f177.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=JVXvrzwfrGBOGG38Iy/tvIAL95guDZt9+MofFKH6iU0=; b=U8lme8Q3HHSIXyw41+eHSVLihUjRCCJxBRJqdXWkDWLWp6hVqpUDr68JDBpc/IoGbm F/xAHpP0hLt6Hcz2EnUwITDQNrhx67fsNxem0WX5E76Xgp+njA55kp4N3/MlgbnyiroI PLPMsdTe2xZXJeRkYmK3rWR1MxsawD+5ReHrXCMUIOihPLLuhR6ftjQR7VBmlZaMeBW4 ebdXpsNs9U00FgT4yVU2VRFm8QjAoBGTOx0TEgiiPFfxv3azsBSAm267izXTT33tx2fB VNziC0h+pmxrUkN/gOMxHxOH92YYNs0BaopJ1CyTtbZcPIGwVfCf7R/BqrwsmyQEl85G iUcA== X-Gm-Message-State: AHYfb5gY5/oNZRSqihI8vkRUHnQYZBxug42kfQoLjZm4B9dKIjIiDb9m IvKV9Rhs7d6WMrMpnikBWw== X-Received: by 10.200.48.232 with SMTP id w37mr20359276qta.186.1502463069562; Fri, 11 Aug 2017 07:51:09 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 14/18] posix: Use char_array for home_dir in glob Date: Fri, 11 Aug 2017 11:50:40 -0300 Message-Id: <1502463044-4042-15-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 uses char_array for home directory discovery. It simplifies the buffer management. Checked on x86_64-linux-gnu. * posix/glob.c (glob): Use char_array for home directory. --- posix/glob.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 67196fd..d2cb871 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -607,8 +607,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), || char_array_pos (&dirname, 2) == '/'))) { /* Look up home directory. */ - char *home_dir = getenv ("HOME"); - int malloc_home_dir = 0; + struct char_array home_dir; + + const char *home_env = getenv ("HOME"); + home_env = home_env == NULL ? "" : home_env; + if (!char_array_init_str (&home_dir, home_env)) + { + retval = GLOB_NOSPACE; + goto out; + } # ifdef _AMIGA if (home_dir == NULL || home_dir[0] == '\0') home_dir = "SYS:"; @@ -635,7 +642,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), home_dir = "c:/users/default"; /* poor default */ } # else - if (home_dir == NULL || home_dir[0] == '\0') + if (char_array_is_empty (&home_dir)) { int success; char user_name[LOGIN_NAME_MAX]; @@ -668,9 +675,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), # endif if (p != NULL) { - home_dir = strdup (p->pw_dir); - malloc_home_dir = 1; - if (home_dir == NULL) + if (!char_array_set_str (&home_dir, p->pw_dir)) { scratch_buffer_free (&pwtmpbuf); goto err_nospace; @@ -679,10 +684,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), scratch_buffer_free (&pwtmpbuf); } } - if (home_dir == NULL || home_dir[0] == '\0') + if (char_array_is_empty (&home_dir)) { - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); if (flags & GLOB_TILDE_CHECK) { retval = GLOB_NOMATCH; @@ -690,8 +693,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } else { - home_dir = (char *) "~"; /* No luck. */ - malloc_home_dir = 0; + if (!char_array_set_str (&home_dir, "~")) + { + retval = GLOB_NOSPACE; + goto out; + } } } # endif /* WINDOWS32 */ @@ -699,7 +705,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Now construct the full directory. */ if (char_array_pos (&dirname, 1) == '\0') { - if (!char_array_set_str (&dirname, home_dir)) + if (!char_array_set_str (&dirname, char_array_str (&home_dir))) goto err_nospace; dirlen = char_array_size (&dirname) - 1; } @@ -707,9 +713,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { /* Replaces '~' by the obtained HOME dir. */ char_array_erase (&dirname, 0); - if (!char_array_prepend_str (&dirname, home_dir)) + if (!char_array_prepend_str (&dirname, + char_array_str (&home_dir))) goto err_nospace; } + char_array_free (&home_dir); dirname_modified = true; } # if !defined _AMIGA && !defined WINDOWS32