From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30602 invoked by alias); 12 Aug 2010 11:04:38 -0000 Received: (qmail 30569 invoked by uid 22791); 12 Aug 2010 11:04:37 -0000 X-SWARE-Spam-Status: No, hits=-5.9 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 12 Aug 2010 11:04:12 +0000 Received: from int-mx04.intmail.prod.int.phx2.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.17]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o7CB4A5d004483 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 12 Aug 2010 07:04:10 -0400 Received: from hase.home (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx04.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o7CB499q005720 for ; Thu, 12 Aug 2010 07:04:09 -0400 From: Andreas Schwab To: libc-hacker@sourceware.org Subject: [PATCH] Properly quote output of locale X-Yow: Kids, the seven basic food groups are GUM, PUFF PASTRY, PIZZA, PESTICIDES, ANTIBIOTICS, NUTRA-SWEET and MILK DUDS!! Date: Thu, 12 Aug 2010 11:04:00 -0000 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Mailing-List: contact libc-hacker-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sourceware.org X-SW-Source: 2010-08/txt/msg00000.txt.bz2 2010-08-12 Andreas Schwab [BZ #11904] * locale/programs/locale.c (print_assignment): New function. (show_locale_vars): Use it. --- NEWS | 2 +- locale/programs/locale.c | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index d9cc5e4..e9fce83 100644 --- a/NEWS +++ b/NEWS @@ -9,7 +9,7 @@ Version 2.13 * The following bugs are resolved with this release: - 11640, 11701, 11840, 11856, 11883, 11903 + 11640, 11701, 11840, 11856, 11883, 11903, 11904 * New Linux interfaces: prlimit, prlimit64, fanotify_init, fanotify_mark diff --git a/locale/programs/locale.c b/locale/programs/locale.c index a3602cf..2005dec 100644 --- a/locale/programs/locale.c +++ b/locale/programs/locale.c @@ -762,6 +762,29 @@ write_charmaps (void) twalk (all_data, print_names); } +/* Print a properly quoted assignment of NAME with VAL, using double + quotes iff DQUOTE is true. */ +static void +print_assignment (const char *name, const char *val, bool dquote) +{ + printf ("%s=", name); + if (dquote) + putchar ('"'); + while (*val != '\0') + { + size_t segment + = strcspn (val, dquote ? "$`\"\\" : "~|&;<>()$`\\\"' \t\n"); + printf ("%.*s", (int) segment, val); + val += segment; + if (*val == '\0') + break; + putchar ('\\'); + putchar (*val++); + } + if (dquote) + putchar ('"'); + putchar ('\n'); +} /* We have to show the contents of the environments determining the locale. */ @@ -769,7 +792,7 @@ static void show_locale_vars (void) { size_t cat_no; - const char *lcall = getenv ("LC_ALL"); + const char *lcall = getenv ("LC_ALL") ? : ""; const char *lang = getenv ("LANG") ? : ""; auto void get_source (const char *name); @@ -778,15 +801,15 @@ show_locale_vars (void) { char *val = getenv (name); - if ((lcall ?: "")[0] != '\0' || val == NULL) - printf ("%s=\"%s\"\n", name, - (lcall ?: "")[0] ? lcall : (lang ?: "")[0] ? lang : "POSIX"); + if (lcall[0] != '\0' || val == NULL) + print_assignment (name, lcall[0] ? lcall : lang[0] ? lang : "POSIX", + true); else - printf ("%s=%s\n", name, val); + print_assignment (name, val, false); } /* LANG has to be the first value. */ - printf ("LANG=%s\n", lang); + print_assignment ("LANG", lang, false); /* Now all categories in an unspecified order. */ for (cat_no = 0; cat_no < NCATEGORIES; ++cat_no) @@ -794,7 +817,7 @@ show_locale_vars (void) get_source (category[cat_no].name); /* The last is the LC_ALL value. */ - printf ("LC_ALL=%s\n", lcall ? : ""); + print_assignment ("LC_ALL", lcall, false); } -- 1.7.2.1 -- Andreas Schwab, schwab@redhat.com GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84 5EC7 45C6 250E 6F00 984E "And now for something completely different."