3
3
*
4
4
* Copyright (c) 2000-2004, PostgreSQL Global Development Group
5
5
*
6
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.129 2004/10/16 03:10:16 momjian Exp $
6
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.130 2004/11/04 22:25:14 momjian Exp $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "command.h"
23
23
#include <io.h>
24
24
#include <fcntl.h>
25
25
#include <direct.h>
26
+ #ifndef WIN32_CLIENT_ONLY
27
+ #include <sys/types.h> /* for umask() */
28
+ #include <sys/stat.h> /* for stat() */
29
+ #endif
26
30
#endif
27
31
28
32
#include "libpq-fe.h"
@@ -1097,7 +1101,7 @@ editFile(const char *fname)
1097
1101
#ifndef WIN32
1098
1102
"exec "
1099
1103
#endif
1100
- "%s '%s'" , editorName , fname );
1104
+ "%s\"%s\" \"%s\"%s" , SYSTEMQUOTE , editorName , fname , SYSTEMQUOTE );
1101
1105
result = system (sys );
1102
1106
if (result == -1 )
1103
1107
psql_error ("could not start editor \"%s\"\n" , editorName );
@@ -1119,7 +1123,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
1119
1123
bool error = false;
1120
1124
int fd ;
1121
1125
1122
- #ifndef WIN32
1126
+ #ifndef WIN32_CLIENT_ONLY
1123
1127
struct stat before ,
1124
1128
after ;
1125
1129
#endif
@@ -1130,13 +1134,35 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
1130
1134
{
1131
1135
/* make a temp file to edit */
1132
1136
#ifndef WIN32
1133
- const char * tmpdirenv = getenv ("TMPDIR" );
1137
+ const char * tmpdir = getenv ("TMPDIR" );
1134
1138
1135
- snprintf (fnametmp , sizeof (fnametmp ), "%s/psql.edit.%d.%d" ,
1136
- tmpdirenv ? tmpdirenv : "/tmp" , geteuid (), (int )getpid ());
1139
+ if (!tmpdir )
1140
+ tmpdir = "/tmp" ;
1141
+ #else
1142
+ char tmpdir [MAXPGPATH ];
1143
+ int ret ;
1144
+
1145
+ ret = GetTempPath (MAXPGPATH , tmpdir );
1146
+ if (ret == 0 || ret > MAXPGPATH )
1147
+ {
1148
+ psql_error ("Can not locate temporary directory: %s" ,
1149
+ !ret ? strerror (errno ) : "" );
1150
+ return false;
1151
+ }
1152
+ /*
1153
+ * No canonicalize_path() here.
1154
+ * EDIT.EXE run from CMD.EXE prepends the current directory to the
1155
+ * supplied path unless we use only backslashes, so we do that.
1156
+ */
1157
+ #endif
1158
+ snprintf (fnametmp , sizeof (fnametmp ), "%s%spsql.edit.%d" , tmpdir ,
1159
+ #ifndef WIN32
1160
+ "/" ,
1137
1161
#else
1138
- GetTempFileName ( "." , "psql" , 0 , fnametmp );
1162
+ "" , /* trailing separator already present */
1139
1163
#endif
1164
+ (int )getpid ());
1165
+
1140
1166
fname = (const char * ) fnametmp ;
1141
1167
1142
1168
fd = open (fname , O_WRONLY | O_CREAT | O_EXCL , 0600 );
@@ -1174,7 +1200,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
1174
1200
}
1175
1201
}
1176
1202
1177
- #ifndef WIN32
1203
+ #ifndef WIN32_CLIENT_ONLY
1178
1204
if (!error && stat (fname , & before ) != 0 )
1179
1205
{
1180
1206
psql_error ("%s: %s\n" , fname , strerror (errno ));
@@ -1186,7 +1212,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
1186
1212
if (!error )
1187
1213
error = !editFile (fname );
1188
1214
1189
- #ifndef WIN32
1215
+ #ifndef WIN32_CLIENT_ONLY
1190
1216
if (!error && stat (fname , & after ) != 0 )
1191
1217
{
1192
1218
psql_error ("%s: %s\n" , fname , strerror (errno ));
@@ -1509,9 +1535,13 @@ do_shell(const char *command)
1509
1535
if (!command )
1510
1536
{
1511
1537
char * sys ;
1512
- const char * shellName ;
1538
+ const char * shellName = NULL ;
1513
1539
1514
- shellName = getenv ("SHELL" );
1540
+ #ifdef WIN32
1541
+ shellName = getenv ("COMSPEC" );
1542
+ #endif
1543
+ if (shellName == NULL )
1544
+ shellName = getenv ("SHELL" );
1515
1545
if (shellName == NULL )
1516
1546
shellName = DEFAULT_SHELL ;
1517
1547
@@ -1520,7 +1550,7 @@ do_shell(const char *command)
1520
1550
#ifndef WIN32
1521
1551
"exec "
1522
1552
#endif
1523
- "%s" , shellName );
1553
+ "%s\"%s\"%s" , SYSTEMQUOTE , shellName , SYSTEMQUOTE );
1524
1554
result = system (sys );
1525
1555
free (sys );
1526
1556
}
0 commit comments