[parted-devel] Don't use deprecated mktemp, to avoid linker warning

Jim Meyering jim at meyering.net
Wed Mar 7 22:22:03 CET 2007


	* libparted/tests/common.c (_create_disk): Rewrite to
	use mkstemp instead.

diff --git a/libparted/tests/common.c b/libparted/tests/common.c
index af5b983..642da23 100644
--- a/libparted/tests/common.c
+++ b/libparted/tests/common.c
@@ -8,20 +8,25 @@

 char *_create_disk (const off_t size)
 {
-		char filename[] = "parted-test-XXXXXX";
-		mktemp (filename);
+		char *filename = strdup ("parted-test-XXXXXX");
+		if (filename == NULL)
+		  return NULL;
+		int fd = mkstemp (filename);
+		if (fd < 0) {
+	free_filename:;
+			free (filename);
+			return NULL;
+		}

-		FILE *disk = fopen (filename, "w");
+		FILE *disk = fdopen (fd, "w");
 		if (disk == NULL)
-			/* FIXME: give a diagnostic? */
-			return NULL;
+			goto free_filename;
 		off_t total_size = size * 1024 * 1024;	/* Mb */

-		fseek (disk, total_size, SEEK_SET);
-		fwrite ("", sizeof (char), 1, disk);
-		if (fclose (disk) != 0)
-		      /* FIXME: give a diagnostic? */
-		      return NULL;
+		int fail = (fseek (disk, total_size, SEEK_SET) != 0
+		            || fwrite ("", sizeof (char), 1, disk) != 1);
+		if (fclose (disk) != 0 || fail)
+			goto free_filename;

-		return strdup (filename);
+		return filename;
 }



More information about the parted-devel mailing list