6 * Copyright (c) 2010-2012, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/util.c
12 #include "pg_upgrade.h"
22 * Displays the result of an operation (ok, failed, error message,...)
25 report_status(eLogType type, const char *fmt,...)
28 char message[MAX_STRING];
31 vsnprintf(message, sizeof(message), fmt, args);
34 pg_log(type, "%s\n", message);
41 * Displays a message that describes an operation we are about to begin.
42 * We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
43 * "failed" indicators line up nicely.
45 * A typical sequence would look like this:
46 * prep_status("about to flarb the next %d files", fileCount );
48 * if(( message = flarbFiles(fileCount)) == NULL)
49 * report_status(PG_REPORT, "ok" );
51 * pg_log(PG_FATAL, "failed - %s\n", message );
54 prep_status(const char *fmt,...)
57 char message[MAX_STRING];
60 vsnprintf(message, sizeof(message), fmt, args);
63 if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
64 pg_log(PG_REPORT, "%s", message);
66 pg_log(PG_REPORT, "%-" MESSAGE_WIDTH "s", message);
71 pg_log(eLogType type, char *fmt,...)
74 char message[MAX_STRING];
77 vsnprintf(message, sizeof(message), fmt, args);
80 /* PG_VERBOSE is only output in verbose mode */
81 /* fopen() on log_opts.internal might have failed, so check it */
82 if ((type != PG_VERBOSE || log_opts.verbose) && log_opts.internal != NULL)
84 fwrite(message, strlen(message), 1, log_opts.internal);
85 /* if we are using OVERWRITE_MESSAGE, add newline to log file */
86 if (strchr(message, '\r') != NULL)
87 fwrite("\n", 1, 1, log_opts.internal);
88 fflush(log_opts.internal);
95 printf("%s", _(message));
100 printf("%s", _(message));
104 printf("\n%s", _(message));
105 printf("Failure, exiting\n");
120 report_status(PG_REPORT, "ok");
127 * Properly double-quote a SQL identifier.
129 * The result should be pg_free'd, but most callers don't bother because
130 * memory leakage is not a big deal in this program.
133 quote_identifier(const char *s)
135 char *result = pg_malloc(strlen(s) * 2 + 3);
155 * (copied from initdb.c) find the current user
158 get_user_info(char **user_name)
163 struct passwd *pw = getpwuid(geteuid());
166 #else /* the windows code */
172 struct passwd_win32 *pw = &pass_win32;
173 DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
175 GetUserName(pw->pw_name, &pwname_size);
180 *user_name = pg_strdup(pw->pw_name);
192 pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
198 pg_realloc(void *ptr, size_t n)
200 void *p = realloc(ptr, n);
203 pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
218 pg_strdup(const char *s)
220 char *result = strdup(s);
223 pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
232 * Returns the text of the error message for the given error number
234 * This feature is factored into a separate function because it is
238 getErrorText(int errNum)
241 _dosmaperr(GetLastError());
243 return pg_strdup(strerror(errNum));
250 * convert string to oid
253 str2uint(const char *str)
255 return strtoul(str, NULL, 10);
262 * This is like putenv(), but takes two arguments.
263 * It also does unsetenv() if val is NULL.
266 pg_putenv(const char *var, const char *val)
271 char *envstr = (char *) pg_malloc(strlen(var) +
274 sprintf(envstr, "%s=%s", var, val);
278 * Do not free envstr because it becomes part of the environment on
279 * some operating systems. See port/unsetenv.c::unsetenv.
282 SetEnvironmentVariableA(var, val);
290 SetEnvironmentVariableA(var, "");