6 * Copyright (c) 2010-2011, 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 if (log_opts.fd != NULL)
82 fwrite(message, strlen(message), 1, log_opts.fd);
83 /* if we are using OVERWRITE_MESSAGE, add newline */
84 if (strchr(message, '\r') != NULL)
85 fwrite("\n", 1, 1, log_opts.fd);
93 printf("%s", _(message));
98 printf("%s", _(message));
102 printf("\n%s", _(message));
103 printf("Failure, exiting\n");
109 fprintf(log_opts.debug_fd, "%s\n", _(message));
123 report_status(PG_REPORT, "ok");
130 * Properly double-quote a SQL identifier.
132 * The result should be pg_free'd, but most callers don't bother because
133 * memory leakage is not a big deal in this program.
136 quote_identifier(const char *s)
138 char *result = pg_malloc(strlen(s) * 2 + 3);
158 * (copied from initdb.c) find the current user
161 get_user_info(char **user_name)
166 struct passwd *pw = getpwuid(geteuid());
169 #else /* the windows code */
175 struct passwd_win32 *pw = &pass_win32;
176 DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
178 GetUserName(pw->pw_name, &pwname_size);
183 *user_name = pg_strdup(pw->pw_name);
195 pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
210 pg_strdup(const char *s)
212 char *result = strdup(s);
215 pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
224 * Returns the text of the error message for the given error number
226 * This feature is factored into a separate function because it is
230 getErrorText(int errNum)
233 _dosmaperr(GetLastError());
235 return pg_strdup(strerror(errNum));
242 * convert string to oid
245 str2uint(const char *str)
247 return strtoul(str, NULL, 10);
254 * This is like putenv(), but takes two arguments.
255 * It also does unsetenv() if val is NULL.
258 pg_putenv(const char *var, const char *val)
263 char *envstr = (char *) pg_malloc(strlen(var) +
266 sprintf(envstr, "%s=%s", var, val);
270 * Do not free envstr because it becomes part of the environment on
271 * some operating systems. See port/unsetenv.c::unsetenv.
274 SetEnvironmentVariableA(var, val);
282 SetEnvironmentVariableA(var, "");