]> git.saurik.com Git - wxWidgets.git/blob - src/iodbc/misc.c
wxGetUserName() returns the user name (wxGetUserId()) if all else fails
[wxWidgets.git] / src / iodbc / misc.c
1 /** miscellaneous functions
2
3 Copyright (C) 1995 by Ke Jin <kejin@empress.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 **/
15
16 #include <../iodbc/iodbc.h>
17
18 #include <../iodbc/isql.h>
19 #include <../iodbc/isqlext.h>
20
21 #include <stdio.h>
22 #include <strings.h>
23
24 static int
25 upper_strneq(
26 char* s1,
27 char* s2,
28 int n )
29 {
30 int i;
31 char c1 = 0, c2 = 0;
32
33 for(i=1;i<n;i++)
34 {
35 c1 = s1[i];
36 c2 = s2[i];
37
38 if( c1 >= 'a' && c1 <= 'z' )
39 {
40 c1 += ('A' - 'a');
41 }
42 else if( c1 == '\n' )
43 {
44 c1 = '\0';
45 }
46
47 if( c2 >= 'a' && c2 <= 'z' )
48 {
49 c2 += ('A' - 'a');
50 }
51 else if( c2 == '\n' )
52 {
53 c2 = '\0';
54 }
55
56 if( (c1 - c2) || !c1 || !c2 )
57 {
58 break;
59 }
60 }
61
62 return (int)!(c1 - c2);
63 }
64
65 static char* /* return new position in input str */
66 readtoken(
67 char* istr, /* old position in input buf */
68 char* obuf ) /* token string ( if "\0", then finished ) */
69 {
70 for(; *istr && *istr != '\n' ; istr ++ )
71 {
72 char c, nx;
73
74 c = *(istr);
75
76 if( c == ' ' || c == '\t' )
77 {
78 continue;
79 }
80
81 nx = *(istr + 1);
82
83 *obuf = c;
84 obuf ++;
85
86 if( c == ';' || c == '=' )
87 {
88 istr ++;
89 break;
90 }
91
92 if( nx == ' ' || nx == '\t' || nx == ';' || nx == '=' )
93 {
94 istr ++;
95 break;
96 }
97 }
98
99 *obuf = '\0';
100
101 return istr;
102 }
103
104 #if !defined(WINDOWS) && !defined(WIN32) && !defined(OS2)
105 # include <pwd.h>
106 # define UNIX_PWD
107 #endif
108
109 static char*
110 getinitfile(char* buf, int size)
111 {
112 int /* i, */ j;
113 char* ptr;
114
115 j = STRLEN("/odbc.ini") + 1;
116
117 if( size < j )
118 {
119 return NULL;
120 }
121
122 #if !defined(UNIX_PWD)
123
124 i = GetWindowsDirectory((LPSTR)buf, size );
125
126 if( i == 0 || i > size - j )
127 {
128 return NULL;
129 }
130
131 sprintf( buf + i, "/odbc.ini");
132
133 return buf;
134 #else
135 ptr = (char*)getpwuid(getuid());
136
137 if( ptr == NULL )
138 {
139 return NULL;
140 }
141
142 ptr = ((struct passwd*)ptr)->pw_dir;
143
144 if( ptr == NULL || *ptr == '\0' )
145 {
146 ptr = "/home";
147 }
148
149 if( size < STRLEN(ptr) + j )
150 {
151 return NULL;
152 }
153
154 sprintf( buf, "%s%s", ptr, "/.odbc.ini");
155 /* i.e. searching ~/.odbc.ini */
156 #endif
157
158 return buf;
159 }
160
161 char* _iodbcdm_getkeyvalbydsn(
162 char* dsn,
163 int dsnlen,
164 char* keywd,
165 char* value,
166 int size )
167 /* read odbc init file to resolve the value of specified
168 * key from named or defaulted dsn section
169 */
170 {
171 char buf[1024];
172 char dsntk[SQL_MAX_DSN_LENGTH + 3] = { '[', '\0' };
173 char token[1024]; /* large enough */
174 FILE* file;
175 char pathbuf[1024];
176 char* path;
177
178 #define DSN_NOMATCH 0
179 #define DSN_NAMED 1
180 #define DSN_DEFAULT 2
181
182 int dsnid = DSN_NOMATCH;
183 int defaultdsn = DSN_NOMATCH;
184
185 if( dsn == NULL || *dsn == 0 )
186 {
187 dsn = "default";
188 dsnlen = STRLEN(dsn);
189 }
190
191 if( dsnlen == SQL_NTS )
192 {
193 dsnlen = STRLEN(dsn);
194 }
195
196 if( dsnlen <= 0 || keywd == NULL || buf == 0 || size <= 0 )
197 {
198 return NULL;
199 }
200
201 if( dsnlen > sizeof(dsntk) - 2 )
202 {
203 return NULL;
204 }
205
206 value[0] = '\0';
207
208 STRNCAT( dsntk, dsn, dsnlen );
209 STRCAT( dsntk, "]" );
210
211 dsnlen = dsnlen + 2;
212
213 path = getinitfile(pathbuf, sizeof(pathbuf));
214
215 if( path == NULL )
216 {
217 return NULL;
218 }
219
220 file = (FILE*)fopen(path, "r");
221
222 if( file == NULL )
223 {
224 return NULL;
225 }
226
227 for(;;)
228 {
229 char* str;
230
231 str = fgets(buf, sizeof(buf), file);
232
233 if( str == NULL )
234 {
235 break;
236 }
237
238 if( *str == '[' )
239 {
240 if( upper_strneq(str, "[default]", STRLEN("[default]")) )
241 {
242 /* we only read first dsn default dsn
243 * section (as well as named dsn).
244 */
245 if( defaultdsn == DSN_NOMATCH )
246 {
247 dsnid = DSN_DEFAULT;
248 defaultdsn = DSN_DEFAULT;
249 }
250 else
251 {
252 dsnid = DSN_NOMATCH;
253 }
254
255 continue;
256 }
257 else if( upper_strneq( str, dsntk, dsnlen ) )
258 {
259 dsnid = DSN_NAMED;
260 }
261 else
262 {
263 dsnid = DSN_NOMATCH;
264 }
265
266 continue;
267 }
268 else if( dsnid == DSN_NOMATCH )
269 {
270 continue;
271 }
272
273 str = readtoken(str, token);
274
275 if( upper_strneq( keywd, token, STRLEN(keywd)) )
276 {
277 str = readtoken(str, token);
278
279 if( ! STREQ( token, "=") )
280 /* something other than = */
281 {
282 continue;
283 }
284
285 str = readtoken(str, token);
286
287 if( STRLEN(token) > size - 1)
288 {
289 break;
290 }
291
292 STRNCPY(value, token, size);
293 /* copy the value(i.e. next token) to buf */
294
295 if( dsnid != DSN_DEFAULT )
296 {
297 break;
298 }
299 }
300 }
301
302 fclose(file);
303
304 return (*value)? value:NULL;
305 }
306
307 char* _iodbcdm_getkeyvalinstr(
308 char* cnstr,
309 int cnlen,
310 char* keywd,
311 char* value,
312 int size )
313 {
314 char token[1024] = { '\0' };
315 int flag = 0;
316
317 if( cnstr == NULL || value == NULL
318 || keywd == NULL || size < 1 )
319 {
320 return NULL;
321 }
322
323 if( cnlen == SQL_NTS )
324 {
325 cnlen = STRLEN (cnstr);
326 }
327
328 if( cnlen <= 0 )
329 {
330 return NULL;
331 }
332
333 for(;;)
334 {
335 cnstr = readtoken(cnstr, token);
336
337 if( *token == '\0' )
338 {
339 break;
340 }
341
342 if( STREQ( token, ";" ) )
343 {
344 flag = 0;
345 continue;
346 }
347
348 switch(flag)
349 {
350 case 0:
351 if( upper_strneq(token, keywd, strlen(keywd)) )
352 {
353 flag = 1;
354 }
355 break;
356
357 case 1:
358 if( STREQ( token, "=" ) )
359 {
360 flag = 2;
361 }
362 break;
363
364 case 2:
365 if( size < strlen(token) + 1 )
366 {
367 return NULL;
368 }
369
370 STRNCPY( value, token, size );
371
372 return value;
373
374 default:
375 break;
376 }
377 }
378
379 return NULL;
380 }