- char buf[1024];
- char dsntk[SQL_MAX_DSN_LENGTH + 3] = { '[', '\0' };
- char token[1024]; /* large enough */
- FILE* file;
- char pathbuf[1024];
- char* path;
-
-#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';
-
- STRNCAT( dsntk, dsn, dsnlen );
- STRCAT( dsntk, "]" );
-
- dsnlen = dsnlen + 2;
-
- path = getinitfile(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;
- }
-
- strtok( str, "\n\r" );
-
- 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( upper_strneq( keywd, token, STRLEN(keywd)) )
- {
- 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 buf[1024];
+ char dsntk[SQL_MAX_DSN_LENGTH + 3] = {'[', '\0'};
+ char token[1024]; /* large enough */
+ FILE *file;
+ char pathbuf[1024];
+ char *path;
+
+#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';
+
+ 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 (upper_strneq (keywd, token, STRLEN (keywd)))
+ {
+ 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;