]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(xfopen): Rewrite using open/fdopen in order to set
authorJim Meyering <jim@meyering.net>
Thu, 26 Oct 1995 05:14:41 +0000 (05:14 +0000)
committerJim Meyering <jim@meyering.net>
Thu, 26 Oct 1995 05:14:41 +0000 (05:14 +0000)
proper permissions on temporary files.  Reported by Erik Corry
(erik@kroete2.freinet.de).

src/sort.c

index be9ba38a59e4c5567acd7b608ebcb3211150d868..1a5d983027a43345c43a4d69c7cbd1676601408f 100644 (file)
@@ -294,16 +294,26 @@ xrealloc (char *p, unsigned int n)
 }
 
 static FILE *
-xfopen (char *file, char *how)
+xfopen (const char *file, const char *how)
 {
-  FILE *fp = strcmp (file, "-") ? fopen (file, how) : stdin;
+  FILE *fp;
 
-  if (fp == 0)
+  if (strcmp (file, "-") == 0)
     {
-      error (0, errno, "%s", file);
-      cleanup ();
-      exit (2);
+      fp = stdin;
     }
+  else
+    {
+      int fd;
+      fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+      if (fd < 0 || (fp = fdopen (fd, how)) == NULL)
+       {
+         error (0, errno, "%s", file);
+         cleanup ();
+         exit (2);
+       }
+    }
+
   if (fp == stdin)
     have_read_stdin = 1;
   return fp;