From patchwork Thu Jan 11 13:05:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 124198 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp654788qgn; Thu, 11 Jan 2018 05:07:58 -0800 (PST) X-Google-Smtp-Source: ACJfBotO+3FdS1GkUp5EI3eeD4Hh64yhUuguRZbsTamI23N2ME/r0cvjMxfc7rxC3uxJfz+Fuk1Z X-Received: by 10.99.146.14 with SMTP id o14mr17541692pgd.267.1515676078521; Thu, 11 Jan 2018 05:07:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515676078; cv=none; d=google.com; s=arc-20160816; b=C26EgwTavw9OFX0X6SFNvjqKgG39PF36gVThNE3axV1eMtY949RzJ1Nfx3u4awvwa2 rlbmsALubpsqFiKvi2cK0jEbXFJ8v6kEjtjYqi2vNyyBZ1jPhjWM2+t9gTg1XNxK5Jmr mlnLJLMbyev7KMV1hwcVyS+7VaN1zGD5To4XmQwzWdMTcxz0WszoA3fSpv7lNkGyztWO VjlmvR2f+Yme+UyBO8ycpplsj6JEqbjaFfdIeG6tITF5OAl3Kvu3WzGKfkzlWeRsMM3u KlFW0TAwZQ8wNcUQ8XpSdKDBuJjqE7GbA/HIUkYEgsB1FIQcjHKXtIU7qIf5S+yo/pPz GHyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter :arc-authentication-results; bh=GgZYNEarIpRHP7hobwe6Zb11mGtp6vclkM61EXOkrrA=; b=aPvue1llyNZkEgSafVppAT61KgOEVPoemP42LlwwUUgFtO2E4Kfnzyb+B8KLnRywNd zY3BGs1Dzj04WVtGyD3iFTFtd55EArwHi4TOUYHfIDRjGLV2xF0bvOE0oV2V+n53RI0x Bxfx4vOC5VEwowXhfSbRVtdmpko1dGYhQMUX661Xgb7ebVjCmn1MyER4z15+FqRdGbMY S0hcp1soDe9zke394Sy+cdReUTnSJ4eBq4Bs+eiuEwWmlfHv+x6wNjsJo+Tl+7NAzax+ 2k4/GMz2jdVA0ibCEh9D6jLJFkgMQojAV1N98J5CZ6AI7akfoHZKO5LoZsvsVICZosHx FzsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=irtPVNsm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r59si1360465plb.314.2018.01.11.05.07.58; Thu, 11 Jan 2018 05:07:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=irtPVNsm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934481AbeAKNHn (ORCPT + 28 others); Thu, 11 Jan 2018 08:07:43 -0500 Received: from conuserg-12.nifty.com ([210.131.2.79]:60598 "EHLO conuserg-12.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934064AbeAKNG1 (ORCPT ); Thu, 11 Jan 2018 08:06:27 -0500 Received: from grover.sesame (FL1-125-199-20-195.osk.mesh.ad.jp [125.199.20.195]) (authenticated) by conuserg-12.nifty.com with ESMTP id w0BD5mX3031475; Thu, 11 Jan 2018 22:05:50 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-12.nifty.com w0BD5mX3031475 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1515675950; bh=GgZYNEarIpRHP7hobwe6Zb11mGtp6vclkM61EXOkrrA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=irtPVNsm43M9q/Ated7V3cIDT06kVgh1hQlUuhXHJM32wWauRfsZjxHnnGJeT8NVw 4OtPZmDEKejtxiH8u4+yVqwXKPoqbHTkIqNoLbUEyKZO8LpDuVzAc9P5yM1txzYg6o XwMVVbxAs5PJeE4IKWyR4E2jdOXWKK+5fcR6RfcH3XBrdfPiHfVVJ2Sn8pX1rJ7EBM 0iRk0h9X6lTTZtWt4qgqKo+M9Gewdwhs/vsdpSjYz/CcRmbN3GyXKacnspsxhWfZJx jjywj0Sd8w6eIS9efJ2i8lfDXeqMo7eI0o+6ZdHppAoGFw/SNo8N4f82amTgd8Lcbr SZkH/GMM9g3ng== X-Nifty-SrcIP: [125.199.20.195] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Michal Marek , Sam Ravnborg , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v2 3/7] fixdep: factor out common code for reading files Date: Thu, 11 Jan 2018 22:05:42 +0900 Message-Id: <1515675946-7514-3-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515675946-7514-1-git-send-email-yamada.masahiro@socionext.com> References: <1515675946-7514-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Now, do_config_files() and print_deps() are almost the same. Only the difference is the parser function called (parse_config_file vs parse_dep_file). We can reduce the code duplication by factoring out the common code into read_file() - this function allocates a buffer and loads a file to it. It returns the pointer to the allocated buffer. (As before, it bails out by exit(2) for any error.) The caller must free the buffer when done. Having empty source files is possible; fixdep should simply skip them. I deleted the "st.st_size == 0" check, so read_file() allocates 1-byte buffer for an empty file. strstr() will immediately return NULL, and this is what we expect. On the other hand, an empty dep_file should be treated as an error. In this case, parse_dep_file() will error out with "no targets found" and it is a correct error message. Signed-off-by: Masahiro Yamada --- Changes in v2: - rename load_file to read_file scripts/basic/fixdep.c | 75 ++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 55 deletions(-) -- 2.7.4 diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index 91bb4c1..9f9238e 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -264,42 +264,36 @@ static int strrcmp(const char *s, const char *sub) return memcmp(s + slen - sublen, sub, sublen); } -static void do_config_file(const char *filename) +static void *read_file(const char *filename) { struct stat st; int fd; - char *map; + char *buf; fd = open(filename, O_RDONLY); if (fd < 0) { - fprintf(stderr, "fixdep: error opening config file: "); + fprintf(stderr, "fixdep: error opening file: "); perror(filename); exit(2); } if (fstat(fd, &st) < 0) { - fprintf(stderr, "fixdep: error fstat'ing config file: "); + fprintf(stderr, "fixdep: error fstat'ing file: "); perror(filename); exit(2); } - if (st.st_size == 0) { - close(fd); - return; - } - map = malloc(st.st_size + 1); - if (!map) { + buf = malloc(st.st_size + 1); + if (!buf) { perror("fixdep: malloc"); exit(2); } - if (read(fd, map, st.st_size) != st.st_size) { + if (read(fd, buf, st.st_size) != st.st_size) { perror("fixdep: read"); exit(2); } - map[st.st_size] = '\0'; + buf[st.st_size] = '\0'; close(fd); - parse_config_file(map); - - free(map); + return buf; } /* @@ -314,6 +308,7 @@ static void parse_dep_file(char *m) int is_last, is_target; int saw_any_target = 0; int is_first_dep = 0; + void *buf; while (1) { /* Skip any "white space" */ @@ -372,7 +367,10 @@ static void parse_dep_file(char *m) is_first_dep = 0; } else printf(" %s \\\n", s); - do_config_file(s); + + buf = read_file(s); + parse_config_file(buf); + free(buf); } } @@ -397,46 +395,10 @@ static void parse_dep_file(char *m) printf("$(deps_%s):\n", target); } -static void print_deps(const char *filename) -{ - struct stat st; - int fd; - char *buf; - - fd = open(filename, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "fixdep: error opening depfile: "); - perror(filename); - exit(2); - } - if (fstat(fd, &st) < 0) { - fprintf(stderr, "fixdep: error fstat'ing depfile: "); - perror(filename); - exit(2); - } - if (st.st_size == 0) { - close(fd); - return; - } - buf = malloc(st.st_size + 1); - if (!buf) { - perror("fixdep: malloc"); - exit(2); - } - if (read(fd, buf, st.st_size) != st.st_size) { - perror("fixdep: read"); - exit(2); - } - buf[st.st_size] = '\0'; - close(fd); - - parse_dep_file(buf); - - free(buf); -} - int main(int argc, char *argv[]) { + void *buf; + if (argc == 5 && !strcmp(argv[1], "-e")) { insert_extra_deps = 1; argv++; @@ -448,7 +410,10 @@ int main(int argc, char *argv[]) cmdline = argv[3]; print_cmdline(); - print_deps(depfile); + + buf = read_file(depfile); + parse_dep_file(buf); + free(buf); return 0; }