commit a927f6f629d8faa068aa1aff9ebf1dcce7f349ca
parent ff26d4769e3b190bbc051526359a9235803b51af
Author: sin <sin@2f30.org>
Date: Thu, 3 Dec 2015 12:12:59 +0000
Use mkstemp() instead of a fixed filename "ed.tmp"
Diffstat:
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/ed.c b/ed.c
@@ -38,6 +38,7 @@ size_t idxsize, lastidx;
struct hline *zero;
char *text;
char *savfname;
+char tmpname[FILENAME_MAX];
size_t sizetxt, memtxt;
int scratch;
int pflag, modflag, uflag;
@@ -207,7 +208,7 @@ clearbuf()
{
if (scratch)
close(scratch);
- remove("ed.tmp");
+ remove(tmpname);
free(zero);
zero = NULL;
scratch = csize = idxsize = lastidx = curln = lastln = 0;
@@ -221,7 +222,14 @@ setscratch()
mode_t mode = S_IRUSR | S_IWUSR;
clearbuf();
- if ((scratch = open("ed.tmp", flags, mode)) < 0 ||
+ strcpy(tmpname, "ed.XXXXXX");
+ if (mkstemp(tmpname) < 0) {
+ /* try /tmp if cwd is not writable */
+ strcpy(tmpname, "/tmp/ed.XXXXXX");
+ if (mkstemp(tmpname) < 0)
+ error("failed to create scratch file");
+ }
+ if ((scratch = open(tmpname, flags, mode)) < 0 ||
(k = makeline("", NULL))) {
error("input/output error in scratch file");
}