4
* $Id: misc.c 2613 1999-06-01 15:32:12Z VZ $
6
* Miscellaneous functions
8
* The iODBC driver manager.
10
* Copyright (C) 1995 by Ke Jin <kejin@empress.com>
12
* This library is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU Library General Public
14
* License as published by the Free Software Foundation; either
15
* version 2 of the License, or (at your option) any later version.
17
* This library is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
* Library General Public License for more details.
22
* You should have received a copy of the GNU Library General Public
23
* License along with this library; if not, write to the Free
24
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44
for (i = 1; i < n; i++)
49
if (c1 >= 'a' && c1 <= 'z')
58
if (c2 >= 'a' && c2 <= 'z')
67
if ((c1 - c2) || !c1 || !c2)
73
return (int) !(c1 - c2);
76
static char * /* return new position in input str */
78
char *istr, /* old position in input buf */
79
char *obuf) /* token string ( if "\0", then finished ) */
83
/* Skip leading white space */
84
while (*istr == ' ' || *istr == '\t')
87
for (; *istr && *istr != '\n'; istr++)
96
for (; *istr && *istr != '\n'; istr++);
102
if (nx == ';' || nx == '=' || c == '=')
110
/* Trim end of token */
111
for (; obuf > start && (*(obuf - 1) == ' ' || *(obuf - 1) == '\t');)
117
#if !defined(WINDOWS) && !defined(WIN32) && !defined(OS2)
123
_iodbcdm_getinifile (char *buf, int size)
128
j = STRLEN ("/odbc.ini") + 1;
135
#if !defined(UNIX_PWD)
137
i = GetWindowsDirectory ((LPSTR) buf, size);
139
if (i == 0 || i > size - j)
144
sprintf (buf + i, "/odbc.ini");
148
if ((ptr = getenv ("ODBCINI")) != NULL)
154
if ((ptr = getenv ("IODBCINI")) != NULL)
160
if ((ptr = getenv ("HOME")) == NULL)
162
ptr = (char *) getpwuid (getuid ());
169
ptr = ((struct passwd *) ptr)->pw_dir;
172
if (ptr == NULL || *ptr == '\0')
177
if (size < STRLEN (ptr) + j)
182
sprintf (buf, "%s%s", ptr, "/.odbc.ini");
183
/* i.e. searching ~/.odbc.ini */
191
* read odbc init file to resolve the value of specified
192
* key from named or defaulted dsn section
195
_iodbcdm_getkeyvalbydsn (
203
char dsntk[SQL_MAX_DSN_LENGTH + 3] = {'[', '\0'};
204
char token[1024]; /* large enough */
209
#define DSN_NOMATCH 0
211
#define DSN_DEFAULT 2
213
int dsnid = DSN_NOMATCH;
214
int defaultdsn = DSN_NOMATCH;
216
if (dsn == NULL || *dsn == 0)
219
dsnlen = STRLEN (dsn);
222
if (dsnlen == SQL_NTS)
224
dsnlen = STRLEN (dsn);
227
if (dsnlen <= 0 || keywd == NULL || buf == 0 || size <= 0)
232
if (dsnlen > sizeof (dsntk) - 2)
239
STRNCAT (dsntk, dsn, dsnlen);
244
path = _iodbcdm_getinifile (pathbuf, sizeof (pathbuf));
251
file = (FILE *) fopen (path, "r");
262
str = fgets (buf, sizeof (buf), file);
271
if (upper_strneq (str, "[default]", STRLEN ("[default]")))
273
/* we only read first dsn default dsn
274
* section (as well as named dsn).
276
if (defaultdsn == DSN_NOMATCH)
279
defaultdsn = DSN_DEFAULT;
288
else if (upper_strneq (str, dsntk, dsnlen))
299
else if (dsnid == DSN_NOMATCH)
304
str = readtoken (str, token);
306
if (upper_strneq (keywd, token, STRLEN (keywd)))
308
str = readtoken (str, token);
310
if (!STREQ (token, "="))
311
/* something other than = */
316
str = readtoken (str, token);
318
if (STRLEN (token) > size - 1)
323
STRNCPY (value, token, size);
324
/* copy the value(i.e. next token) to buf */
326
if (dsnid != DSN_DEFAULT)
335
return (*value) ? value : NULL;
340
_iodbcdm_getkeyvalinstr (
347
char token[1024] = {'\0'};
350
if (cnstr == NULL || value == NULL
351
|| keywd == NULL || size < 1)
356
if (cnlen == SQL_NTS)
358
cnlen = STRLEN (cnstr);
368
cnstr = readtoken (cnstr, token);
375
if (STREQ (token, ";"))
384
if (upper_strneq (token, keywd, strlen (keywd)))
391
if (STREQ (token, "="))
398
if (size < strlen (token) + 1)
403
STRNCPY (value, token, size);