~ubuntu-branches/ubuntu/hardy/psqlodbc/hardy

« back to all changes in this revision

Viewing changes to gpps.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-05-13 10:47:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040513104736-a530gmn0p3knep89
Tags: upstream-07.03.0200
ImportĀ upstreamĀ versionĀ 07.03.0200

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------
 
2
 * GetPrivateProfileString()
 
3
 *
 
4
 * approximate implementation of
 
5
 * Windows NT System Services version of GetPrivateProfileString()
 
6
 * probably doesn't handle the NULL key for section name or value key
 
7
 * correctly also, doesn't provide Microsoft backwards compatability
 
8
 * wrt TAB characters in the value string
 
9
 *
 
10
 * Microsoft terminates value
 
11
 * at the first TAB, but I couldn't discover what the behavior should
 
12
 * be regarding TABS in quoted strings so, I treat tabs like any other
 
13
 * characters
 
14
 *
 
15
 * NO comments following value string separated by a TAB
 
16
 * are allowed (that is an anachronism anyway)
 
17
 * Added code to search for ODBC_INI file in users home directory on
 
18
 * Unix
 
19
 *-------
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
#if !defined(WIN32) && !defined(WITH_UNIXODBC) && !defined(WITH_IODBC)
 
24
 
 
25
#include "gpps.h"
 
26
 
 
27
#include <stdio.h>
 
28
#include <unistd.h>
 
29
#include <ctype.h>
 
30
#include <pwd.h>
 
31
 
 
32
#include <sys/types.h>
 
33
#include <string.h>
 
34
#include "misc.h"
 
35
#include "dlg_specific.h"
 
36
 
 
37
#ifndef TRUE
 
38
#define TRUE    ((BOOL)1)
 
39
#endif
 
40
#ifndef FALSE
 
41
#define FALSE   ((BOOL)0)
 
42
#endif
 
43
 
 
44
#ifndef ODBCINSTDIR
 
45
#error "ODBCINSTDIR must be defined to compile this file"
 
46
#endif
 
47
 
 
48
 
 
49
/*
 
50
 * theIniFileName is searched for in:
 
51
 *      $HOME/theIniFileName
 
52
 *      theIniFileName
 
53
 *      ODBCINSTDIR/ODBCINST_INI
 
54
 */
 
55
DWORD
 
56
GetPrivateProfileString(const char *theSection, /* section name */
 
57
                                                const char *theKey,             /* search key name */
 
58
                                                const char *theDefault, /* default value if not
 
59
                                                                                                 * found */
 
60
                                                char *theReturnBuffer,  /* return value stored
 
61
                                                                                                 * here */
 
62
                                                size_t theReturnBufferLength,   /* byte length of return
 
63
                                                                                                                 * buffer */
 
64
                                                const char *theIniFileName)             /* pathname of ini file
 
65
                                                                                                                 * to search */
 
