Replace pg_asprintf() with psprintf().

This eliminates an awkward coding pattern that's also unnecessarily
inconsistent with backend coding.  psprintf() is now the thing to
use everywhere.
This commit is contained in:
Tom Lane 2013-10-22 19:40:26 -04:00
parent 09a89cb5fc
commit 2c66f9924c
19 changed files with 57 additions and 125 deletions

View File

@ -508,7 +508,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
free(comma_filenodes); free(comma_filenodes);
/* now build the query */ /* now build the query */
pg_asprintf(&todo, todo = psprintf(
"SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n" "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
"FROM pg_catalog.pg_class c \n" "FROM pg_catalog.pg_class c \n"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n" " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"

View File

@ -458,10 +458,9 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
prep_status("Creating script to analyze new cluster"); prep_status("Creating script to analyze new cluster");
if (os_info.user_specified) if (os_info.user_specified)
pg_asprintf(&user_specification, "-U \"%s\" ", os_info.user); user_specification = psprintf("-U \"%s\" ", os_info.user);
pg_asprintf(analyze_script_file_name, "analyze_new_cluster.%s", *analyze_script_file_name = psprintf("analyze_new_cluster.%s", SCRIPT_EXT);
SCRIPT_EXT);
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL) if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
pg_fatal("Could not open file \"%s\": %s\n", pg_fatal("Could not open file \"%s\": %s\n",
@ -592,8 +591,7 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
int tblnum; int tblnum;
char old_cluster_pgdata[MAXPGPATH]; char old_cluster_pgdata[MAXPGPATH];
pg_asprintf(deletion_script_file_name, "delete_old_cluster.%s", *deletion_script_file_name = psprintf("delete_old_cluster.%s", SCRIPT_EXT);
SCRIPT_EXT);
/* /*
* Some users (oddly) create tablespaces inside the cluster data * Some users (oddly) create tablespaces inside the cluster data

View File

@ -86,7 +86,8 @@ set_tablespace_directory_suffix(ClusterInfo *cluster)
/* This cluster has a version-specific subdirectory */ /* This cluster has a version-specific subdirectory */
/* The leading slash is needed to start a new directory. */ /* The leading slash is needed to start a new directory. */
pg_asprintf(&cluster->tablespace_suffix, "/PG_%s_%d", cluster->tablespace_suffix = psprintf("/PG_%s_%d",
cluster->major_version_str, cluster->controldata.cat_ver); cluster->major_version_str,
cluster->controldata.cat_ver);
} }
} }

View File

@ -278,7 +278,7 @@ pg_putenv(const char *var, const char *val)
#ifndef WIN32 #ifndef WIN32
char *envstr; char *envstr;
pg_asprintf(&envstr, "%s=%s", var, val); envstr = psprintf("%s=%s", var, val);
putenv(envstr); putenv(envstr);
/* /*

View File

@ -32,10 +32,6 @@ static char *format_type_internal(Oid type_oid, int32 typemod,
bool typemod_given, bool allow_invalid, bool typemod_given, bool allow_invalid,
bool force_qualify); bool force_qualify);
static char *printTypmod(const char *typname, int32 typmod, Oid typmodout); static char *printTypmod(const char *typname, int32 typmod, Oid typmodout);
static char *
psnprintf(size_t len, const char *fmt,...)
/* This lets gcc check the format string for consistency. */
__attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
/* /*
@ -320,7 +316,7 @@ format_type_internal(Oid type_oid, int32 typemod,
} }
if (is_array) if (is_array)
buf = psnprintf(strlen(buf) + 3, "%s[]", buf); buf = psprintf("%s[]", buf);
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
@ -342,8 +338,7 @@ printTypmod(const char *typname, int32 typmod, Oid typmodout)
if (typmodout == InvalidOid) if (typmodout == InvalidOid)
{ {
/* Default behavior: just print the integer typmod with parens */ /* Default behavior: just print the integer typmod with parens */
res = psnprintf(strlen(typname) + MAX_INT32_LEN + 3, "%s(%d)", res = psprintf("%s(%d)", typname, (int) typmod);
typname, (int) typmod);
} }
else else
{ {
@ -352,8 +347,7 @@ printTypmod(const char *typname, int32 typmod, Oid typmodout)
tmstr = DatumGetCString(OidFunctionCall1(typmodout, tmstr = DatumGetCString(OidFunctionCall1(typmodout,
Int32GetDatum(typmod))); Int32GetDatum(typmod)));
res = psnprintf(strlen(typname) + strlen(tmstr) + 1, "%s%s", res = psprintf("%s%s", typname, tmstr);
typname, tmstr);
} }
return res; return res;
@ -448,20 +442,3 @@ oidvectortypes(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(cstring_to_text(result)); PG_RETURN_TEXT_P(cstring_to_text(result));
} }
/* snprintf into a palloc'd string */
static char *
psnprintf(size_t len, const char *fmt,...)
{
va_list ap;
char *buf;
buf = palloc(len);
va_start(ap, fmt);
vsnprintf(buf, len, fmt, ap);
va_end(ap);
return buf;
}

View File

@ -949,7 +949,7 @@ mkdatadir(const char *subdir)
char *path; char *path;
if (subdir) if (subdir)
pg_asprintf(&path, "%s/%s", pg_data, subdir); path = psprintf("%s/%s", pg_data, subdir);
else else
path = pg_strdup(pg_data); path = pg_strdup(pg_data);
@ -969,7 +969,7 @@ mkdatadir(const char *subdir)
static void static void
set_input(char **dest, char *filename) set_input(char **dest, char *filename)
{ {
pg_asprintf(dest, "%s/%s", share_path, filename); *dest = psprintf("%s/%s", share_path, filename);
} }
/* /*
@ -1023,9 +1023,9 @@ write_version_file(char *extrapath)
char *path; char *path;
if (extrapath == NULL) if (extrapath == NULL)
pg_asprintf(&path, "%s/PG_VERSION", pg_data); path = psprintf("%s/PG_VERSION", pg_data);
else else
pg_asprintf(&path, "%s/%s/PG_VERSION", pg_data, extrapath); path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
if ((version_file = fopen(path, PG_BINARY_W)) == NULL) if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
{ {
@ -1053,7 +1053,7 @@ set_null_conf(void)
FILE *conf_file; FILE *conf_file;
char *path; char *path;
pg_asprintf(&path, "%s/postgresql.conf", pg_data); path = psprintf("%s/postgresql.conf", pg_data);
conf_file = fopen(path, PG_BINARY_W); conf_file = fopen(path, PG_BINARY_W);
if (conf_file == NULL) if (conf_file == NULL)
{ {
@ -2951,7 +2951,7 @@ setup_pgdata(void)
* need quotes otherwise on Windows because paths there are most likely to * need quotes otherwise on Windows because paths there are most likely to
* have embedded spaces. * have embedded spaces.
*/ */
pg_asprintf(&pgdata_set_env, "PGDATA=%s", pg_data); pgdata_set_env = psprintf("PGDATA=%s", pg_data);
putenv(pgdata_set_env); putenv(pgdata_set_env);
} }
@ -3345,7 +3345,7 @@ create_xlog_symlink(void)
} }
/* form name of the place where the symlink must go */ /* form name of the place where the symlink must go */
pg_asprintf(&linkloc, "%s/pg_xlog", pg_data); linkloc = psprintf("%s/pg_xlog", pg_data);
#ifdef HAVE_SYMLINK #ifdef HAVE_SYMLINK
if (symlink(xlog_dir, linkloc) != 0) if (symlink(xlog_dir, linkloc) != 0)

View File

@ -2049,7 +2049,7 @@ main(int argc, char **argv)
pgdata_D = pg_strdup(optarg); pgdata_D = pg_strdup(optarg);
canonicalize_path(pgdata_D); canonicalize_path(pgdata_D);
pg_asprintf(&env_var, "PGDATA=%s", pgdata_D); env_var = psprintf("PGDATA=%s", pgdata_D);
putenv(env_var); putenv(env_var);
/* /*
@ -2057,7 +2057,7 @@ main(int argc, char **argv)
* variable but we do -D too for clearer postmaster * variable but we do -D too for clearer postmaster
* 'ps' display * 'ps' display
*/ */
pg_asprintf(&pgdata_opt, "-D \"%s\" ", pgdata_D); pgdata_opt = psprintf("-D \"%s\" ", pgdata_D);
break; break;
} }
case 'l': case 'l':
@ -2098,7 +2098,7 @@ main(int argc, char **argv)
register_username = pg_strdup(optarg); register_username = pg_strdup(optarg);
else else
/* Prepend .\ for local accounts */ /* Prepend .\ for local accounts */
pg_asprintf(&register_username, ".\\%s", optarg); register_username = psprintf(".\\%s", optarg);
break; break;
case 'w': case 'w':
do_wait = true; do_wait = true;

View File

@ -489,7 +489,7 @@ cfopen_read(const char *path, const char *mode)
{ {
char *fname; char *fname;
pg_asprintf(&fname, "%s.gz", path); fname = psprintf("%s.gz", path);
fp = cfopen(fname, mode, 1); fp = cfopen(fname, mode, 1);
free(fname); free(fname);
} }
@ -519,7 +519,7 @@ cfopen_write(const char *path, const char *mode, int compression)
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
char *fname; char *fname;
pg_asprintf(&fname, "%s.gz", path); fname = psprintf("%s.gz", path);
fp = cfopen(fname, mode, 1); fp = cfopen(fname, mode, 1);
free(fname); free(fname);
#else #else

View File

@ -10439,7 +10439,7 @@ convertOperatorReference(Archive *fout, const char *opr)
/* If not schema-qualified, don't need to add OPERATOR() */ /* If not schema-qualified, don't need to add OPERATOR() */
if (!sawdot) if (!sawdot)
return name; return name;
pg_asprintf(&oname, "OPERATOR(%s)", name); oname = psprintf("OPERATOR(%s)", name);
free(name); free(name);
return oname; return oname;
} }
@ -12753,7 +12753,7 @@ dumpTable(Archive *fout, TableInfo *tbinfo)
char *acltag; char *acltag;
attnamecopy = pg_strdup(fmtId(attname)); attnamecopy = pg_strdup(fmtId(attname));
pg_asprintf(&acltag, "%s.%s", tbinfo->dobj.name, attname); acltag = psprintf("%s.%s", tbinfo->dobj.name, attname);
/* Column's GRANT type is always TABLE */ /* Column's GRANT type is always TABLE */
dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE", dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE",
namecopy, attnamecopy, acltag, namecopy, attnamecopy, acltag,

View File

@ -1188,7 +1188,7 @@ exec_command(const char *cmd,
/* Set variable to the value of the next argument */ /* Set variable to the value of the next argument */
char *newval; char *newval;
pg_asprintf(&newval, "%s=%s", envvar, envval); newval = psprintf("%s=%s", envvar, envval);
putenv(newval); putenv(newval);
success = true; success = true;
@ -1549,7 +1549,7 @@ prompt_for_password(const char *username)
{ {
char *prompt_text; char *prompt_text;
pg_asprintf(&prompt_text, _("Password for user %s: "), username); prompt_text = psprintf(_("Password for user %s: "), username);
result = simple_prompt(prompt_text, 100, false); result = simple_prompt(prompt_text, 100, false);
free(prompt_text); free(prompt_text);
} }
@ -1929,17 +1929,17 @@ editFile(const char *fname, int lineno)
*/ */
#ifndef WIN32 #ifndef WIN32
if (lineno > 0) if (lineno > 0)
pg_asprintf(&sys, "exec %s %s%d '%s'", sys = psprintf("exec %s %s%d '%s'",
editorName, editor_lineno_arg, lineno, fname); editorName, editor_lineno_arg, lineno, fname);
else else
pg_asprintf(&sys, "exec %s '%s'", sys = psprintf("exec %s '%s'",
editorName, fname); editorName, fname);
#else #else
if (lineno > 0) if (lineno > 0)
pg_asprintf(&sys, SYSTEMQUOTE "\"%s\" %s%d \"%s\"" SYSTEMQUOTE, sys = psprintf(SYSTEMQUOTE "\"%s\" %s%d \"%s\"" SYSTEMQUOTE,
editorName, editor_lineno_arg, lineno, fname); editorName, editor_lineno_arg, lineno, fname);
else else
pg_asprintf(&sys, SYSTEMQUOTE "\"%s\" \"%s\"" SYSTEMQUOTE, sys = psprintf(SYSTEMQUOTE "\"%s\" \"%s\"" SYSTEMQUOTE,
editorName, fname); editorName, fname);
#endif #endif
result = system(sys); result = system(sys);
@ -2635,9 +2635,9 @@ do_shell(const char *command)
/* See EDITOR handling comment for an explanation */ /* See EDITOR handling comment for an explanation */
#ifndef WIN32 #ifndef WIN32
pg_asprintf(&sys, "exec %s", shellName); sys = psprintf("exec %s", shellName);
#else #else
pg_asprintf(&sys, SYSTEMQUOTE "\"%s\"" SYSTEMQUOTE, shellName); sys = psprintf(SYSTEMQUOTE "\"%s\"" SYSTEMQUOTE, shellName);
#endif #endif
result = system(sys); result = system(sys);
free(sys); free(sys);

View File

@ -594,7 +594,7 @@ StoreQueryTuple(const PGresult *result)
char *value; char *value;
/* concate prefix and column name */ /* concate prefix and column name */
pg_asprintf(&varname, "%s%s", pset.gset_prefix, colname); varname = psprintf("%s%s", pset.gset_prefix, colname);
if (!PQgetisnull(result, 0, i)) if (!PQgetisnull(result, 0, i))
value = PQgetvalue(result, 0, i); value = PQgetvalue(result, 0, i);
@ -1685,7 +1685,7 @@ expand_tilde(char **filename)
{ {
char *newfn; char *newfn;
pg_asprintf(&newfn, "%s%s", home, p); newfn = psprintf("%s%s", home, p);
free(fn); free(fn);
*filename = newfn; *filename = newfn;
} }

View File

@ -79,7 +79,7 @@ xstrcat(char **var, const char *more)
{ {
char *newvar; char *newvar;
pg_asprintf(&newvar, "%s%s", *var, more); newvar = psprintf("%s%s", *var, more);
free(*var); free(*var);
*var = newvar; *var = newvar;
} }

View File

@ -298,7 +298,7 @@ initializeInput(int flags)
if (histfile == NULL) if (histfile == NULL)
{ {
if (get_home_path(home)) if (get_home_path(home))
pg_asprintf(&psql_history, "%s/%s", home, PSQLHISTORY); psql_history = psprintf("%s/%s", home, PSQLHISTORY);
} }
else else
{ {

View File

@ -182,8 +182,8 @@ main(int argc, char *argv[])
if (options.username == NULL) if (options.username == NULL)
password_prompt = pg_strdup(_("Password: ")); password_prompt = pg_strdup(_("Password: "));
else else
pg_asprintf(&password_prompt, _("Password for user %s: "), password_prompt = psprintf(_("Password for user %s: "),
options.username); options.username);
if (pset.getPassword == TRI_YES) if (pset.getPassword == TRI_YES)
password = simple_prompt(password_prompt, 100, false); password = simple_prompt(password_prompt, 100, false);
@ -638,8 +638,8 @@ process_psqlrc_file(char *filename)
#define R_OK 4 #define R_OK 4
#endif #endif
pg_asprintf(&psqlrc_minor, "%s-%s", filename, PG_VERSION); psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
pg_asprintf(&psqlrc_major, "%s-%s", filename, PG_MAJORVERSION); psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
/* check for minor version first, then major, then no version */ /* check for minor version first, then major, then no version */
if (access(psqlrc_minor, R_OK) == 0) if (access(psqlrc_minor, R_OK) == 0)

View File

@ -3832,8 +3832,6 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
for (ptr = pset.vars->next; ptr; ptr = ptr->next) for (ptr = pset.vars->next; ptr; ptr = ptr->next)
{ {
char *buffer;
if (nvars >= maxvars) if (nvars >= maxvars)
{ {
maxvars *= 2; maxvars *= 2;
@ -3846,8 +3844,7 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
} }
} }
pg_asprintf(&buffer, "%s%s%s", prefix, ptr->name, suffix); varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
varnames[nvars++] = buffer;
} }
varnames[nvars] = NULL; varnames[nvars] = NULL;

View File

@ -167,41 +167,3 @@ pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
return len * 2; return len * 2;
} }
/*
* XXX this is going away shortly.
*/
#ifdef FRONTEND
int
pg_asprintf(char **ret, const char *fmt, ...)
{
size_t len = 128; /* initial assumption about buffer size */
for (;;)
{
char *result;
va_list args;
/*
* Allocate result buffer. Note that in frontend this maps to malloc
* with exit-on-error.
*/
result = (char *) palloc(len);
/* Try to format the data. */
va_start(args, fmt);
len = pvsnprintf(result, len, fmt, args);
va_end(args);
if (len == 0)
{
*ret = result;
return 0;
}
/* Release buffer and loop around to try again with larger len. */
pfree(result);
}
}
#endif

View File

