From patchwork Mon Jun 7 08:52:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1488484 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=tMqArw1R; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fz6br1KJvz9sW6 for ; Mon, 7 Jun 2021 18:53:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 68CD93838010 for ; Mon, 7 Jun 2021 08:53:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68CD93838010 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623056021; bh=vbNAxmGqg5GlEHtaqySveRvFHP/Mc/r5bgxlNvxcP4o=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=tMqArw1R0Bt/CmN8KHc1fQiExP72Zy6fOvQyY/skCyQne6r1jLnpGgnySRe3rDKHv m7MKcxMarOWIZYPzCFo4HAkiNIggtvWL0OjLFXrs87BKr1r953Kbc8Qp4mwn0nVuex TDZUBpqMFQ8cx3ug6odgCWe5TlA/NpeZxdLRgr8o= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from quail.birch.relay.mailchannels.net (quail.birch.relay.mailchannels.net [23.83.209.151]) by sourceware.org (Postfix) with ESMTPS id 31B5F3857020 for ; Mon, 7 Jun 2021 08:52:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 31B5F3857020 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 7F7BF343068; Mon, 7 Jun 2021 08:52:40 +0000 (UTC) Received: from pdx1-sub0-mail-a36.g.dreamhost.com (100-96-11-18.trex.outbound.svc.cluster.local [100.96.11.18]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 096AD3425E0; Mon, 7 Jun 2021 08:52:40 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a36.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.11.18 (trex/6.3.1); Mon, 07 Jun 2021 08:52:40 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Trade-Left: 7ad1e43e0ff15306_1623055960261_165238195 X-MC-Loop-Signature: 1623055960261:429711976 X-MC-Ingress-Time: 1623055960261 Received: from pdx1-sub0-mail-a36.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a36.g.dreamhost.com (Postfix) with ESMTP id BD5E77F257; Mon, 7 Jun 2021 08:52:39 +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-a36.g.dreamhost.com (Postfix) with ESMTPSA id AC30282BC8; Mon, 7 Jun 2021 08:52:37 +0000 (UTC) X-DH-BACKEND: pdx1-sub0-mail-a36 To: libc-alpha@sourceware.org Subject: [PATCH 1/5] iconvconfig: Make file handling more general purpose Date: Mon, 7 Jun 2021 14:22:17 +0530 Message-Id: <20210607085221.43612-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210607085221.43612-1-siddhesh@sourceware.org> References: <20210607085221.43612-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_H3, RCVD_IN_MSPIKE_WL, 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 Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Split out configuration file handling code from handle_dir into its own function so that it can be reused for multiple configuration files. Reviewed-by: DJ Delorie --- iconv/iconvconfig.c | 65 ++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c index e04dd8a2e1..f5aba636d7 100644 --- a/iconv/iconvconfig.c +++ b/iconv/iconvconfig.c @@ -644,37 +644,17 @@ add_module (char *rp, const char *directory) cost, need_ext); } - -/* Read the config file and add the data for this directory to that. */ -static int -handle_dir (const char *dir) +/* Read a gconv-modules configuration file. */ +static bool +handle_file (const char *dir, const char *infile) { - char *cp; FILE *fp; char *line = NULL; size_t linelen = 0; - size_t dirlen = strlen (dir); - - if (dir[dirlen - 1] != '/') - { - char *newp = (char *) xmalloc (dirlen + 2); - dir = memcpy (newp, dir, dirlen); - newp[dirlen++] = '/'; - newp[dirlen] = '\0'; - } - - char infile[prefix_len + dirlen + sizeof "gconv-modules"]; - cp = infile; - if (dir[0] == '/') - cp = mempcpy (cp, prefix, prefix_len); - strcpy (mempcpy (cp, dir, dirlen), "gconv-modules"); fp = fopen (infile, "r"); if (fp == NULL) - { - error (0, errno, "cannot open `%s'", infile); - return 1; - } + return false; /* No threads present. */ __fsetlocking (fp, FSETLOCKING_BYCALLER); @@ -723,7 +703,42 @@ handle_dir (const char *dir) fclose (fp); - return 0; + 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; + + if (dir[dirlen - 1] != '/') + { + char *newp = (char *) xmalloc (dirlen + 2); + dir = memcpy (newp, dir, dirlen); + newp[dirlen++] = '/'; + newp[dirlen] = '\0'; + } + + char infile[prefix_len + dirlen + sizeof "gconv-modules"]; + cp = infile; + if (dir[0] == '/') + cp = mempcpy (cp, prefix, prefix_len); + strcpy (mempcpy (cp, dir, dirlen), "gconv-modules"); + + found |= handle_file (dir, infile); + + if (!found) + { + error (0, errno, "failed to open gconv configuration file in `%s'", + dir); + error (0, 0, + "ensure that the directory contains a valid gconv-modules file."); + } + + return found ? 0 : 1; }