File: misc.c

package info (click to toggle)
libiodbc2 3.0.5-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,172 kB
  • ctags: 2,749
  • sloc: ansic: 23,877; sh: 6,614; makefile: 286
file content (557 lines) | stat: -rw-r--r-- 10,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 *  misc.c
 *
 *  $Id: misc.c,v 1.13 2001/06/06 12:33:35 source Exp $
 *
 *  Miscellaneous functions
 *
 *  The iODBC driver manager.
 *  
 *  Copyright (C) 1995 by Ke Jin <kejin@empress.com> 
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <iodbc.h>

#include <sql.h>
#include <sqlext.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef _MAC
#include <getfpn.h>
#endif /* _MAC */


static int
upper_strneq (
    char *s1,
    char *s2,
    int n)
{
  int i;
  char c1, c2;

  for (i = 1; i < n; i++)
    {
      c1 = s1[i];
      c2 = s2[i];

      if (c1 >= 'a' && c1 <= 'z')
	{
	  c1 += ('A' - 'a');
	}
      else if (c1 == '\n')
	{
	  c1 = '\0';
	}

      if (c2 >= 'a' && c2 <= 'z')
	{
	  c2 += ('A' - 'a');
	}
      else if (c2 == '\n')
	{
	  c2 = '\0';
	}

      if ((c1 - c2) || !c1 || !c2)
	{
	  break;
	}
    }

  return (int) !(c1 - c2);
}


static char *			/* return new position in input str */
readtoken (
    char *istr,			/* old position in input buf */
    char *obuf)			/* token string ( if "\0", then finished ) */
{
  char *start = obuf;

  /* Skip leading white space */
  while (*istr == ' ' || *istr == '\t')
    istr++;

  for (; *istr && *istr != '\n'; istr++)
    {
      char c, nx;

      c = *(istr);
      nx = *(istr + 1);

      if (c == ';')
	{
	  for (; *istr && *istr != '\n'; istr++);
	  break;
	}
      *obuf = c;
      obuf++;

      if (nx == ';' || nx == '=' || c == '=')
	{
	  istr++;
	  break;
	}
    }
  *obuf = '\0';

  /* Trim end of token */
  for (; obuf > start && (*(obuf - 1) == ' ' || *(obuf - 1) == '\t');)
    *--obuf = '\0';

  return istr;
}


#if !defined(WINDOWS) && !defined(WIN32) && !defined(OS2) && !defined(_MAC)
#include <pwd.h>
#define UNIX_PWD
#endif

/*
 * Algorithm for resolving an odbc.ini reference
 * 
 * For UNIX :    1. Check for $ODBCINI variable, if exists return $ODBCINI.
 *               2. Check for $HOME/.odbc.ini or ~/.odbc.ini file, if exists 
 *                  return it.
 *               3. Check for SYS_ODBC_INI build variable, if exists return 
 *                  it. (ie : /etc/odbc.ini).
 *               4. No odbc.ini presence, return NULL.
 *
 * For WINDOWS, WIN32, OS2 :
 *               1. Check for the system odbc.ini file, if exists return it.
 *               2. No odbc.ini presence, return NULL.
 *
 * For VMS:      1. Check for $ODBCINI variable, if exists return $ODBCINI.
 *               2. Check for SYS$LOGIN:ODBC.INI file, if exists return it.
 *               3. No odbc.ini presence, return NULL.
 *
 * For Mac:      1. On powerPC, file is ODBC Preferences PPC
 *                  On 68k, file is ODBC Preferences
 *               2. Check for ...:System Folder:Preferences:ODBC Preferences 
 *                  file, if exists return it.
 *               3. No odbc.ini presence, return NULL.
 *
 * For MacX:     1. Check for $ODBCINI variable, if exists return $ODBCINI.
 *               2. Check for $HOME/.odbc.ini or ~/.odbc.ini file, if exists 
 *                  return it.
 *               3. Check for $HOME/Library/Preferences/ODBC.preference or 
 *                  ~/.odbc.ini file, if exists return it.
 *               4. Check for SYS_ODBC_INI build variable, if exists return 
 *                  it. (ie : /etc/odbc.ini).
 *               5. Check for /System/Library/Preferences/ODBC.preference 
 *                  file, if exists return it.
 *               6. No odbc.ini presence, return NULL.
 */
