Skip to content

Commit f82e2ba

Browse files
committed
Use a proper enum for tri-valued variables.
1 parent 59b89e9 commit f82e2ba

File tree

1 file changed

+48
-53
lines changed

1 file changed

+48
-53
lines changed

src/bin/scripts/createuser.c

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
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 $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -17,6 +17,11 @@
1717

1818
static void help(const char *progname);
1919

20+
enum trivalue {
21+
TRI_DEFAULT,
22+
TRI_NO,
23+
TRI_YES
24+
};
2025

2126
int
2227
main(int argc, char *argv[])
@@ -51,7 +56,6 @@ main(int argc, char *argv[])
5156
const char *progname;
5257
int optindex;
5358
int c;
54-
5559
char *newuser = NULL;
5660
char *host = NULL;
5761
char *port = NULL;
@@ -62,16 +66,13 @@ main(int argc, char *argv[])
6266
char *conn_limit = NULL;
6367
bool pwprompt = false;
6468
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;
7576

7677
PQExpBufferData sql;
7778

@@ -107,36 +108,36 @@ main(int argc, char *argv[])
107108
quiet = true;
108109
break;
109110
case 'd':
110-
createdb = +1;
111+
createdb = TRI_YES;
111112
break;
112113
case 'D':
113-
createdb = -1;
114+
createdb = TRI_NO;
114115
break;
115116
case 's':
116117
case 'a':
117-
superuser = +1;
118+
superuser = TRI_YES;
118119
break;
119120
case 'S':
120121
case 'A':
121-
superuser = -1;
122+
superuser = TRI_NO;
122123
break;
123124
case 'r':
124-
createrole = +1;
125+
createrole = TRI_YES;
125126
break;
126127
case 'R':
127-
createrole = -1;
128+
createrole = TRI_NO;
128129
break;
129130
case 'i':
130-
inherit = +1;
131+
inherit = TRI_YES;
131132
break;
132133
case 'I':
133-
inherit = -1;
134+
inherit = TRI_NO;
134135
break;
135136
case 'l':
136-
login = +1;
137+
login = TRI_YES;
137138
break;
138139
case 'L':
139-
login = -1;
140+
login = TRI_NO;
140141
break;
141142
case 'c':
142143
conn_limit = optarg;
@@ -145,10 +146,10 @@ main(int argc, char *argv[])
145146
pwprompt = true;
146147
break;
147148
case 'E':
148-
encrypted = +1;
149+
encrypted = TRI_YES;
149150
break;
150151
case 'N':
151-
encrypted = -1;
152+
encrypted = TRI_NO;
152153
break;
153154
default:
154155
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
@@ -195,16 +196,16 @@ main(int argc, char *argv[])
195196

196197
reply = simple_prompt("Shall the new role be a superuser? (y/n) ", 1, true);
197198
if (check_yesno_response(reply) == 1)
198-
superuser = +1;
199+
superuser = TRI_YES;
199200
else
200-
superuser = -1;
201+
superuser = TRI_NO;
201202
}
202203

203-
if (superuser == +1)
204+
if (superuser == TRI_YES)
204205
{
205206
/* Not much point in trying to restrict a superuser */
206-
createdb = +1;
207-
createrole = +1;
207+
createdb = TRI_YES;
208+
createrole = TRI_YES;
208209
}
209210

210211
if (createdb == 0)
@@ -213,9 +214,9 @@ main(int argc, char *argv[])
213214

214215
reply = simple_prompt("Shall the new role be allowed to create databases? (y/n) ", 1, true);
215216
if (check_yesno_response(reply) == 1)
216-
createdb = +1;
217+
createdb = TRI_YES;
217218
else
218-
createdb = -1;
219+
createdb = TRI_NO;
219220
}
220221

221222
if (createrole == 0)
@@ -224,54 +225,48 @@ main(int argc, char *argv[])
224225

225226
reply = simple_prompt("Shall the new role be allowed to create more new roles? (y/n) ", 1, true);
226227
if (check_yesno_response(reply) == 1)
227-
createrole = +1;
228+
createrole = TRI_YES;
228229
else
229-
createrole = -1;
230+
createrole = TRI_NO;
230231
}
231232

232233
if (inherit == 0)
233-
{
234-
/* silently default to YES */
235-
inherit = +1;
236-
}
234+
inherit = TRI_YES;
237235

238236
if (login == 0)
239-
{
240-
/* silently default to YES */
241-
login = +1;
242-
}
237+
login = TRI_YES;
243238

244239
initPQExpBuffer(&sql);
245240

246241
printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
247242
if (newpassword)
248243
{
249-
if (encrypted == +1)
244+
if (encrypted == TRI_YES)
250245
appendPQExpBuffer(&sql, " ENCRYPTED");
251-
if (encrypted == -1)
246+
if (encrypted == TRI_NO)
252247
appendPQExpBuffer(&sql, " UNENCRYPTED");
253248
appendPQExpBuffer(&sql, " PASSWORD ");
254249
appendStringLiteral(&sql, newpassword, false);
255250
}
256-
if (superuser == +1)
251+
if (superuser == TRI_YES)
257252
appendPQExpBuffer(&sql, " SUPERUSER");
258-
if (superuser == -1)
253+
if (superuser == TRI_NO)
259254
appendPQExpBuffer(&sql, " NOSUPERUSER");
260-
if (createdb == +1)
255+
if (createdb == TRI_YES)
261256
appendPQExpBuffer(&sql, " CREATEDB");
262-
if (createdb == -1)
257+
if (createdb == TRI_NO)
263258
appendPQExpBuffer(&sql, " NOCREATEDB");
264-
if (createrole == +1)
259+
if (createrole == TRI_YES)
265260
appendPQExpBuffer(&sql, " CREATEROLE");
266-
if (createrole == -1)
261+
if (createrole == TRI_NO)
267262
appendPQExpBuffer(&sql, " NOCREATEROLE");
268-
if (inherit == +1)
263+
if (inherit == TRI_YES)
269264
appendPQExpBuffer(&sql, " INHERIT");
270-
if (inherit == -1)
265+
if (inherit == TRI_NO)
271266
appendPQExpBuffer(&sql, " NOINHERIT");
272-
if (login == +1)
267+
if (login == TRI_YES)
273268
appendPQExpBuffer(&sql, " LOGIN");
274-
if (login == -1)
269+
if (login == TRI_NO)
275270
appendPQExpBuffer(&sql, " NOLOGIN");
276271
if (conn_limit != NULL)
277272
appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy