33 *
44 * Copyright 2000 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.38 2000/11/13 23:37:53 momjian Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.39 2000/11/25 06:21:54 momjian Exp $
77 */
88#include "postgres.h"
99#include "command.h"
1313#include <ctype.h>
1414#ifndef WIN32
1515#include <sys/types.h> /* for umask() */
16- #include <sys/stat.h> /* for umask(), stat() */
16+ #include <sys/stat.h> /* for stat() */
17+ #include <fcntl.h> /* open() flags */
1718#include <unistd.h> /* for geteuid(), getpid(), stat() */
1819#else
1920#include <win32.h>
@@ -1397,7 +1398,8 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
13971398 FILE * stream ;
13981399 const char * fname ;
13991400 bool error = false;
1400-
1401+ int fd ;
1402+
14011403#ifndef WIN32
14021404 struct stat before ,
14031405 after ;
@@ -1411,7 +1413,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
14111413 {
14121414 /* make a temp file to edit */
14131415#ifndef WIN32
1414- mode_t oldumask ;
14151416 const char * tmpdirenv = getenv ("TMPDIR" );
14161417
14171418 sprintf (fnametmp , "%s/psql.edit.%ld.%ld" ,
@@ -1422,15 +1423,11 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
14221423#endif
14231424 fname = (const char * ) fnametmp ;
14241425
1425- #ifndef WIN32
1426- oldumask = umask (0177 );
1427- #endif
1428- stream = fopen (fname , "w" );
1429- #ifndef WIN32
1430- umask (oldumask );
1431- #endif
1426+ fd = open (fname , O_WRONLY |O_CREAT |O_EXCL , 0600 );
1427+ if (fd != -1 )
1428+ stream = fdopen (fd , "w" );
14321429
1433- if (!stream )
1430+ if (fd == -1 || !stream )
14341431 {
14351432 psql_error ("couldn't open temp file %s: %s\n" , fname , strerror (errno ));
14361433 error = true;
0 commit comments