]> granicus.if.org Git - postgresql/commitdiff
Replace pg_asprintf() with psprintf().
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 22 Oct 2013 23:40:26 +0000 (19:40 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 22 Oct 2013 23:40:26 +0000 (19:40 -0400)
This eliminates an awkward coding pattern that's also unnecessarily
inconsistent with backend coding.  psprintf() is now the thing to
use everywhere.

19 files changed:
contrib/oid2name/oid2name.c
contrib/pg_upgrade/check.c
contrib/pg_upgrade/tablespace.c
contrib/pg_upgrade/util.c
src/backend/utils/adt/format_type.c
src/bin/initdb/initdb.c
src/bin/pg_ctl/pg_ctl.c
src/bin/pg_dump/compress_io.c
src/bin/pg_dump/pg_dump.c
src/bin/psql/command.c
src/bin/psql/common.c
src/bin/psql/copy.c
src/bin/psql/input.c
src/bin/psql/startup.c
src/bin/psql/tab-complete.c
src/common/psprintf.c
src/include/common/fe_memutils.h
src/test/isolation/isolationtester.c
src/test/regress/pg_regress.c

index ab92c637e591c4fcacf19e8587760bbb41faac25..67d79346b22e84187dacc7ccb79da5721f14b789 100644 (file)
@@ -508,7 +508,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
        free(comma_filenodes);
 
        /* 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"
                         "FROM pg_catalog.pg_class c \n"
                 "      LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"
index 464bbe2604fa7727ca20d2548b83411bce5b8ff3..936306ce618e13f117179d4bae2933529076f736 100644 (file)
@@ -458,10 +458,9 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
        prep_status("Creating script to analyze new cluster");
 
        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",
-                               SCRIPT_EXT);
+       *analyze_script_file_name = psprintf("analyze_new_cluster.%s", SCRIPT_EXT);
 
        if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
                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;
        char            old_cluster_pgdata[MAXPGPATH];
 
-       pg_asprintf(deletion_script_file_name, "delete_old_cluster.%s",
-                               SCRIPT_EXT);
+       *deletion_script_file_name = psprintf("delete_old_cluster.%s", SCRIPT_EXT);
 
        /*
         * Some users (oddly) create tablespaces inside the cluster data
index f090f62b29cc7fe3125171514d5f7844184a0331..27273662dd2432bd90ab1e6b6c9ed2bd6b83084d 100644 (file)
@@ -86,7 +86,8 @@ set_tablespace_directory_suffix(ClusterInfo *cluster)
                /* This cluster has a version-specific subdirectory */
 
                /* The leading slash is needed to start a new directory. */
-               pg_asprintf(&cluster->tablespace_suffix, "/PG_%s_%d",
-                                       cluster->major_version_str,     cluster->controldata.cat_ver);
+               cluster->tablespace_suffix = psprintf("/PG_%s_%d",
+                                                                                         cluster->major_version_str,
+                                                                                         cluster->controldata.cat_ver);
        }
 }
index 7bca19b84d0f130b0785d636d528a5bf104b25ff..a67bd64043f1344f760f8d397f91800d2873412e 100644 (file)
@@ -278,7 +278,7 @@ pg_putenv(const char *var, const char *val)
 #ifndef WIN32
                char       *envstr;
 
-               pg_asprintf(&envstr, "%s=%s", var, val);
+               envstr = psprintf("%s=%s", var, val);
                putenv(envstr);
 
                /*
index cd164c7e7ea7b7267db5cf8c4fde774a2dfd37a8..21d4f8dc555836ccfca4f5c145b38d4d27ec0642 100644 (file)
@@ -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;
-}
index f7073e26bd7228fe873f36451da49de2f7882d62..3983b23731330b78a66a74d14faaf76f7aff85c2 100644 (file)
@@ -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)
index be51dc62ca740aa57552ca137d9849b8688cc127..8399cdd57e3520ec78648a9e1503bf79e8b90bc5 100644 (file)
@@ -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;
index d859c8ee1e96d96fffd5f6b21e1f576f0d6726bf..dd62fa013c0c9412bf094aa536590b3400b71434 100644 (file)
@@ -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
index 01c63b133483c0d6a6bbd66490631bc997da9b36..bc70dd6a6ed2f8e38b728228b373ccb559fdd03e 100644 (file)
@@ -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,
index 06ed56be683635eb1200745a921575b62ed4611d..0daac61e01c9a9509b4d5849a48b96e65ef84e04 100644 (file)
@@ -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);
index 71f0c8a95a836aafeea289706302c85e54456ee5..bbdafab5e0507e7c40adf2d0f36a384253895b7a 100644 (file)
@@ -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;
                }
index 6db063ca95371d8fad9bde84f7c3025005fd7bad..a0c19a8de485fbae9ce9ba7034af1bd967bd516a 100644 (file)
@@ -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;
 }
index f2b6e4ed7fa6764879e729588618168281781f70..29f2fa12d300395d575f250771de679b13dc35f7 100644 (file)
@@ -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
                {
index a45ec552f4253aec06e4ce34bc124027557b1dc9..a9836a59a818f78163d65a88eb0ce0db2db479ef 100644 (file)
@@ -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)
index ae8f8370f9559a060302159e13c081b0df1d6b18..84d2eb4d4209a2cfa2c3c6e8bcc0be48e87ac008 100644 (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)
        {
-               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;
index 87fd013f8401bbf9f8efe3b58ab9a84addaa82c6..788c8f0d69762b9d3ab5e4ba8f9d0536661dca1e 100644 (file)
@@ -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
index db4e710b465e8a035b9edd523fdcbfb2a8651b59..82ed8cd9e64c3c2b0002661de1f5daccb801400a 100644 (file)
@@ -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"
 
index 7a3572f0c17b2572760e1fa65c6cc46861d83556..9679e6a6e6b630375d425e9224056661807e33ad 100644 (file)
@@ -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));
                                }
index e51d08878dacf848b47376e58e2f776aafae913a..02cb5f4a7962c17832d2fbf19f38e9fa796a71ec 100644 (file)
@@ -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);