public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Factor out make_temp_file
@ 2019-01-01  0:00 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2019-01-01  0:00 UTC (permalink / raw)
  To: dwz, jakub

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  <tdevries@suse.de>

	* 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-02-15 11:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-01  0:00 [PATCH 1/2] Factor out make_temp_file Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).