From patchwork Tue Jan 9 08:30:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 123808 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3749836qgn; Tue, 9 Jan 2018 00:32:15 -0800 (PST) X-Google-Smtp-Source: ACJfBotZeqzWzPBFEODznJw+yqd5e1u7Psv8v8Qy5qkLNpdAzp/tSQADskZcsTxdGECjkyZTUnve X-Received: by 10.99.106.138 with SMTP id f132mr11667001pgc.115.1515486735722; Tue, 09 Jan 2018 00:32:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515486735; cv=none; d=google.com; s=arc-20160816; b=BbxzqR9Mb4vG142UmQo6R3683MUr8B96Om187+HODKr0YrnOQgfpdg4aE8rgLh7DoW 5INbn45DF05gvDokAt5e9xXK/3ParfRxCcYJK0vb1HzmUDqsNXvO6DahJEZ3ObS7If7j wUY2EFNd9jzgjpnUXxnoiPs4ftZIGZ//e1BeNc3MkCtoAW2TSblb2eINzG5F1VAXb+bX gnzYJ0/Q8njgkmkuJT38jvFLJ7RRlVdKj9V0sV8Z2Ya19N74loLAXPGS3+NARzKz1qsT +Vwyrqw+BjAiZemgRJD/iodLK00Hd3LrDdHV/ZKAa2KPuX/OIgMo1JhPHfABPU+wcGM1 otGw== 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=v4B8CfISkXQIbKsnFb/dV48dCxgpM5Y4NCLYNO3Js0I=; b=YyI68JVuGzxhKlsupKd4+mjYKjPCCQnTglP8Dmr5416bH7yv6CLhYxVcykoiQcg57o OCOfMD6FN4NX+lcg3omkzlb/0l0c0JTVgWbBWeJLsi6ArUzPQoRyyk7u2VHN1KPNdReS fUJVc4x6XpZ5rPUVL88fvyjAE/zSY783DsrWVmFX2VJmgZt05mIyN4dgvyPXmWr8Qvzh 7fdUyN1ToRSOmnW06MHeGwZn8TAkofb/yb0zxvqVsjxEwicY8X9lfflRxhcmxry71a1C i6LnNk3/56r//B/w9mitgTQSM6OHwgESZEjMjq2p+4qRFrtRA7pmWUokoEyVLrNoQPFb nSgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=zAkMbYNG; 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 g3si10025924plb.153.2018.01.09.00.32.15; Tue, 09 Jan 2018 00:32:15 -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=zAkMbYNG; 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 S1751525AbeAIIcN (ORCPT + 28 others); Tue, 9 Jan 2018 03:32:13 -0500 Received: from conuserg-10.nifty.com ([210.131.2.77]:27465 "EHLO conuserg-10.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751439AbeAIIcD (ORCPT ); Tue, 9 Jan 2018 03:32:03 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-10.nifty.com with ESMTP id w098Uugt024960; Tue, 9 Jan 2018 17:30:56 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com w098Uugt024960 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1515486657; bh=v4B8CfISkXQIbKsnFb/dV48dCxgpM5Y4NCLYNO3Js0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zAkMbYNGUGadwwYykUh/KfBUVkkSoYj8GRc85XvhQUzumVZ4fB0bZTVedXQiPS3I9 Ev3bdRoBegl8hF3ra2OlkoBus7sHyHnjc6bPOKhtwLeH0tOcuMOnonxWJizgWH7Jlu TlpSRE+z5EmsHqgMrj/59MvwGc0RrosyQkmAQgbg2VS3w2yVueVE11JzXnzWrvC5oH aA/SteWfpL9Jo/ESPAnQ2HBdBrW41RP+ynTcfaSyJBKEnSYd1WQ7BdG3RgI/GG/tnA GDC2DYZHFgFb+RbxRLjG0XHjyzzg727gtyKYtXcSCpX6OSGvqpYxUjUnl/FHyViSGm s6jI77qjxWkcw== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Sam Ravnborg , Michal Marek , Lukas Bulwahn , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH 2/6] fixdep: merge do_config_file() and print_deps() into load_file() Date: Tue, 9 Jan 2018 17:30:46 +0900 Message-Id: <1515486650-1141-2-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515486650-1141-1-git-send-email-yamada.masahiro@socionext.com> References: <1515486650-1141-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 putting the common code into load_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 load_file() allocates 1-byte buffer for an empty file. strstr() will just return NULL, and this is what we expect. On the other hand, 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 --- 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 7efbeb7..f954f226 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -266,42 +266,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 *load_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; } /* @@ -316,6 +310,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" */ @@ -374,7 +369,10 @@ static void parse_dep_file(char *m) is_first_dep = 0; } else printf(" %s \\\n", s); - do_config_file(s); + + buf = load_file(s); + parse_config_file(buf); + free(buf); } } @@ -399,46 +397,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++; @@ -450,7 +412,10 @@ int main(int argc, char *argv[]) cmdline = argv[3]; print_cmdline(); - print_deps(depfile); + + buf = load_file(depfile); + parse_dep_file(buf); + free(buf); return 0; }