aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2013-10-22 19:40:26 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2013-10-22 19:40:26 -0400
commit2c66f9924c1162bfba27c77004ccf42fb6ea188d (patch)
tree920c20776ef6d13d7a5d3a836202d897abcaf428 /src
parent09a89cb5fc29b47c26d151e82293fd3bef592b7b (diff)
downloadpostgresql-2c66f9924c1162bfba27c77004ccf42fb6ea188d.tar.gz
postgresql-2c66f9924c1162bfba27c77004ccf42fb6ea188d.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/adt/format_type.c29
-rw-r--r--src/bin/initdb/initdb.c14
-rw-r--r--src/bin/pg_ctl/pg_ctl.c6
-rw-r--r--src/bin/pg_dump/compress_io.c4
-rw-r--r--src/bin/pg_dump/pg_dump.c4
-rw-r--r--src/bin/psql/command.c16
-rw-r--r--src/bin/psql/common.c4
-rw-r--r--src/bin/psql/copy.c2
-rw-r--r--src/bin/psql/input.c2
-rw-r--r--src/bin/psql/startup.c8
-rw-r--r--src/bin/psql/tab-complete.c5
-rw-r--r--src/common/psprintf.c38
-rw-r--r--src/include/common/fe_memutils.h1
-rw-r--r--src/test/isolation/isolationtester.c4
-rw-r--r--src/test/regress/pg_regress.c28
15 files changed, 49 insertions, 116 deletions
diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c
index cd164c7e7ea..21d4f8dc555 100644
--- a/src/backend/utils/adt/format_type.c
+++ b/src/backend/utils/adt/format_type.c
@@ -32,10 +32,6 @@ static char *format_type_internal(Oid type_oid, int32 typemod,
bool typemod_given, bool allow_invalid,
bool force_qualify);
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)
- buf = psnprintf(strlen(buf) + 3, "%s[]", buf);
+ buf = psprintf("%s[]", buf);
ReleaseSysCache(tuple);
@@ -342,8 +338,7 @@ printTypmod(const char *typname, int32 typmod, Oid typmodout)
if (typmodout == InvalidOid)
{
/* Default behavior: just print the integer typmod with parens */
- res = psnprintf(strlen(typname) + MAX_INT32_LEN + 3, "%s(%d)",
- typname, (int) typmod);
+ res = psprintf("%s(%d)", typname, (int) typmod);
}
else
{
@@ -352,8 +347,7 @@ printTypmod(const char *typname, int32 typmod, Oid typmodout)
tmstr = DatumGetCString(OidFunctionCall1(typmodout,
Int32GetDatum(typmod)));
- res = psnprintf(strlen(typname) + strlen(tmstr) + 1, "%s%s",
- typname, tmstr);
+ res = psprintf("%s%s", typname, tmstr);
}
return res;
@@ -448,20 +442,3 @@ oidvectortypes(PG_FUNCTION_ARGS)
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;
-}
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index f7073e26bd7..3983b237313 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -949,7 +949,7 @@ mkdatadir(const char *subdir)
char *path;
if (subdir)
- pg_asprintf(&path, "%s/%s", pg_data, subdir);
+ path = psprintf("%s/%s", pg_data, subdir);
else
path = pg_strdup(pg_data);
@@ -969,7 +969,7 @@ mkdatadir(const char *subdir)
static void
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;
if (extrapath == NULL)
- pg_asprintf(&path, "%s/PG_VERSION", pg_data);
+ path = psprintf("%s/PG_VERSION", pg_data);
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)
{
@@ -1053,7 +1053,7 @@ set_null_conf(void)
FILE *conf_file;
char *path;
- pg_asprintf(&path, "%s/postgresql.conf", pg_data);
+ path = psprintf("%s/postgresql.conf", pg_data);
conf_file = fopen(path, PG_BINARY_W);
if (conf_file == NULL)
{
@@ -2951,7 +2951,7 @@ setup_pgdata(void)
* need quotes otherwise on Windows because paths there are most likely to
* have embedded spaces.
*/
- pg_asprintf(&pgdata_set_env, "PGDATA=%s", pg_data);
+ pgdata_set_env = psprintf("PGDATA=%s", pg_data);
putenv(pgdata_set_env);
}
@@ -3345,7 +3345,7 @@ create_xlog_symlink(void)
}
/* 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
if (symlink(xlog_dir, linkloc) != 0)
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index be51dc62ca7..8399cdd57e3 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -2049,7 +2049,7 @@ main(int argc, char **argv)
pgdata_D = pg_strdup(optarg);
canonicalize_path(pgdata_D);
- pg_asprintf(&env_var, "PGDATA=%s", pgdata_D);
+ env_var = psprintf("PGDATA=%s", pgdata_D);
putenv(env_var);
/*
@@ -2057,7 +2057,7 @@ main(int argc, char **argv)
* variable but we do -D too for clearer postmaster
* 'ps' display
*/
- pg_asprintf(&pgdata_opt, "-D \"%s\" ", pgdata_D);
+ pgdata_opt = psprintf("-D \"%s\" ", pgdata_D);
break;
}
case 'l':
@@ -2098,7 +2098,7 @@ main(int argc, char **argv)
register_username = pg_strdup(optarg);
else
/* Prepend .\ for local accounts */
- pg_asprintf(&register_username, ".\\%s", optarg);
+ register_username = psprintf(".\\%s", optarg);
break;
case 'w':
do_wait = true;
diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c
index d859c8ee1e9..dd62fa013c0 100644
--- a/src/bin/pg_dump/compress_io.c
+++ b/src/bin/pg_dump/compress_io.c
@@ -489,7 +489,7 @@ cfopen_read(const char *path, const char *mode)
{
char *fname;
- pg_asprintf(&fname, "%s.gz", path);
+ fname = psprintf("%s.gz", path);
fp = cfopen(fname, mode, 1);
free(fname);
}
@@ -519,7 +519,7 @@ cfopen_write(const char *path, const char *mode, int compression)
#ifdef HAVE_LIBZ
char *fname;
- pg_asprintf(&fname, "%s.gz", path);
+ fname = psprintf("%s.gz", path);
fp = cfopen(fname, mode, 1);
free(fname);
#else
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 01c63b13348..bc70dd6a6ed 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -10439,7 +10439,7 @@ convertOperatorReference(Archive *fout, const char *opr)
/* If not schema-qualified, don't need to add OPERATOR() */
if (!sawdot)
return name;
- pg_asprintf(&oname, "OPERATOR(%s)", name);
+ oname = psprintf("OPERATOR(%s)", name);
free(name);
return oname;
}
@@ -12753,7 +12753,7 @@ dumpTable(Archive *fout, TableInfo *tbinfo)
char *acltag;
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 */
dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE",
namecopy, attnamecopy, acltag,
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 06ed56be683..0daac61e01c 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1188,7 +1188,7 @@ exec_command(const char *cmd,
/* Set variable to the value of the next argument */
char *newval;
- pg_asprintf(&newval, "%s=%s", envvar, envval);
+ newval = psprintf("%s=%s", envvar, envval);
putenv(newval);
success = true;
@@ -1549,7 +1549,7 @@ prompt_for_password(const char *username)
{
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);
free(prompt_text);
}
@@ -1929,17 +1929,17 @@ editFile(const char *fname, int lineno)
*/
#ifndef WIN32
if (lineno > 0)
- pg_asprintf(&sys, "exec %s %s%d '%s'",
+ sys = psprintf("exec %s %s%d '%s'",
editorName, editor_lineno_arg, lineno, fname);
else
- pg_asprintf(&sys, "exec %s '%s'",
+ sys = psprintf("exec %s '%s'",
editorName, fname);
#else
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);
else
- pg_asprintf(&sys, SYSTEMQUOTE "\"%s\" \"%s\"" SYSTEMQUOTE,
+ sys = psprintf(SYSTEMQUOTE "\"%s\" \"%s\"" SYSTEMQUOTE,
editorName, fname);
#endif
result = system(sys);
@@ -2635,9 +2635,9 @@ do_shell(const char *command)
/* See EDITOR handling comment for an explanation */
#ifndef WIN32
- pg_asprintf(&sys, "exec %s", shellName);
+ sys = psprintf("exec %s", shellName);
#else
- pg_asprintf(&sys, SYSTEMQUOTE "\"%s\"" SYSTEMQUOTE, shellName);
+ sys = psprintf(SYSTEMQUOTE "\"%s\"" SYSTEMQUOTE, shellName);
#endif
result = system(sys);
free(sys);
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 71f0c8a95a8..bbdafab5e05 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -594,7 +594,7 @@ StoreQueryTuple(const PGresult *result)
char *value;
/* 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))
value = PQgetvalue(result, 0, i);
@@ -1685,7 +1685,7 @@ expand_tilde(char **filename)
{
char *newfn;
- pg_asprintf(&newfn, "%s%s", home, p);
+ newfn = psprintf("%s%s", home, p);
free(fn);
*filename = newfn;
}
diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c
index 6db063ca953..a0c19a8de48 100644
--- a/src/bin/psql/copy.c
+++ b/src/bin/psql/copy.c
@@ -79,7 +79,7 @@ xstrcat(char **var, const char *more)
{
char *newvar;
- pg_asprintf(&newvar, "%s%s", *var, more);
+ newvar = psprintf("%s%s", *var, more);
free(*var);
*var = newvar;
}
diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c
index f2b6e4ed7fa..29f2fa12d30 100644
--- a/src/bin/psql/input.c
+++ b/src/bin/psql/input.c
@@ -298,7 +298,7 @@ initializeInput(int flags)
if (histfile == NULL)
{
if (get_home_path(home))
- pg_asprintf(&psql_history, "%s/%s", home, PSQLHISTORY);
+ psql_history = psprintf("%s/%s", home, PSQLHISTORY);
}
else
{
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index a45ec552f42..a9836a59a81 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -182,8 +182,8 @@ main(int argc, char *argv[])
if (options.username == NULL)
password_prompt = pg_strdup(_("Password: "));
else
- pg_asprintf(&password_prompt, _("Password for user %s: "),
- options.username);
+ password_prompt = psprintf(_("Password for user %s: "),
+ options.username);
if (pset.getPassword == TRI_YES)
password = simple_prompt(password_prompt, 100, false);
@@ -638,8 +638,8 @@ process_psqlrc_file(char *filename)
#define R_OK 4
#endif
- pg_asprintf(&psqlrc_minor, "%s-%s", filename, PG_VERSION);
- pg_asprintf(&psqlrc_major, "%s-%s", filename, PG_MAJORVERSION);
+ psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
+ psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
/* check for minor version first, then major, then no version */
if (access(psqlrc_minor, R_OK) == 0)
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index ae8f8370f95..84d2eb4d420 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3832,8 +3832,6 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
for (ptr = pset.vars->next; ptr; ptr = ptr->next)
{
- char *buffer;
-
if (nvars >= maxvars)
{
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++] = buffer;
+ varnames[nvars++] = psprintf("%s%s%s", prefix, ptr->name, suffix);
}
varnames[nvars] = NULL;
diff --git a/src/common/psprintf.c b/src/common/psprintf.c
index 87fd013f840..788c8f0d697 100644
--- a/src/common/psprintf.c
+++ b/src/common/psprintf.c
@@ -167,41 +167,3 @@ pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
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
diff --git a/src/include/common/fe_memutils.h b/src/include/common/fe_memutils.h
index db4e710b465..82ed8cd9e64 100644
--- a/src/include/common/fe_memutils.h
+++ b/src/include/common/fe_memutils.h
@@ -14,7 +14,6 @@ extern void *pg_malloc(size_t size);
extern void *pg_malloc0(size_t size);
extern void *pg_realloc(void *pointer, size_t size);
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"
diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index 7a3572f0c17..9679e6a6e6b 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -466,7 +466,7 @@ report_two_error_messages(Step * step1, Step * step2)
{
char *prefix;
- pg_asprintf(&prefix, "%s %s", step1->name, step2->name);
+ prefix = psprintf("%s %s", step1->name, step2->name);
if (step1->errormsg)
{
@@ -794,7 +794,7 @@ try_complete_step(Step * step, int flags)
PG_DIAG_MESSAGE_PRIMARY);
if (sev && msg)
- pg_asprintf(&step->errormsg, "%s: %s", sev, msg);
+ step->errormsg = psprintf("%s: %s", sev, msg);
else
step->errormsg = pg_strdup(PQresultErrorMessage(res));
}
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index e51d08878da..02cb5f4a796 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -656,7 +656,7 @@ doputenv(const char *var, const char *val)
{
char *s;
- pg_asprintf(&s, "%s=%s", var, val);
+ s = psprintf("%s=%s", var, val);
putenv(s);
}
@@ -671,10 +671,12 @@ add_to_path(const char *pathname, char separator, const char *addval)
char *newval;
if (!oldval || !oldval[0])
+ {
/* no previous value */
- pg_asprintf(&newval, "%s=%s", pathname, addval);
+ newval = psprintf("%s=%s", pathname, addval);
+ }
else
- pg_asprintf(&newval, "%s=%s%c%s", pathname, addval, separator, oldval);
+ newval = psprintf("%s=%s%c%s", pathname, addval, separator, oldval);
putenv(newval);
}
@@ -685,8 +687,6 @@ add_to_path(const char *pathname, char separator, const char *addval)
static void
initialize_environment(void)
{
- char *tmp;
-
putenv("PGAPPNAME=pg_regress");
if (nolocale)
@@ -742,7 +742,8 @@ initialize_environment(void)
if (!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);
}
@@ -792,14 +793,11 @@ initialize_environment(void)
/*
* Adjust path variables to point into the temp-install tree
*/
- pg_asprintf(&tmp, "%s/install/%s", temp_install, bindir);
- bindir = tmp;
+ bindir = psprintf("%s/install/%s", temp_install, bindir);
- pg_asprintf(&tmp, "%s/install/%s", temp_install, libdir);
- libdir = tmp;
+ libdir = psprintf("%s/install/%s", temp_install, libdir);
- pg_asprintf(&tmp, "%s/install/%s", temp_install, datadir);
- datadir = tmp;
+ datadir = psprintf("%s/install/%s", temp_install, datadir);
/* psql will be installed into temp-install bindir */
psqldir = bindir;
@@ -954,7 +952,7 @@ spawn_process(const char *cmdline)
*/
char *cmdline2;
- pg_asprintf(&cmdline2, "exec %s", cmdline);
+ cmdline2 = psprintf("exec %s", cmdline);
execl(shellprog, shellprog, "-c", cmdline2, (char *) NULL);
fprintf(stderr, _("%s: could not exec \"%s\": %s\n"),
progname, shellprog, strerror(errno));
@@ -1031,7 +1029,7 @@ spawn_process(const char *cmdline)
exit(2);
}
- pg_asprintf(&cmdline2, "cmd /c %s", cmdline);
+ cmdline2 = psprintf("cmd /c %s", cmdline);
#ifndef __CYGWIN__
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);