5
5
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
6
6
* Portions Copyright (c) 1994, Regents of the University of California
7
7
*
8
- * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.22 2005/12/12 15:41:52 momjian Exp $
8
+ * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.23 2005/12/12 15:48:04 momjian Exp $
9
9
*
10
10
*-------------------------------------------------------------------------
11
11
*/
17
17
18
18
static void help (const char * progname );
19
19
20
+ enum trivalue {
21
+ TRI_DEFAULT ,
22
+ TRI_NO ,
23
+ TRI_YES
24
+ };
20
25
21
26
int
22
27
main (int argc , char * argv [])
@@ -51,7 +56,6 @@ main(int argc, char *argv[])
51
56
const char * progname ;
52
57
int optindex ;
53
58
int c ;
54
-
55
59
char * newuser = NULL ;
56
60
char * host = NULL ;
57
61
char * port = NULL ;
@@ -62,16 +66,13 @@ main(int argc, char *argv[])
62
66
char * conn_limit = NULL ;
63
67
bool pwprompt = false;
64
68
char * newpassword = NULL ;
65
- /*
66
- * Tri-valued variables. -1 is "NO", +1 is enable and 0 uses the
67
- * server default.
68
- */
69
- int createdb = 0 ;
70
- int superuser = 0 ;
71
- int createrole = 0 ;
72
- int inherit = 0 ;
73
- int login = 0 ;
74
- int encrypted = 0 ;
69
+ /* Tri-valued variables. */
70
+ enum trivalue createdb = TRI_DEFAULT ,
71
+ superuser = TRI_DEFAULT ,
72
+ createrole = TRI_DEFAULT ,
73
+ inherit = TRI_DEFAULT ,
74
+ login = TRI_DEFAULT ,
75
+ encrypted = TRI_DEFAULT ;
75
76
76
77
PQExpBufferData sql ;
77
78
@@ -107,36 +108,36 @@ main(int argc, char *argv[])
107
108
quiet = true;
108
109
break ;
109
110
case 'd' :
110
- createdb = +1 ;
111
+ createdb = TRI_YES ;
111
112
break ;
112
113
case 'D' :
113
- createdb = -1 ;
114
+ createdb = TRI_NO ;
114
115
break ;
115
116
case 's' :
116
117
case 'a' :
117
- superuser = +1 ;
118
+ superuser = TRI_YES ;
118
119
break ;
119
120
case 'S' :
120
121
case 'A' :
121
- superuser = -1 ;
122
+ superuser = TRI_NO ;
122
123
break ;
123
124
case 'r' :
124
- createrole = +1 ;
125
+ createrole = TRI_YES ;
125
126
break ;
126
127
case 'R' :
127
- createrole = -1 ;
128
+ createrole = TRI_NO ;
128
129
break ;
129
130
case 'i' :
130
- inherit = +1 ;
131
+ inherit = TRI_YES ;
131
132
break ;
132
133
case 'I' :
133
- inherit = -1 ;
134
+ inherit = TRI_NO ;
134
135
break ;
135
136
case 'l' :
136
- login = +1 ;
137
+ login = TRI_YES ;
137
138
break ;
138
139
case 'L' :
139
- login = -1 ;
140
+ login = TRI_NO ;
140
141
break ;
141
142
case 'c' :
142
143
conn_limit = optarg ;
@@ -145,10 +146,10 @@ main(int argc, char *argv[])
145
146
pwprompt = true;
146
147
break ;
147
148
case 'E' :
148
- encrypted = +1 ;
149
+ encrypted = TRI_YES ;
149
150
break ;
150
151
case 'N' :
151
- encrypted = -1 ;
152
+ encrypted = TRI_NO ;
152
153
break ;
153
154
default :
154
155
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
@@ -195,16 +196,16 @@ main(int argc, char *argv[])
195
196
196
197
reply = simple_prompt ("Shall the new role be a superuser? (y/n) " , 1 , true);
197
198
if (check_yesno_response (reply ) == 1 )
198
- superuser = +1 ;
199
+ superuser = TRI_YES ;
199
200
else
200
- superuser = -1 ;
201
+ superuser = TRI_NO ;
201
202
}
202
203
203
- if (superuser == +1 )
204
+ if (superuser == TRI_YES )
204
205
{
205
206
/* Not much point in trying to restrict a superuser */
206
- createdb = +1 ;
207
- createrole = +1 ;
207
+ createdb = TRI_YES ;
208
+ createrole = TRI_YES ;
208
209
}
209
210
210
211
if (createdb == 0 )
@@ -213,9 +214,9 @@ main(int argc, char *argv[])
213
214
214
215
reply = simple_prompt ("Shall the new role be allowed to create databases? (y/n) " , 1 , true);
215
216
if (check_yesno_response (reply ) == 1 )
216
- createdb = +1 ;
217
+ createdb = TRI_YES ;
217
218
else
218
- createdb = -1 ;
219
+ createdb = TRI_NO ;
219
220
}
220
221
221
222
if (createrole == 0 )
@@ -224,54 +225,48 @@ main(int argc, char *argv[])
224
225
225
226
reply = simple_prompt ("Shall the new role be allowed to create more new roles? (y/n) " , 1 , true);
226
227
if (check_yesno_response (reply ) == 1 )
227
- createrole = +1 ;
228
+ createrole = TRI_YES ;
228
229
else
229
- createrole = -1 ;
230
+ createrole = TRI_NO ;
230
231
}
231
232
232
233
if (inherit == 0 )
233
- {
234
- /* silently default to YES */
235
- inherit = +1 ;
236
- }
234
+ inherit = TRI_YES ;
237
235
238
236
if (login == 0 )
239
- {
240
- /* silently default to YES */
241
- login = +1 ;
242
- }
237
+ login = TRI_YES ;
243
238
244
239
initPQExpBuffer (& sql );
245
240
246
241
printfPQExpBuffer (& sql , "CREATE ROLE %s" , fmtId (newuser ));
247
242
if (newpassword )
248
243
{
249
- if (encrypted == +1 )
244
+ if (encrypted == TRI_YES )
250
245
appendPQExpBuffer (& sql , " ENCRYPTED" );
251
- if (encrypted == -1 )
246
+ if (encrypted == TRI_NO )
252
247
appendPQExpBuffer (& sql , " UNENCRYPTED" );
253
248
appendPQExpBuffer (& sql , " PASSWORD " );
254
249
appendStringLiteral (& sql , newpassword , false);
255
250
}
256
- if (superuser == +1 )
251
+ if (superuser == TRI_YES )
257
252
appendPQExpBuffer (& sql , " SUPERUSER" );
258
- if (superuser == -1 )
253
+ if (superuser == TRI_NO )
259
254
appendPQExpBuffer (& sql , " NOSUPERUSER" );
260
- if (createdb == +1 )
255
+ if (createdb == TRI_YES )
261
256
appendPQExpBuffer (& sql , " CREATEDB" );
262
- if (createdb == -1 )
257
+ if (createdb == TRI_NO )
263
258
appendPQExpBuffer (& sql , " NOCREATEDB" );
264
- if (createrole == +1 )
259
+ if (createrole == TRI_YES )
265
260
appendPQExpBuffer (& sql , " CREATEROLE" );
266
- if (createrole == -1 )
261
+ if (createrole == TRI_NO )
267
262
appendPQExpBuffer (& sql , " NOCREATEROLE" );
268
- if (inherit == +1 )
263
+ if (inherit == TRI_YES )
269
264
appendPQExpBuffer (& sql , " INHERIT" );
270
- if (inherit == -1 )
265
+ if (inherit == TRI_NO )
271
266
appendPQExpBuffer (& sql , " NOINHERIT" );
272
- if (login == +1 )
267
+ if (login == TRI_YES )
273
268
appendPQExpBuffer (& sql , " LOGIN" );
274
- if (login == -1 )
269
+ if (login == TRI_NO )
275
270
appendPQExpBuffer (& sql , " NOLOGIN" );
276
271
if (conn_limit != NULL )
277
272
appendPQExpBuffer (& sql , " CONNECTION LIMIT %s" , conn_limit );
0 commit comments