@ -14,7 +14,6 @@ extern void *pg_malloc(size_t size);
extern void *pg_malloc0(size_t size); extern void *pg_malloc0(size_t size);
extern void *pg_realloc(void *pointer, size_t size); extern void *pg_realloc(void *pointer, size_t size);
extern void pg_free(void *pointer); extern void pg_free(void *pointer);
extern int pg_asprintf(char **ret, const char *format, ...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
#include "utils/palloc.h" #include "utils/palloc.h"

View File

@ -466,7 +466,7 @@ report_two_error_messages(Step * step1, Step * step2)
{ {
char *prefix; char *prefix;
pg_asprintf(&prefix, "%s %s", step1->name, step2->name); prefix = psprintf("%s %s", step1->name, step2->name);
if (step1->errormsg) if (step1->errormsg)
{ {
@ -794,7 +794,7 @@ try_complete_step(Step * step, int flags)
PG_DIAG_MESSAGE_PRIMARY); PG_DIAG_MESSAGE_PRIMARY);
if (sev && msg) if (sev && msg)
pg_asprintf(&step->errormsg, "%s: %s", sev, msg); step->errormsg = psprintf("%s: %s", sev, msg);
else else
step->errormsg = pg_strdup(PQresultErrorMessage(res)); step->errormsg = pg_strdup(PQresultErrorMessage(res));
} }

View File

@ -656,7 +656,7 @@ doputenv(const char *var, const char *val)
{ {
char *s; char *s;
pg_asprintf(&s, "%s=%s", var, val); s = psprintf("%s=%s", var, val);
putenv(s); putenv(s);
} }
@ -671,10 +671,12 @@ add_to_path(const char *pathname, char separator, const char *addval)
char *newval; char *newval;
if (!oldval || !oldval[0]) if (!oldval || !oldval[0])
{
/* no previous value */ /* no previous value */
pg_asprintf(&newval, "%s=%s", pathname, addval); newval = psprintf("%s=%s", pathname, addval);
}
else else
pg_asprintf(&newval, "%s=%s%c%s", pathname, addval, separator, oldval); newval = psprintf("%s=%s%c%s", pathname, addval, separator, oldval);
putenv(newval); putenv(newval);
} }
@ -685,8 +687,6 @@ add_to_path(const char *pathname, char separator, const char *addval)
static void static void
initialize_environment(void) initialize_environment(void)
{ {
char *tmp;
putenv("PGAPPNAME=pg_regress"); putenv("PGAPPNAME=pg_regress");
if (nolocale) if (nolocale)
@ -742,7 +742,8 @@ initialize_environment(void)
if (!old_pgoptions) if (!old_pgoptions)
old_pgoptions = ""; old_pgoptions = "";
pg_asprintf(&new_pgoptions, "PGOPTIONS=%s %s", old_pgoptions, my_pgoptions); new_pgoptions = psprintf("PGOPTIONS=%s %s",
old_pgoptions, my_pgoptions);
putenv(new_pgoptions); putenv(new_pgoptions);
} }
@ -792,14 +793,11 @@ initialize_environment(void)
/* /*
* Adjust path variables to point into the temp-install tree * Adjust path variables to point into the temp-install tree
*/ */
pg_asprintf(&tmp, "%s/install/%s", temp_install, bindir); bindir = psprintf("%s/install/%s", temp_install, bindir);
bindir = tmp;
pg_asprintf(&tmp, "%s/install/%s", temp_install, libdir); libdir = psprintf("%s/install/%s", temp_install, libdir);
libdir = tmp;
pg_asprintf(&tmp, "%s/install/%s", temp_install, datadir); datadir = psprintf("%s/install/%s", temp_install, datadir);
datadir = tmp;
/* psql will be installed into temp-install bindir */ /* psql will be installed into temp-install bindir */
psqldir = bindir; psqldir = bindir;
@ -954,7 +952,7 @@ spawn_process(const char *cmdline)
*/ */
char *cmdline2; char *cmdline2;
pg_asprintf(&cmdline2, "exec %s", cmdline); cmdline2 = psprintf("exec %s", cmdline);
execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL); execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL);
fprintf(stderr, _("%s: could not exec \"%s\": %s\n"), fprintf(stderr, _("%s: could not exec \"%s\": %s\n"),
progname, shellprog, strerror(errno)); progname, shellprog, strerror(errno));
@ -1031,7 +1029,7 @@ spawn_process(const char *cmdline)
exit(2); exit(2);
} }
pg_asprintf(&cmdline2, "cmd /c %s", cmdline); cmdline2 = psprintf("cmd /c %s", cmdline);
#ifndef __CYGWIN__ #ifndef __CYGWIN__
AddUserToTokenDacl(restrictedToken); AddUserToTokenDacl(restrictedToken);
@ -1852,7 +1850,7 @@ make_absolute_path(const char *in)
} }
} }
pg_asprintf(&result, "%s/%s", cwdbuf, in); result = psprintf("%s/%s", cwdbuf, in);
} }
canonicalize_path(result); canonicalize_path(result);