1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-27 16:50:58 +01:00

Use the new gettempfile() function.

This commit is contained in:
Wayne Davison 2004-10-21 00:33:50 +00:00
parent 65729f5570
commit 3ccc3e4bdc
4 changed files with 9 additions and 16 deletions

View file

@ -1971,8 +1971,7 @@ zftp_open(char *name, char **args, int flags)
* However, it is closed whenever there are no connections open. * However, it is closed whenever there are no connections open.
*/ */
if (zfstatfd == -1) { if (zfstatfd == -1) {
fname = gettempname(NULL, 1); zfstatfd = gettempfile(NULL, 1, &fname);
zfstatfd = open(fname, O_RDWR|O_CREAT|O_EXCL, 0600);
DPUTS(zfstatfd == -1, "zfstatfd not created"); DPUTS(zfstatfd == -1, "zfstatfd not created");
#if defined(F_SETFD) && defined(FD_CLOEXEC) #if defined(F_SETFD) && defined(FD_CLOEXEC)
/* If the shell execs a program, we don't want this fd left open. */ /* If the shell execs a program, we don't want this fd left open. */

View file

@ -1445,10 +1445,8 @@ bin_fc(char *nam, char **argv, Options ops, int func)
char *fil; char *fil;
retval = 1; retval = 1;
fil = gettempname(NULL, 1); if ((tempfd = gettempfile(NULL, 1, &fil)) < 0
if (((tempfd = open(fil, O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY, 0600)) || ((out = fdopen(tempfd, "w")) == NULL)) {
== -1) ||
((out = fdopen(tempfd, "w")) == NULL)) {
unqueue_signals(); unqueue_signals();
zwarnnam("fc", "can't open temp file: %e", NULL, errno); zwarnnam("fc", "can't open temp file: %e", NULL, errno);
} else { } else {
@ -3535,8 +3533,8 @@ bin_print(char *name, char **args, Options ops, int func)
zwarnnam(name, "open_memstream failed", NULL, 0); zwarnnam(name, "open_memstream failed", NULL, 0);
#else #else
int tempfd; int tempfd;
char *tmpf = gettempname(NULL, 1); char *tmpf;
if ((tempfd = open(tmpf, O_RDWR|O_CREAT|O_EXCL, 0644)) < 0 if ((tempfd = gettempfile(NULL, 1, &tmpf)) < 0
|| (fout = fdopen(tempfd, "w+")) == NULL) || (fout = fdopen(tempfd, "w+")) == NULL)
zwarnnam(name, "can't open temp file: %e", NULL, errno); zwarnnam(name, "can't open temp file: %e", NULL, errno);
unlink(tmpf); unlink(tmpf);

View file

@ -2801,8 +2801,7 @@ getherestr(struct redir *fn)
untokenize(t); untokenize(t);
unmetafy(t, &len); unmetafy(t, &len);
t[len++] = '\n'; t[len++] = '\n';
s = gettempname(NULL, 1); if ((fd = gettempfile(NULL, 1, &s)) < 0)
if (!s || (fd = open(s, O_CREAT|O_WRONLY|O_EXCL|O_NOCTTY, 0600)) == -1)
return -1; return -1;
write(fd, t, len); write(fd, t, len);
close(fd); close(fd);
@ -2975,11 +2974,9 @@ getoutputfile(char *cmd)
return NULL; return NULL;
if (!(prog = parsecmd(cmd))) if (!(prog = parsecmd(cmd)))
return NULL; return NULL;
if (!(nam = gettempname(NULL, 1))) if (!(nam = gettempname(NULL, 0)))
return NULL; return NULL;
nam = ztrdup(nam);
if (!jobtab[thisjob].filelist) if (!jobtab[thisjob].filelist)
jobtab[thisjob].filelist = znewlinklist(); jobtab[thisjob].filelist = znewlinklist();
zaddlinknode(jobtab[thisjob].filelist, nam); zaddlinknode(jobtab[thisjob].filelist, nam);

View file

@ -2137,8 +2137,7 @@ lockhistfile(char *fn, int keep_trying)
lockfile = bicat(unmeta(fn), ".LOCK"); lockfile = bicat(unmeta(fn), ".LOCK");
#ifdef HAVE_LINK #ifdef HAVE_LINK
tmpfile = gettempname(fn, 0); if ((fd = gettempfile(fn, 0, &tmpfile)) >= 0) {
if ((fd = open(tmpfile, O_WRONLY|O_CREAT|O_EXCL, 0644)) >= 0) {
FILE *out = fdopen(fd, "w"); FILE *out = fdopen(fd, "w");
if (out) { if (out) {
fprintf(out, "%ld %s\n", (long)getpid(), getsparam("HOST")); fprintf(out, "%ld %s\n", (long)getpid(), getsparam("HOST"));
@ -2163,8 +2162,8 @@ lockhistfile(char *fn, int keep_trying)
break; break;
} }
unlink(tmpfile); unlink(tmpfile);
free(tmpfile);
} }
free(tmpfile);
#else /* not HAVE_LINK */ #else /* not HAVE_LINK */
while ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0644)) < 0) { while ((fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, 0644)) < 0) {
if (errno != EEXIST || !keep_trying) if (errno != EEXIST || !keep_trying)