4 * Description: This module contains miscellaneous routines
5 * such as for debugging/logging and string functions.
11 * Comments: See "notice.txt" for copyright and license information.
25 #include <sys/types.h>
28 #include <process.h> /* Byron: is this where Windows keeps def.
32 extern GLOBAL_VALUES globals;
33 void generate_filename(const char *, const char *, char *);
37 generate_filename(const char *dirname, const char *prefix, char *filename)
42 struct passwd *ptr = 0;
44 ptr = getpwuid(getuid());
47 if (dirname == 0 || filename == 0)
50 strcpy(filename, dirname);
51 strcat(filename, DIRSEPARATOR);
53 strcat(filename, prefix);
55 strcat(filename, ptr->pw_name);
57 sprintf(filename, "%s%u%s", filename, pid, ".log");
61 static int mylog_on = 0, qlog_on = 0;
62 void logs_on_off(int cnopen, int mylog_onoff, int qlog_onoff)
64 static int mylog_on_count = 0, mylog_off_count = 0,
65 qlog_on_count = 0, qlog_off_count = 0;
68 mylog_on_count += cnopen;
70 mylog_off_count += cnopen;
71 if (mylog_on_count > 0)
73 else if (mylog_off_count > 0)
76 mylog_on = globals.debug;
78 qlog_on_count += cnopen;
80 qlog_off_count += cnopen;
81 if (qlog_on_count > 0)
83 else if (qlog_off_count > 0)
86 qlog_on = globals.commlog;
95 static FILE *LOGFP = NULL;
103 generate_filename(MYLOGDIR, MYLOGFILE, filebuf);
104 LOGFP = fopen(filebuf, PG_BINARY_W);
109 vfprintf(LOGFP, fmt, args);
124 static FILE *LOGFP = NULL;
132 generate_filename(QLOGDIR, QLOGFILE, filebuf);
133 LOGFP = fopen(filebuf, PG_BINARY_W);
138 vfprintf(LOGFP, fmt, args);
146 /* Undefine these because windows.h will redefine and cause a warning */
163 * returns STRCPY_FAIL, STRCPY_TRUNCATED, or #bytes copied
164 * (not including null term)
167 my_strcpy(char *dst, int dst_len, const char *src, int src_len)
172 if (src_len == SQL_NULL_DATA)
177 else if (src_len == SQL_NTS)
178 src_len = strlen(src);
184 if (src_len < dst_len)
186 memcpy(dst, src, src_len);
191 memcpy(dst, src, dst_len - 1);
192 dst[dst_len - 1] = '\0'; /* truncated */
193 return STRCPY_TRUNCATED;
202 * strncpy copies up to len characters, and doesn't terminate
203 * the destination string if src has len characters or more.
204 * instead, I want it to copy up to len-1 characters and always
205 * terminate the destination string.
208 strncpy_null(char *dst, const char *src, int len)
215 /* Just in case, check for special lengths */
216 if (len == SQL_NULL_DATA)
221 else if (len == SQL_NTS)
222 len = strlen(src) + 1;
224 for (i = 0; src[i] && i < len - 1; i++)
235 * Create a null terminated string (handling the SQL_NTS thing):
236 * 1. If buf is supplied, place the string in there
237 * (assumes enough space) and return buf.
238 * 2. If buf is not supplied, malloc space and return this string
242 make_string(const char *s, int len, char *buf)
247 if (s && (len > 0 || (len == SQL_NTS && strlen(s) > 0)))
249 length = (len > 0) ? len : strlen(s);
253 strncpy_null(buf, s, length + 1);
257 str = malloc(length + 1);
261 strncpy_null(str, s, length + 1);
270 * Concatenate a single formatted argument to a given buffer handling the SQL_NTS thing.
271 * "fmt" must contain somewhere in it the single form '%.*s'.
272 * This is heavily used in creating queries for info routines (SQLTables, SQLColumns).
273 * This routine could be modified to use vsprintf() to handle multiple arguments.
276 my_strcat(char *buf, const char *fmt, const char *s, int len)
278 if (s && (len > 0 || (len == SQL_NTS && strlen(s) > 0)))
280 int length = (len > 0) ? len : strlen(s);
282 int pos = strlen(buf);
284 sprintf(&buf[pos], fmt, length, s);
292 remove_newlines(char *string)
296 for (i = 0; i < strlen(string); i++)
298 if ((string[i] == '\n') ||
310 for (i = strlen(s) - 1; i >= 0; i--)