[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Factor out make_temp_file



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