From patchwork Thu Jun 8 21:13:29 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: 103420 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633123qgd; Thu, 8 Jun 2017 14:16:48 -0700 (PDT) X-Received: by 10.84.208.102 with SMTP id f35mr34041811plh.53.1496956608588; Thu, 08 Jun 2017 14:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956608; cv=none; d=google.com; s=arc-20160816; b=ar79FJbBskm3zlFfTavc3HRyKmoW7bUYlmTdpXNYTXLPVmNJzUd5X9LGUud6mE+rp0 1+pkTt/i+gJr+yuz15HFFgxlIKxDKCSnRnkf0u/9zm7pT/3+ApYlBbNj0+n0gsEQHRWq CrVhOG5wWSpttNnu4HqXCkXTHh3riPQBooEPDZXZid8LSRnC/xXNqXrHt5jDPCXHGJ/O Voqlpx7Ky1U9+bY33v1FzkPyFsws2ae5Mb+zhngFc9KvkuDt4EZyaCg+F7uH7c/PDd1B wFZpBeX2NE113PTwXbWj+bz18yBmz5vZhtoH/qd+4O2iF4AbTu140QcXig7/Foh4vt4J AQPQ== 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=mftcGRoC+RFXeTMXrZFfQPjseGZICjE9/eH/xdSJQtc=; b=n4kB/mpU0WRvEkV82j3dqY6RN3l+H/Y4AsTtt1OTDTOrTSzDgrb87oiEzhUMqwpSfh HElZ9QmPKY6aAIuDNC3kQ4xBYZtkZfSJck/ky13ZNaTowI/FqIZYq5lSZOZqtmy/Csu2 GjyGUNGt6ujScHD/hbCSGQhM3Ls95/dYcXsieoKGVCYpqESQW0rmw8zbiP0gTWxvI/xH HD5xLPlXdOGHD2UlUuMYP+extYwxtmkM3vwi/uhrJ4846Ga3rsLyqQ6frUKLa5JOuo6k HvLJQcHS/fwQoo0zwchC4K5kQrlJ7kLUdOSVweFBKFN70dYmoSvTL0IRp3Je7RZtVb2j 8sTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80214-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80214-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 w9si5274301pge.413.2017.06.08.14.16.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:48 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80214-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-80214-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80214-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=QK2VCwYcuxXwt6H4GUZiq4PTbcndbbS ejL3gjrXvPJqI36vrgHz02SXwTW7WFJPpHyf+QLRqANT/ntGMeuUxl7XUjIkdrKo YqaKzzSQNMq0iLsUPXEJcDQ5oTVF97T2xZHDZ+vtf3FPBVSUQN3o/SL9Rf2pYNHD 6NgLV9S2cW/U= 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=yyHwrTzvTev7fgTMiRj+gIDvEAc=; b=iONOD MdAAi3KmSlVaSAWpTZ81WmbbBYA0ARdIz4oJlK5vH6P8IjmSxuRnHzTcei7mvv5T IpdO/FJ0lch0fu3A/lo7ZlzDm/hDUfoLn7RdamGTGS7BTQg7jn2ye6ol1QUC9iIs rnYZ1D5TWQdsE1P/orJM4cw+k/liY9aiFBYRtY= Received: (qmail 41715 invoked by alias); 8 Jun 2017 21:14:28 -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 40831 invoked by uid 89); 8 Jun 2017 21:14:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=6898 X-HELO: mail-qt0-f182.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=mftcGRoC+RFXeTMXrZFfQPjseGZICjE9/eH/xdSJQtc=; b=WYJSsdzJw+lHlWuD5jisId+BCk2IOJZhKIw/xMjq01krBaFvkNwee37/Dzms0JCpRR FCtAczZxPHRdIkZwKMAjddRT1fC7LBbt7BR3zC5HtrJ/nbWGjnM8klgm/fuyGzi7P12j Vq9qXQlQfNQdPWeYOfksQkmVXLd/v5pn/3fUyWC5LuYndkxSalLRFxOIg0HkzftaTZ5w 9pa7DpL0/EDAgziYbrIJ1Xim7BRMBCsRunQiH5VvFNI+pi0V+J+zVA3kkhn5pCf7d9FK pt1hsEVEWVNKnAgsNU5nX7R7eB22c4LltI0cQQeEkihai225RywRkwT36ZprURpLUaqg YtkQ== X-Gm-Message-State: AODbwcAv0/b+qABMWBVZPQFT9X1LJrsLAYbIpnWzWQi4NeXFj2+laoKL beycIqhgRQVjcw6tivQ0Bw== X-Received: by 10.237.47.69 with SMTP id l63mr14253692qtd.196.1496956459181; Thu, 08 Jun 2017 14:14:19 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 15/17] posix: Use char_array for home_dir in glob Date: Thu, 8 Jun 2017 18:13:29 -0300 Message-Id: <1496956411-25594-16-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 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 652ae9e..4cefdca 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -606,8 +606,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:"; @@ -634,7 +641,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]; @@ -667,9 +674,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; @@ -678,10 +683,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; @@ -689,8 +692,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 */ @@ -698,7 +704,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; } @@ -706,9 +712,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