66
{
 
67
        char            buf[MAXPGPATH];
 
68
        char       *ptr = 0;
 
69
        FILE       *aFile = 0;
 
70
        size_t          aLength;
 
71
        char            aLine[2048];
 
72
        char       *aValue;
 
73
        char       *aStart;
 
74
        char       *aString;
 
75
        size_t          aLineLength;
 
76
        size_t          aReturnLength = 0;
 
77
        BOOL            aSectionFound = FALSE;
 
78
        BOOL            aKeyFound = FALSE;
 
79
 
 
80
        ptr = (char *) getpwuid(getuid());      /* get user info */
 
81
 
 
82
        if (ptr == NULL || (((struct passwd *) ptr)->pw_dir) == NULL || *(((struct passwd *) ptr)->pw_dir) == '\0')
 
83
                ptr = "/home";
 
84
        else
 
85
                ptr = ((struct passwd *) ptr)->pw_dir;  /* get user home dir */
 
86
 
 
87
        /*
 
88
         * If it can't be opened because the paths are too long, then skip it,
 
89
         * don't just truncate the path string...  The truncated path might
 
90
         * accidently be an existing file.      The default value will be returned
 
91
         * instead.
 
92
         */
 
93
        if (MAXPGPATH - 1 >= strlen(ptr) + 1 + strlen(theIniFileName))
 
94
        {
 
95
                sprintf(buf, "%s/%s", ptr, theIniFileName);
 
96
                aFile = (FILE *) fopen(buf, PG_BINARY_R);
 
97
        }
 
98
 
 
99
        /*
 
100
         * This code makes it so that a file in the users home dir overrides a
 
101
         * the "default" file as passed in
 
102
         */
 
103
        if (!aFile)
 
104
        {
 
105
                aFile = (FILE *) fopen(theIniFileName, PG_BINARY_R);
 
106
                if (!aFile)
 
107
                        aFile = (FILE *) fopen(ODBCINSTDIR "/" ODBCINST_INI, PG_BINARY_R);
 
108
        }
 
109
 
 
110
        aLength = (theDefault == NULL) ? 0 : strlen(theDefault);
 
111
 
 
112
        if (theReturnBufferLength == 0 || theReturnBuffer == NULL)
 
113
        {
 
114
                if (aFile)
 
115
                        fclose(aFile);
 
116
                return 0;
 
117
        }
 
118
 
 
119
        if (aFile == NULL)
 
120
        {
 
121
                /* no ini file specified, return the default */
 
122
                ++aLength;                              /* room for NULL char */
 
123
                aLength = theReturnBufferLength < aLength ?
 
124
                        theReturnBufferLength : aLength;
 
125
                strncpy(theReturnBuffer, theDefault, aLength);
 
126
                theReturnBuffer[aLength - 1] = '\0';
 
127
                return aLength - 1;
 
128
        }
 
129
 
 
130
        while (fgets(aLine, sizeof(aLine), aFile) != NULL)
 
131
        {
 
132
                aLineLength = strlen(aLine);
 
133
                /* strip final '\n' */
 
134
                if (aLineLength > 0 && aLine[aLineLength - 1] == '\n')
 
135
                        aLine[aLineLength - 1] = '\0';
 
136
                switch (*aLine)
 
137
                {
 
138
                        case ' ':                       /* blank line */
 
139
                        case ';':                       /* comment line */
 
140
                                continue;
 
141
                                break;
 
142
 
 
143
                        case '[':                       /* section marker */
 
144
                                if ((aString = strchr(aLine, ']')))
 
145
                                {
 
146
                                        aStart = aLine + 1;
 
147
                                        aString--;
 
148
                                        while (isspace((unsigned char) *aStart))
 
149
                                                aStart++;
 
150
                                        while (isspace((unsigned char) *aString))
 
151
                                                aString--;
 
152
                                        *(aString + 1) = '\0';
 
153
 
 
154
                                        /* accept as matched if NULL key or exact match */
 
155
                                        if (!theSection || !strcmp(aStart, theSection))
 
156
                                                aSectionFound = TRUE;
 
157
                                        else
 
158
                                                aSectionFound = FALSE;
 
159
                                }
 
160
                                break;
 
161
 
 
162
                        default:
 
163
 
 
164
                                /* try to match value keys if in proper section */
 
165
                                if (aSectionFound)
 
166
                                {
 
167
                                        /* try to match requested key */
 
168
                                        if ((aString = aValue = strchr(aLine, '=')))
 
169
                                        {
 
170
                                                *aValue = '\0';
 
171
                                                ++aValue;
 
172
 
 
173
                                                /* strip leading blanks in value field */
 
174
                                                while (*aValue == ' ' && aValue < aLine + sizeof(aLine))
 
175
                                                        *aValue++ = '\0';
 
176
                                                if (aValue >= aLine + sizeof(aLine))
 
177
                                                        aValue = "";
 
178
                                        }
 
179
                                        else
 
180
                                                aValue = "";
 
181
 
 
182
                                        aStart = aLine;
 
183
                                        while (isspace((unsigned char) *aStart))
 
184
                                                aStart++;
 
185
 
 
186
                                        /* strip trailing blanks from key */
 
187
                                        if (aString)
 
188
                                        {
 
189
                                                while (--aString >= aStart && *aString == ' ')
 
190
                                                        *aString = '\0';
 
191
                                        }
 
192
 
 
193
                                        /* see if key is matched */
 
194
                                        if (theKey == NULL || !strcmp(theKey, aStart))
 
195
                                        {
 
196
                                                /* matched -- first, terminate value part */
 
197
                                                aKeyFound = TRUE;
 
198
                                                aLength = strlen(aValue);
 
199
 
 
200
                                                /* remove trailing blanks from aValue if any */
 
201
                                                aString = aValue + aLength - 1;
 
202
 
 
203
                                                while (--aString > aValue && *aString == ' ')
 
204
                                                {
 
205
                                                        *aString = '\0';
 
206
                                                        --aLength;
 
207
                                                }
 
208
 
 
209
                                                /* unquote value if quoted */
 
210
                                                if (aLength >= 2 && aValue[0] == '"' &&
 
211
                                                        aValue[aLength - 1] == '"')
 
212
                                                {
 
213
                                                        /* string quoted with double quotes */
 
214
 
 
215
                                                        aValue[aLength - 1] = '\0';
 
216
                                                        ++aValue;
 
217
                                                        aLength -= 2;
 
218
                                                }
 
219
                                                else
 
220
                                                {
 
221
                                                        /* single quotes allowed also... */
 
222
                                                        if (aLength >= 2 && aValue[0] == '\'' &&
 
223
                                                                aValue[aLength - 1] == '\'')
 
224
                                                        {
 
225
                                                                aValue[aLength - 1] = '\0';
 
226
                                                                ++aValue;
 
227
                                                                aLength -= 2;
 
228
                                                        }
 
229
                                                }
 
230
 
 
231
                                                /* compute maximum length copyable */
 
232
                                                aLineLength = (aLength <
 
233
                                                theReturnBufferLength - aReturnLength) ? aLength :
 
234
                                                        theReturnBufferLength - aReturnLength;
 
235
 
 
236
                                                /* do the copy to return buffer */
 
237
                                                if (aLineLength)
 
238
                                                {
 
239
                                                        strncpy(&theReturnBuffer[aReturnLength],
 
240
                                                                        aValue, aLineLength);
 
241
                                                        aReturnLength += aLineLength;
 
242
                                                        if (aReturnLength < theReturnBufferLength)
 
243
                                                        {
 
244
                                                                theReturnBuffer[aReturnLength] = '\0';
 
245
                                                                ++aReturnLength;
 
246
                                                        }
 
247
                                                }
 
248
                                                if (aFile)
 
249
                                                {
 
250
                                                        fclose(aFile);
 
251
                                                        aFile = NULL;
 
252
                                                }
 
253
                                                return aReturnLength > 0 ? aReturnLength - 1 : 0;
 
254
                                        }
 
255
                                }
 
256
                                break;
 
257
                }
 
258
        }
 
259
 
 
260
        if (aFile)
 
261
                fclose(aFile);
 
262
 
 
263
        if (!aKeyFound)
 
264
        {
 
265
                /* key wasn't found return default */
 
266
                ++aLength;                              /* room for NULL char */
 
267
                aLength = theReturnBufferLength < aLength ?
 
268
                        theReturnBufferLength : aLength;
 
269
                strncpy(theReturnBuffer, theDefault, aLength);
 
270
                theReturnBuffer[aLength - 1] = '\0';
 
271
                aReturnLength = aLength - 1;
 
272
        }
 
273
        return aReturnLength > 0 ? aReturnLength - 1 : 0;
 
274
}
 
