summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1995-10-26 13:19:10 +0000
committerJim Meyering <jim@meyering.net>1995-10-26 13:19:10 +0000
commit66b9285a6ff9f8df03c1d757742eb00cfc717b38 (patch)
treeef09479068664a359e51921c8576e4b4f6d3158c /src
parent470e773e76df88f03d94f98a8306662150dd50a9 (diff)
downloadcoreutils-66b9285a6ff9f8df03c1d757742eb00cfc717b38.tar.xz
(xtmpfopen): New function -- for opening temp files.
Use it instead of xfopen.
Diffstat (limited to 'src')
-rw-r--r--src/sort.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/sort.c b/src/sort.c
index 1a5d98302..edae0f8ce 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -294,6 +294,23 @@ xrealloc (char *p, unsigned int n)
}
static FILE *
+xtmpfopen (const char *file)
+{
+ FILE *fp;
+ int fd;
+
+ fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ if (fd < 0 || (fp = fdopen (fd, "w")) == NULL)
+ {
+ error (0, errno, "%s", file);
+ cleanup ();
+ exit (2);
+ }
+
+ return fp;
+}
+
+static FILE *
xfopen (const char *file, const char *how)
{
FILE *fp;
@@ -304,9 +321,7 @@ xfopen (const char *file, const char *how)
}
else
{
- int fd;
- fd = open (file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- if (fd < 0 || (fp = fdopen (fd, how)) == NULL)
+ if ((fp = fopen (file, how)) == NULL)
{
error (0, errno, "%s", file);
cleanup ();
@@ -1307,7 +1322,7 @@ merge (char **files, int nfiles, FILE *ofp)
{
for (j = 0; j < NMERGE; ++j)
fps[j] = xfopen (files[i * NMERGE + j], "r");
- tfp = xfopen (temp = tempname (), "w");
+ tfp = xtmpfopen (temp = tempname ());
mergefps (fps, NMERGE, tfp);
xfclose (tfp);
for (j = 0; j < NMERGE; ++j)
@@ -1316,7 +1331,7 @@ merge (char **files, int nfiles, FILE *ofp)
}
for (j = 0; j < nfiles % NMERGE; ++j)
fps[j] = xfopen (files[i * NMERGE + j], "r");
- tfp = xfopen (temp = tempname (), "w");
+ tfp = xtmpfopen (temp = tempname ());
mergefps (fps, nfiles % NMERGE, tfp);
xfclose (tfp);
for (j = 0; j < nfiles % NMERGE; ++j)
@@ -1371,7 +1386,7 @@ sort (char **files, int nfiles, FILE *ofp)
else
{
++n_temp_files;
- tfp = xfopen (tempname (), "w");
+ tfp = xtmpfopen (tempname ());
}
for (i = 0; i < lines.used; ++i)
if (!unique || i == 0
@@ -1828,7 +1843,7 @@ main (int argc, char **argv)
fp = xfopen (files[i], "r");
tmp = tempname ();
- ofp = xfopen (tmp, "w");
+ ofp = xtmpfopen (tmp);
while ((cc = fread (buf, 1, sizeof buf, fp)) > 0)
xfwrite (buf, 1, cc, ofp);
if (ferror (fp))
@@ -1842,7 +1857,7 @@ main (int argc, char **argv)
files[i] = tmp;
}
}
- ofp = xfopen (outfile, "w");
+ ofp = xtmpfopen (outfile);
}
else
ofp = stdout;