From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 85488 invoked by alias); 15 Feb 2019 11:11:15 -0000 Mailing-List: contact dwz-help@sourceware.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: Sender: dwz-owner@sourceware.org Received: (qmail 85470 invoked by uid 89); 15 Feb 2019 11:11:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.2 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.2 spammy=Vries, mkstemp, disposed, vries X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-HELO: mx1.suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Date: Tue, 01 Jan 2019 00:00:00 -0000 From: Tom de Vries To: dwz@sourceware.org, jakub@redhat.com Subject: [PATCH 1/2] Factor out make_temp_file Message-ID: <20190215111143.GA30045@delia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-SW-Source: 2019-q1/txt/msg00024.txt.bz2 Hi, Factor out new function make_temp_file from main, making main easier to read and the temporary file handling easier to maintain. OK for trunk? Thanks, - Tom Factor out make_temp_file 2019-02-15 Tom de Vries * dwz.c (make_temp_file): New function, factor out of ... (main): ... here. --- dwz.c | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/dwz.c b/dwz.c index d348418..c8ada61 100644 --- a/dwz.c +++ b/dwz.c @@ -11794,6 +11794,30 @@ version (void) exit (0); } +/* Create a temporary file using TEMPLATE. Return the corresponding file + descriptor if successful, otherwise return -1. */ +static int +make_temp_file (const char *template) +{ + char buf[sizeof "/tmp/dwz.debug_abbrev.XXXXXX"]; + int fd; + size_t template_len; + + template_len = strlen (template); + assert (template_len + 1 <= sizeof buf); + strncpy (buf, template, sizeof buf); + + fd = mkstemp (buf); + if (fd == -1) + return fd; + + /* Unlink the filename, such that the file is disposed of once the file + descriptor is closed. */ + unlink (buf); + + return fd; +} + int main (int argc, char *argv[]) { @@ -11897,27 +11921,11 @@ main (int argc, char *argv[]) error (1, 0, "-o option not allowed for multiple files"); if (multifile) { - char buf[sizeof "/tmp/dwz.debug_abbrev.XXXXXX"]; - strcpy (buf, "/tmp/dwz.debug_info.XXXXXX"); - multi_info_fd = mkstemp (buf); - if (multi_info_fd != -1) - unlink (buf); - strcpy (buf, "/tmp/dwz.debug_abbrev.XXXXXX"); - multi_abbrev_fd = mkstemp (buf); - if (multi_abbrev_fd != -1) - unlink (buf); - strcpy (buf, "/tmp/dwz.debug_line.XXXXXX"); - multi_line_fd = mkstemp (buf); - if (multi_line_fd != -1) - unlink (buf); - strcpy (buf, "/tmp/dwz.debug_str.XXXXXX"); - multi_str_fd = mkstemp (buf); - if (multi_str_fd != -1) - unlink (buf); - strcpy (buf, "/tmp/dwz.debug_macro.XXXXXX"); - multi_macro_fd = mkstemp (buf); - if (multi_macro_fd != -1) - unlink (buf); + multi_info_fd = make_temp_file ("/tmp/dwz.debug_info.XXXXXX"); + multi_abbrev_fd = make_temp_file ("/tmp/dwz.debug_abbrev.XXXXXX"); + multi_line_fd = make_temp_file ("/tmp/dwz.debug_line.XXXXXX"); + multi_str_fd = make_temp_file ("/tmp/dwz.debug_str.XXXXXX"); + multi_macro_fd = make_temp_file ("/tmp/dwz.debug_macro.XXXXXX"); if (multi_info_fd == -1 || multi_abbrev_fd == -1 || multi_line_fd == -1