Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 714384a

Browse files
committed
Polish error messages, help output, give marginal clue about command line
option conventions. (E.g., "pg_passwd -?" should not write to a file named "-?".)
1 parent f999912 commit 714384a

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

src/bin/pg_passwd/pg_passwd.c

+51-24
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ extern char *crypt(const char *, const char *);
1818

1919
#endif
2020

21-
char *comname;
22-
static void usage(FILE *stream);
21+
#define PG_PASSWD_LEN 13 /* not including null */
22+
23+
const char * progname;
24+
25+
static void usage(void);
2326
static void read_pwd_file(char *filename);
2427
static void write_pwd_file(char *filename, char *bkname);
25-
static void encrypt_pwd(char key[9], char salt[3], char passwd[14]);
28+
static void encrypt_pwd(char key[9], char salt[3], char passwd[PG_PASSWD_LEN+1]);
2629
static void prompt_for_username(char *username);
2730
static void prompt_for_password(char *prompt, char *password);
2831

2932
static void
30-
usage(FILE *stream)
33+
usage(void)
3134
{
32-
fprintf(stream, "Usage: %s <password file>\n", comname);
35+
printf("%s manipulates flat text password files for PostgreSQL.\n\n", progname);
36+
printf("Usage:\n %s PASSWORD-FILE\n\n", progname);
37+
printf("Report bugs to <pgsql-bugs@postgresql.org>.\n");
3338
}
3439

3540
typedef struct
@@ -97,8 +102,8 @@ read_pwd_file(char *filename)
97102
if (line[l - 1] == '\n')
98103
line[l - 1] = '\0';
99104
else
100-
{ /* too long */
101-
fprintf(stderr, "%s: line %d: line too long.\n",
105+
{
106+
fprintf(stderr, "%s:%d: line too long\n",
102107
filename, npwds + 1);
103108
exit(1);
104109
}
@@ -110,7 +115,7 @@ read_pwd_file(char *filename)
110115

111116
if (strlen(p) == 0)
112117
{
113-
fprintf(stderr, "%s: line %d: null user name.\n",
118+
fprintf(stderr, "%s:%d: null user name\n",
114119
filename, npwds + 1);
115120
exit(1);
116121
}
@@ -121,7 +126,8 @@ read_pwd_file(char *filename)
121126
{
122127
if (strcmp(pwds[i].uname, pwds[npwds].uname) == 0)
123128
{
124-
fprintf(stderr, "%s: duplicated entry.\n", pwds[npwds].uname);
129+
fprintf(stderr, "Duplicated entry: %s\n",
130+
pwds[npwds].uname);
125131
exit(1);
126132
}
127133
}
@@ -135,9 +141,9 @@ read_pwd_file(char *filename)
135141
if (q != NULL)
136142
*(q++) = '\0';
137143

138-
if (strlen(p) != 13 && strcmp(p, "+")!=0)
144+
if (strlen(p) != PG_PASSWD_LEN && strcmp(p, "+")!=0)
139145
{
140-
fprintf(stderr, "WARNING: %s: line %d: invalid password length.\n",
146+
fprintf(stderr, "%s:%d: warning: invalid password length\n",
141147
filename, npwds + 1);
142148
}
143149
pwds[npwds].pwd = strdup(p);
@@ -201,7 +207,7 @@ write_pwd_file(char *filename, char *bkname)
201207
}
202208

203209
static void
204-
encrypt_pwd(char key[9], char salt[3], char passwd[14])
210+
encrypt_pwd(char key[9], char salt[3], char passwd[PG_PASSWD_LEN + 1])
205211
{
206212
int n;
207213

@@ -233,17 +239,17 @@ encrypt_pwd(char key[9], char salt[3], char passwd[14])
233239

234240
#ifdef NOT_USED
235241
static int
236-
check_pwd(char key[9], char passwd[14])
242+
check_pwd(char key[9], char passwd[PG_PASSWD_LEN + 1])
237243
{
238-
char shouldbe[14];
244+
char shouldbe[PG_PASSWD_LEN + 1];
239245
char salt[3];
240246

241247
salt[0] = passwd[0];
242248
salt[1] = passwd[1];
243249
salt[2] = '\0';
244250
encrypt_pwd(key, salt, shouldbe);
245251

246-
return strncmp(shouldbe, passwd, 13) == 0 ? 1 : 0;
252+
return strncmp(shouldbe, passwd, PG_PASSWD_LEN) == 0 ? 1 : 0;
247253
}
248254

249255
#endif
@@ -314,32 +320,53 @@ prompt_for_password(char *prompt, char *password)
314320
int
315321
main(int argc, char *argv[])
316322
{
317-
static char bkname[512];
323+
static char bkname[MAXPGPATH];
324+
char *filename;
318325
char username[9];
319326
char salt[3];
320327
char key[9],
321328
key2[9];
322-
char e_passwd[14];
329+
char e_passwd[PG_PASSWD_LEN + 1];
323330
int i;
324331

325-
comname = argv[0];
332+
progname = argv[0];
333+
326334
if (argc != 2)
327335
{
328-
usage(stderr);
336+
fprintf(stderr, "%s: too %s arguments\nTry '%s -?' for help.\n",
337+
progname, argc > 2 ? "many" : "few", progname);
338+
exit(1);
339+
}
340+
341+
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
342+
{
343+
usage();
344+
exit(0);
345+
}
346+
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
347+
{
348+
puts("pg_passwd (PostgreSQL) " PG_VERSION);
349+
exit(0);
350+
}
351+
if (argv[1][0] == '-')
352+
{
353+
fprintf(stderr, "%s: invalid option: %s\nTry '%s -?' for help.\n",
354+
progname, argv[1], progname);
329355
exit(1);
330356
}
331357

358+
filename = argv[1];
332359

333360
/* open file */
334-
read_pwd_file(argv[1]);
361+
read_pwd_file(filename);
335362

336363
/* ask for the user name and the password */
337364
prompt_for_username(username);
338365
prompt_for_password("New password: ", key);
339366
prompt_for_password("Re-enter new password: ", key2);
340367
if (strncmp(key, key2, 8) != 0)
341368
{
342-
fprintf(stderr, "Password mismatch.\n");
369+
fprintf(stderr, "Password mismatch\n");
343370
exit(1);
344371
}
345372
salt[0] = '\0';
@@ -358,7 +385,7 @@ main(int argc, char *argv[])
358385
{ /* did not exist */
359386
if (npwds == MAXPWDS)
360387
{
361-
fprintf(stderr, "%s: cannot handle so may entries.\n", comname);
388+
fprintf(stderr, "Cannot handle so may entries\n");
362389
exit(1);
363390
}
364391
pwds[npwds].uname = strdup(username);
@@ -368,8 +395,8 @@ main(int argc, char *argv[])
368395
}
369396

370397
/* write back the file */
371-
sprintf(bkname, "%s.bk", argv[1]);
372-
write_pwd_file(argv[1], bkname);
398+
sprintf(bkname, "%s.bk", filename);
399+
write_pwd_file(filename, bkname);
373400

374401
return 0;
375402
}

0 commit comments

Comments
 (0)