From patchwork Thu Jun 10 11:18:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 43818 X-Patchwork-Delegate: dj@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8345239A0016 for ; Thu, 10 Jun 2021 11:23:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8345239A0016 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623324187; bh=TtTf1D2q3gR/lY8p00tNQvLwYP01KxTGmtBA1onfjgE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=MAEXS1xqnqEWCV41jOoylZ/xuCOZgLi+oW1pMxdahu3v5LyBrCoiwyqT06dymXDLz TEpJMkSzOG8oWeVJup1r9Gh1W7TY0hpgeREGeiC6SVko3qdtxRIfzJuG6c8xemUJ2M 7sKqa3mZgVtvrCvgHKMvTPmeo4TQESRgrlrLPUuw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cross.elm.relay.mailchannels.net (cross.elm.relay.mailchannels.net [23.83.212.46]) by sourceware.org (Postfix) with ESMTPS id C55C639A0010 for ; Thu, 10 Jun 2021 11:19:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C55C639A0010 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 012FC34206C; Thu, 10 Jun 2021 11:19:57 +0000 (UTC) Received: from pdx1-sub0-mail-a13.g.dreamhost.com (100-96-17-41.trex.outbound.svc.cluster.local [100.96.17.41]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 7FF11341EB1; Thu, 10 Jun 2021 11:19:56 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a13.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.17.41 (trex/6.3.1); Thu, 10 Jun 2021 11:19:56 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Plucky-Tangy: 0b05f25d666db7b9_1623323996800_137689112 X-MC-Loop-Signature: 1623323996800:1116579169 X-MC-Ingress-Time: 1623323996800 Received: from pdx1-sub0-mail-a13.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a13.g.dreamhost.com (Postfix) with ESMTP id 1692A800C5; Thu, 10 Jun 2021 11:19:56 +0000 (UTC) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a13.g.dreamhost.com (Postfix) with ESMTPSA id 78BDD7FF19; Thu, 10 Jun 2021 11:19:51 +0000 (UTC) X-DH-BACKEND: pdx1-sub0-mail-a13 To: libc-alpha@sourceware.org Subject: [PATCH 4/6] iconvconfig: Use common gconv module parsing function Date: Thu, 10 Jun 2021 16:48:51 +0530 Message-Id: <20210610111853.2286873-5-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210610111853.2286873-1-siddhesh@sourceware.org> References: <20210610111853.2286873-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-3494.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Cc: schwab@linux-m68k.org Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Drop local copy of gconv file parsing and use the one in gconv_parseconfdir.h instead. Now there is a single implementation of configuration file parsing. --- iconv/iconvconfig.c | 126 ++++---------------------------------------- 1 file changed, 11 insertions(+), 115 deletions(-) diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c index c9607fb645..53e5c5c6f4 100644 --- a/iconv/iconvconfig.c +++ b/iconv/iconvconfig.c @@ -18,7 +18,6 @@ #include #include -#include #include #include #include @@ -34,10 +33,10 @@ #include #include #include -#include #include #include "iconvconfig.h" +#include /* Get libc version number. */ #include "../version.h" @@ -568,7 +567,7 @@ new_module (const char *fromname, size_t fromlen, const char *toname, /* Add new module. */ static void -add_module (char *rp, const char *directory) +add_module (char *rp, const char *directory, size_t dirlen, int modcount) { /* We expect now 1. `from' name @@ -646,131 +645,28 @@ add_module (char *rp, const char *directory) cost, need_ext); } -/* Read a gconv-modules configuration file. */ -static bool -handle_file (const char *dir, const char *infile) -{ - FILE *fp; - char *line = NULL; - size_t linelen = 0; - - fp = fopen (infile, "r"); - if (fp == NULL) - return false; - - /* No threads present. */ - __fsetlocking (fp, FSETLOCKING_BYCALLER); - - while (!feof_unlocked (fp)) - { - char *rp, *endp, *word; - ssize_t n = __getdelim (&line, &linelen, '\n', fp); - - if (n < 0) - /* An error occurred. */ - break; - - rp = line; - /* Terminate the line (excluding comments or newline) with a NUL - byte to simplify the following code. */ - endp = strchr (rp, '#'); - if (endp != NULL) - *endp = '\0'; - else - if (rp[n - 1] == '\n') - rp[n - 1] = '\0'; - - while (isspace (*rp)) - ++rp; - - /* If this is an empty line go on with the next one. */ - if (rp == endp) - continue; - - word = rp; - while (*rp != '\0' && !isspace (*rp)) - ++rp; - - if (rp - word == sizeof ("alias") - 1 - && memcmp (word, "alias", sizeof ("alias") - 1) == 0) - add_alias (rp); - else if (rp - word == sizeof ("module") - 1 - && memcmp (word, "module", sizeof ("module") - 1) == 0) - add_module (rp, dir); - /* else */ - /* Otherwise ignore the line. */ - } - - free (line); - - fclose (fp); - - return true; -} - /* Read config files and add the data for this directory to cache. */ static int handle_dir (const char *dir) { - char *cp; size_t dirlen = strlen (dir); bool found = false; + /* Add the prefix before sending it off to the parser. */ + char *fulldir = xmalloc (prefix_len + dirlen + 2); + char *cp = mempcpy (mempcpy (fulldir, prefix, prefix_len), dir, dirlen); + if (dir[dirlen - 1] != '/') { - char *newp = (char *) xmalloc (dirlen + 2); - dir = memcpy (newp, dir, dirlen); - newp[dirlen++] = '/'; - newp[dirlen] = '\0'; + *cp++ = '/'; + *cp = '\0'; + dirlen += 2; } - /* First, look for a gconv-modules file. */ - char *buf = malloc (prefix_len + dirlen + sizeof "gconv-modules.d"); - if (buf == NULL) - goto out; - - cp = buf; - if (dir[0] == '/') - cp = mempcpy (cp, prefix, prefix_len); - cp = mempcpy (cp, dir, dirlen); - cp = stpcpy (cp, "gconv-modules"); - - found |= handle_file (dir, buf); - - /* Next, see if there is a gconv-modules.d directory containing configuration - files and if it is non-empty. */ - cp[0] = '.'; - cp[1] = 'd'; - cp[2] = '\0'; - - DIR *confdir = opendir (buf); - if (confdir != NULL) - { - struct dirent *ent; - while ((ent = readdir (confdir)) != NULL) - { - if (ent->d_type != DT_REG) - continue; - - size_t len = strlen (ent->d_name); - const char *suffix = ".conf"; - - if (len > strlen (suffix) - && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0) - { - char *conf; - if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0) - continue; - found |= handle_file (dir, conf); - free (conf); - } - } - closedir (confdir); - } + found = gconv_parseconfdir (fulldir, dirlen + prefix_len); - free (buf); + free (fulldir); -out: if (!found) { error (0, errno, "failed to open gconv configuration files in `%s'",