275
 
 
276
 
 
277
DWORD
 
278
WritePrivateProfileString(const char *theSection,               /* section name */
 
279
                                                  const char *theKey,   /* write key name */
 
280
                                                  const char *theBuffer,                /* input buffer */
 
281
                                                  const char *theIniFileName)   /* pathname of ini file
 
282
                                                                                                                 * to write */
 
283
{
 
284
        return 0;
 
285
}
 
286
 
 
287
 
 
288
#if NOT_USED
 
289
/*
 
290
 * Ok. What the hell's the default behaviour for a null input buffer, and null
 
291
 * section name. For now if either are null I ignore the request, until
 
292
 * I find out different.
 
293
 */
 
294
DWORD
 
295
WritePrivateProfileString(char *theSection,             /* section name */
 
296
                                                  char *theKey, /* write key name */
 
297
                                                  char *theBuffer,              /* input buffer */
 
298
                                                  char *theIniFileName) /* pathname of ini file to
 
299
                                                                                                 * write */
 
300
{
 
301
        char            buf[MAXPGPATH];
 
302
        char       *ptr = 0;
 
303
        FILE       *aFile = 0;
 
304
        size_t          aLength;
 
305
        char            aLine[2048];
 
306
        char       *aValue;
 
307
        char       *aString;
 
308
        size_t          aLineLength;
 
309
        size_t          aReturnLength = 0;
 
310
 
 
311
        BOOL            aSectionFound = FALSE;
 
312
        BOOL            keyFound = FALSE;
 
313
        int                     j = 0;
 
314
 
 
315
        /* If this isn't correct processing we'll change it later  */
 
316
        if (theSection == NULL || theKey == NULL || theBuffer == NULL ||
 
317
                theIniFileName == NULL)
 
318
                return 0;
 
319
 
 
320
        aLength = strlen(theBuffer);
 
321
        if (aLength == 0)
 
322
                return 0;
 
323
 
 
324
        j = strlen(theIniFileName) + 1;
 
325
        ptr = (char *) getpwuid(getuid());      /* get user info */
 
326
 
 
327
        if (ptr == NULL)
 
328
        {
 
329
                if (MAXPGPATH - 1 < j)
 
330
                        theIniFileName[MAXPGPATH - 1] = '\0';
 
331
 
 
332
                sprintf(buf, "%s", theIniFileName);
 
333
        }
 
334
        ptr = ((struct passwd *) ptr)->pw_dir;          /* get user home dir */
 
335
        if (ptr == NULL || *ptr == '\0')
 
336
                ptr = "/home";
 
337
 
 
338
        /*
 
339
         * This doesn't make it so we find an ini file but allows normal
 
340
         * processing to continue further on down. The likelihood is that the
 
341
         * file won't be found and thus the default value will be returned.
 
342
         */
 
343
        if (MAXPGPATH - 1 < strlen(ptr) + j)
 
344
        {
 
345
                if (MAXPGPATH - 1 < strlen(ptr))
 
346
                        ptr[MAXPGPATH - 1] = '\0';
 
347
                else
 
348
                        theIniFileName[MAXPGPATH - 1 - strlen(ptr)] = '\0';
 
349
        }
 
350
 
 
351
        sprintf(buf, "%s/%s", ptr, theIniFileName);
 
352
 
 
353
        /*
 
354
         * This code makes it so that a file in the users home dir overrides a
 
355
         * the "default" file as passed in
 
356
         */
 
357
        aFile = (FILE *) (buf ? fopen(buf, "r+") : NULL);
 
358
        if (!aFile)
 
359
        {
 
360
                sprintf(buf, "%s", theIniFileName);
 
361
                aFile = (FILE *) (buf ? fopen(buf, "r+") : NULL);
 
362
                if (!aFile)
 
363
                        return 0;
 
364
        }
 
365
 
 
366
        aLength = strlen(theBuffer);
 
367
 
 
368
        /*
 
369
         * We have to search for theKey, because if it already exists we have
 
370
         * to overwrite it. If it doesn't exist we just write a new line to
 
371
         * the file.
 
372
         */
 
373
        while (fgets(aLine, sizeof(aLine), aFile) != NULL)
 
374
        {
 
375
                aLineLength = strlen(aLine);
 
376
                /* strip final '\n' */
 
377
                if (aLineLength > 0 && aLine[aLineLength - 1] == '\n')
 
378
                        aLine[aLineLength - 1] = '\0';
 
379
                switch (*aLine)
 
380
                {
 
381
                        case ' ':                       /* blank line */
 
382
                        case ';':                       /* comment line */
 
383
                                continue;
 
384
                                break;
 
385
 
 
386
                        case '[':                       /* section marker */
 
387
                                if ((aString = strchr(aLine, ']')))
 
388
                                {
 
389
                                        *aString = '\0';
 
390
 
 
391
                                        /* accept as matched if key exact match */
 
392
 
 
393
                                        if (!strcmp(aLine + 1, theSection))
 
394
                                                aSectionFound = TRUE;
 
395
                                }
 
396
                                break;
 
397
 
 
398
                        default:
 
399
                                /* try to match value keys if in proper section */
 
400
                                if (aSectionFound)
 
401
                                {
 
402
                                        /* try to match requested key */
 
403
 
 
404
                                        if ((aString = aValue = strchr(aLine, '=')))
 
405
                                        {
 
406
                                                *aValue = '\0';
 
407
                                                ++aValue;
 
408
 
 
409
                                                /* strip leading blanks in value field */
 
410
                                                while (*aValue == ' ' && aValue < aLine + sizeof(aLine))
 
411
                                                        *aValue++ = '\0';
 
412
                                                if (aValue >= aLine + sizeof(aLine))
 
413
                                                        aValue = "";
 
414
                                        }
 
415
                                        else
 
416
                                                aValue = "";
 
417
 
 
418
                                        /* strip trailing blanks from key */
 
419
                                        if (aString)
 
420
                                        {
 
421
                                                while (--aString >= aLine && *aString == ' ')
 
422
                                                        *aString = '\0';
 
423
                                        }
 
424
 
 
425
                                        /* see if key is matched */
 
426
                                        if (!strcmp(theKey, aLine))
 
427
                                        {
 
428
                                                keyFound = TRUE;
 
429
                                                /* matched -- first, terminate value part */
 
430
 
 
431
                                                /* overwrite current value */
 
432
                                                fseek(aFile, -aLineLength, SEEK_CUR);
 
433
                                                /* overwrite key and value */
 
434
                                                sprintf(aLine, "%s = %s\n", theKey, theBuffer);
 
435
                                                fputs(aLine, aFile);
 
436
                                        }
 
437
                                }
 
438
                }
 
439
                break;
 
440
        }
 
441
}
 
442
 
 
443
if (!keyFound)
 
444
{                                                               /* theKey wasn't in file so  */
 
445
        if (aFile)
 
446
                fclose(aFile);
 
447
 
 
448
        return aReturnLength > 0 ? aReturnLength - 1 : 0;
 
449
}
 
450
#endif   /* NOT_USED */
 
451
 
 
452
#endif   /* not WIN32 */