char *
_iodbcdm_getinifile (char *buf, int size)
{
#ifdef _MAC
  OSErr result;
  long fldrDid;
  short fldrRef;
#endif /* _MAC */
  int i, j;
  char *ptr;

#ifdef _MAC
#  ifdef __POWERPC__
  j = STRLEN (":ODBC Preferences PPC") + 1;
#  else
  j = STRLEN (":ODBC Preferences") + 1;
#  endif /* __POWERPC__ */
#else
  j = STRLEN ("/odbc.ini") + 1;
#endif /* _MAC */

  if (size < j)
    return NULL;

#if !defined(UNIX_PWD)
#  ifdef _MAC
  result =
      FindFolder (kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
      &fldrRef, &fldrDid);
  if (result != noErr)
    return NULL;
  ptr = get_full_pathname (fldrDid, fldrRef);

  i = (ptr) ? STRLEN (ptr) : 0;
  if (i == 0 || i > size - j)
    {
      if (ptr)
	free (ptr);
      return NULL;
    }

#    ifdef __POWERPC__
  STRCPY (buf, ptr);
  STRCAT (buf, ":ODBC Preferences PPC");
#    else
  STRCPY (buf, ptr);
  STRCAT (buf, ":ODBC Preferences");
#    endif /* __POWERPC__ */
  free (ptr);

  return buf;

#  else	/* else _MAC */

  /*
   *  On Windows, there is only one place to look
   */
  i = GetWindowsDirectory ((LPSTR) buf, size);

  if (i == 0 || i > size - j)
    return NULL;

  snprintf (buf + i, size - i, "/odbc.ini");

  return buf;
#  endif /* _MAC */
#else
  /*
   *  1. Check $ODBCINI environment variable
   */
  if ((ptr = getenv ("ODBCINI")) != NULL)
    {
      STRNCPY (buf, ptr, size);

      if (access (buf, R_OK) == 0)
	return buf;
    }

#ifdef VMS
  /*
   *  2a. VMS calls this HOME
   */
  STRNCPY (buf, "SYS$LOGIN:ODBC.INI", size);

  if (access (buf, R_OK) == 0)
    return buf;
#  else	/* else VMS */
  /*
   *  2b. Check either $HOME/.odbc.ini or ~/.odbc.ini
   */
  if ((ptr = getenv ("HOME")) == NULL)
    {
      ptr = (char *) getpwuid (getuid ());

      if (ptr != NULL)
	ptr = ((struct passwd *) ptr)->pw_dir;
    }

  if (ptr != NULL)
    {
      snprintf (buf, size, "%s/.odbc.ini", ptr);

      if (access (buf, R_OK) == 0)
	return buf;

#   ifdef _MACX
      /*
       * Try to check the ~/Library/Preferences/ODBC.preference
       */
      snprintf (buf, size, "%s" ODBC_INI_APP, ptr);

      if (access (buf, R_OK) == 0)
	return buf;
#   endif /* _MACX */
    }

#  endif /* VMS */

  /*
   *  3. Try SYS_ODBC_INI as the last resort
   */
  if ((ptr = getenv ("SYSODBCINI")) != NULL)
    {
      STRNCPY (buf, ptr, size);

      if (access (buf, R_OK) == 0)
	return buf;
    }

  STRNCPY (buf, SYS_ODBC_INI, size);

  if (access (buf, R_OK) == 0)
    return buf;

# ifdef _MACX
  /*
   * Try to check the /System/Library/Preferences/ODBC.preference
   */
  snprintf (buf, size, "/System%s", ODBC_INI_APP);

  if (access (buf, R_OK) == 0)
    return buf;
# endif	/* _MACX */

  /*
   *  No ini file found or accessable
   */
  return NULL;
#endif /* UNIX_PWD */
}


/* 
 *  read odbc init file to resolve the value of specified
 *  key from named or defaulted dsn section 
 */
