if (active_branch)
{
- char *opt0 = psql_scan_slash_option(scan_state,
+ char *user = psql_scan_slash_option(scan_state,
OT_SQLID, NULL, true);
char *pw1;
char *pw2;
+ PQExpBufferData buf;
+
+ if (user == NULL)
+ {
+ /* By default, the command applies to CURRENT_USER */
+ PGresult *res;
+
+ res = PSQLexec("SELECT CURRENT_USER");
+ if (!res)
+ return PSQL_CMD_ERROR;
+
+ user = pg_strdup(PQgetvalue(res, 0, 0));
+ PQclear(res);
+ }
- pw1 = simple_prompt("Enter new password: ", false);
+ initPQExpBuffer(&buf);
+ printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
+
+ pw1 = simple_prompt(buf.data, false);
pw2 = simple_prompt("Enter it again: ", false);
if (strcmp(pw1, pw2) != 0)
}
else
{
- char *user;
char *encrypted_password;
- if (opt0)
- user = opt0;
- else
- user = PQuser(pset.db);
-
encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
if (!encrypted_password)
}
else
{
- PQExpBufferData buf;
PGresult *res;
- initPQExpBuffer(&buf);
printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
fmtId(user));
appendStringLiteralConn(&buf, encrypted_password, pset.db);
res = PSQLexec(buf.data);
- termPQExpBuffer(&buf);
if (!res)
success = false;
else
}
}
- if (opt0)
- free(opt0);
+ free(user);
free(pw1);
free(pw2);
+ termPQExpBuffer(&buf);
}
else
ignore_slash_options(scan_state);