char *
_iodbcdm_getkeyvalbydsn (
    char *dsn,
    int dsnlen,
    char *keywd,
    char *value,
    int size)
{
  char buf[1024];
  char dsntk[SQL_MAX_DSN_LENGTH + 3] = {'[', '\0'};
  char token[1024];		/* large enough */
  FILE *file;
  char pathbuf[1024];
  char *path;
  int nKeyWordLength = 0, nTokenLength = 0;

#define DSN_NOMATCH	0
#define DSN_NAMED	1
#define DSN_DEFAULT	2

  int dsnid = DSN_NOMATCH;
  int defaultdsn = DSN_NOMATCH;

  if (dsn == NULL || *dsn == 0)
    {
      dsn = "default";
      dsnlen = STRLEN (dsn);
    }

  if (dsnlen == SQL_NTS)
    {
      dsnlen = STRLEN (dsn);
    }

  if (dsnlen <= 0 || keywd == NULL || buf == 0 || size <= 0)
    {
      return NULL;
    }

  if (dsnlen > sizeof (dsntk) - 2)
    {
      return NULL;
    }

  value[0] = '\0';
  nKeyWordLength = STRLEN (keywd);

  STRNCAT (dsntk, dsn, dsnlen);
  STRCAT (dsntk, "]");

  dsnlen = dsnlen + 2;

  path = _iodbcdm_getinifile (pathbuf, sizeof (pathbuf));

  if (path == NULL)
    {
      return NULL;
    }

  file = (FILE *) fopen (path, "r");

  if (file == NULL)
    {
      return NULL;
    }

  for (;;)
    {
      char *str;

      str = fgets (buf, sizeof (buf), file);

      if (str == NULL)
	  break;

      if (*str == '[')
	{
	  if (upper_strneq (str, "[default]", STRLEN ("[default]")))
	    {
	      /* we only read first dsn default dsn
	       * section (as well as named dsn).
	       */
	      if (defaultdsn == DSN_NOMATCH)
		{
		  dsnid = DSN_DEFAULT;
		  defaultdsn = DSN_DEFAULT;
		}
	      else
		  dsnid = DSN_NOMATCH;

	      continue;
	    }
	  else if (upper_strneq (str, dsntk, dsnlen))
	    {
	      dsnid = DSN_NAMED;
	    }
	  else
	    {
	      dsnid = DSN_NOMATCH;
	    }

	  continue;
	}
      else if (dsnid == DSN_NOMATCH)
	{
	  continue;
	}

      str = readtoken (str, token);

      if (token)
	nTokenLength = STRLEN (token);
      else
	nTokenLength = 0;

      if (upper_strneq (keywd, token, nTokenLength > nKeyWordLength ? nTokenLength : nKeyWordLength))
	{
	  str = readtoken (str, token);

	  if (!STREQ (token, "="))
	    /* something other than = */
	    {
	      continue;
	    }

	  str = readtoken (str, token);

	  if (STRLEN (token) > size - 1)
	    {
	      break;
	    }

	  STRNCPY (value, token, size);
	  /* copy the value(i.e. next token) to buf */

	  if (dsnid != DSN_DEFAULT)
	    {
	      break;
	    }
	}
    }

  fclose (file);

  return (*value) ? value : NULL;
}


char *
_iodbcdm_getkeyvalinstr (
    char *cnstr,
    int cnlen,
    char *keywd,
    char *value,
    int size)
{
  char token[1024] = {'\0'};
  int flag = 0;

  if (cnstr == NULL || value == NULL
      || keywd == NULL || size < 1)
    {
      return NULL;
    }

  if (cnlen == SQL_NTS)
    {
      cnlen = STRLEN (cnstr);
    }

  if (cnlen <= 0)
    {
      return NULL;
    }

  for (;;)
    {
      cnstr = readtoken (cnstr, token);

      if (*token == '\0')
	  break;

      if (STREQ (token, ";"))
	{
	  flag = 0;
	  continue;
	}

      switch (flag)
	{
	case 0:
	  if (upper_strneq (token, keywd, STRLEN (keywd)))
	      flag = 1;
	  break;

	case 1:
	  if (STREQ (token, "="))
	      flag = 2;
	  break;

	case 2:
	  if (size < STRLEN (token) + 1)
	      return NULL;
	  STRNCPY (value, token, size);
	  return value;

	default:
	  break;
	}
    }

  return NULL;
}


#if 0
int
SQLGetPrivateProfileString (
    char *lpszSection,
    char *lpszEntry,
    char *lpszDefault,
    char *RetBuffer,
    int cbRetBuffer,
    char *lpzFilename)
{
  char *value;

  value = _iodbcdm_getkeyvalbydsn (
      lpszSection, SQL_NTS,
      lpszEntry, RetBuffer, cbRetBuffer);

  if (value == NULL)
    strncpy (RetBuffer, lpszDefault, cbRetBuffer);

  return strlen (RetBuffer);
}
#endif