1/*
2 *  SQLGetInstalledDrivers.c
3 *
4 *  $Id: SQLGetInstalledDrivers.c,v 1.10 2006/01/20 15:58:35 source Exp $
5 *
6 *  Get a list of installed drivers
7 *
8 *  The iODBC driver manager.
9 *
10 *  Copyright (C) 1996-2006 by OpenLink Software <iodbc@openlinksw.com>
11 *  All Rights Reserved.
12 *
13 *  This software is released under the terms of either of the following
14 *  licenses:
15 *
16 *      - GNU Library General Public License (see LICENSE.LGPL)
17 *      - The BSD License (see LICENSE.BSD).
18 *
19 *  Note that the only valid version of the LGPL license as far as this
20 *  project is concerned is the original GNU Library General Public License
21 *  Version 2, dated June 1991.
22 *
23 *  While not mandated by the BSD license, any patches you make to the
24 *  iODBC source code may be contributed back into the iODBC project
25 *  at your discretion. Contributions will benefit the Open Source and
26 *  Data Access community as a whole. Submissions may be made at:
27 *
28 *      http://www.iodbc.org
29 *
30 *
31 *  GNU Library Generic Public License Version 2
32 *  ============================================
33 *  This library is free software; you can redistribute it and/or
34 *  modify it under the terms of the GNU Library General Public
35 *  License as published by the Free Software Foundation; only
36 *  Version 2 of the License dated June 1991.
37 *
38 *  This library is distributed in the hope that it will be useful,
39 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41 *  Library General Public License for more details.
42 *
43 *  You should have received a copy of the GNU Library General Public
44 *  License along with this library; if not, write to the Free
45 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
46 *
47 *
48 *  The BSD License
49 *  ===============
50 *  Redistribution and use in source and binary forms, with or without
51 *  modification, are permitted provided that the following conditions
52 *  are met:
53 *
54 *  1. Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 *  2. Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in
58 *     the documentation and/or other materials provided with the
59 *     distribution.
60 *  3. Neither the name of OpenLink Software Inc. nor the names of its
61 *     contributors may be used to endorse or promote products derived
62 *     from this software without specific prior written permission.
63 *
64 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
68 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 */
76
77
78#include <iodbc.h>
79#include <odbcinst.h>
80#include <unicode.h>
81
82#include "misc.h"
83#include "iodbc_error.h"
84
85#ifdef WIN32
86#define SECT1			"ODBC 32 bit Data Sources"
87#define SECT2			"ODBC 32 bit Drivers"
88#else
89#define SECT1			"ODBC Data Sources"
90#define SECT2			"ODBC Drivers"
91#endif
92
93#define MAX_ENTRIES		1024
94
95extern BOOL GetAvailableDrivers (LPCSTR lpszInfFile, LPSTR lpszBuf,
96    WORD cbBufMax, WORD * pcbBufOut, BOOL infFile);
97
98static int
99SectSorter (const void *p1, const void *p2)
100{
101  const char **s1 = (const char **) p1;
102  const char **s2 = (const char **) p2;
103
104  return strcasecmp (*s1, *s2);
105}
106
107BOOL INSTAPI
108SQLGetInstalledDrivers_Internal (LPSTR lpszBuf, WORD cbBufMax,
109    WORD * pcbBufOut, SQLCHAR waMode)
110{
111  char buffer[4096], desc[1024], *ptr, *oldBuf = lpszBuf;
112  int i, j, usernum = 0, num_entries = 0;
113  void **sect = NULL;
114  SQLUSMALLINT fDir = SQL_FETCH_FIRST_USER;
115
116  if (pcbBufOut)
117    *pcbBufOut = 0;
118
119  /*
120   *  Allocate the buffer for the list
121   */
122  if ((sect = (void **) calloc (MAX_ENTRIES, sizeof (void *))) == NULL)
123    {
124      PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
125      return SQL_FALSE;
126    }
127
128  do
129    {
130      SQLSetConfigMode (fDir ==
131	  SQL_FETCH_FIRST_SYSTEM ? ODBC_SYSTEM_DSN : ODBC_USER_DSN);
132      SQLGetPrivateProfileString (SECT2, NULL, "", buffer,
133	  sizeof (buffer) / sizeof (SQLTCHAR), "odbcinst.ini");
134
135      /* For each drivers */
136      for (ptr = buffer, i = 1; *ptr && i; ptr += STRLEN (ptr) + 1)
137	{
138	  /* Add this section to the datasources list */
139	  if (fDir == SQL_FETCH_FIRST_SYSTEM)
140	    {
141	      for (j = 0; j < usernum; j++)
142		{
143		  if (STREQ (sect[j], ptr))
144		    j = usernum;
145		}
146	      if (j == usernum + 1)
147		continue;
148	    }
149
150	  if (num_entries >= MAX_ENTRIES)
151	    {
152	      i = 0;
153	      break;
154	    }			/* Skip the rest */
155
156	  /* ... and its description */
157	  SQLSetConfigMode (fDir ==
158	      SQL_FETCH_FIRST_SYSTEM ? ODBC_SYSTEM_DSN : ODBC_USER_DSN);
159	  SQLGetPrivateProfileString (SECT2, ptr, "", desc,
160	      sizeof (desc) / sizeof (SQLTCHAR), "odbcinst.ini");
161
162	  /* Check if the driver is installed */
163	  if (!STRCASEEQ (desc, "Installed"))
164	    continue;
165
166	  /* Copy the driver name */
167	  sect[num_entries++] = STRDUP (ptr);
168	}
169
170      switch (fDir)
171	{
172	case SQL_FETCH_FIRST_USER:
173	  fDir = SQL_FETCH_FIRST_SYSTEM;
174	  usernum = num_entries;
175	  break;
176	case SQL_FETCH_FIRST_SYSTEM:
177	  fDir = SQL_FETCH_FIRST;
178	  break;
179	}
180    }
181  while (fDir != SQL_FETCH_FIRST);
182
183  /*
184   *  Sort all entries so we can present a nice list
185   */
186  if (num_entries > 1)
187    {
188      qsort (sect, num_entries, sizeof (char **), SectSorter);
189
190      /* Copy back the result */
191      for (i = 0; cbBufMax > 0 && i < num_entries; i++)
192	{
193	  if (waMode == 'A')
194	    {
195	      STRNCPY (lpszBuf, sect[i], cbBufMax);
196	      cbBufMax -= (STRLEN (sect[i]) + 1);
197	      lpszBuf += (STRLEN (sect[i]) + 1);
198	    }
199	  else
200	    {
201	      dm_StrCopyOut2_A2W (sect[i], (LPWSTR) lpszBuf, cbBufMax, NULL);
202	      cbBufMax -= (STRLEN (sect[i]) + 1);
203	      lpszBuf += (STRLEN (sect[i]) + 1) * sizeof (wchar_t);
204	    }
205	}
206
207      if (waMode == 'A')
208	*lpszBuf = '\0';
209      else
210	*((wchar_t *) lpszBuf) = L'\0';
211    }
212
213  /*
214   *  Free old section list
215   */
216  if (sect)
217    {
218      for (i = 0; i < MAX_ENTRIES; i++)
219	if (sect[i])
220	  free (sect[i]);
221      free (sect);
222    }
223
224  if (pcbBufOut)
225    *pcbBufOut =
226	lpszBuf - oldBuf + (waMode == 'A' ? sizeof (char) : sizeof (wchar_t));
227
228  return waMode == 'A' ? (oldBuf[0] ? SQL_TRUE : SQL_FALSE) :
229      (((wchar_t *) oldBuf)[0] ? SQL_TRUE : SQL_FALSE);
230}
231
232BOOL INSTAPI
233SQLGetInstalledDrivers (LPSTR lpszBuf, WORD cbBufMax, WORD * pcbBufOut)
234{
235  return SQLGetInstalledDrivers_Internal (lpszBuf, cbBufMax, pcbBufOut, 'A');
236}
237
238BOOL INSTAPI
239SQLGetInstalledDriversW (LPWSTR lpszBuf, WORD cbBufMax, WORD FAR * pcbBufOut)
240{
241  return SQLGetInstalledDrivers_Internal ((LPSTR) lpszBuf, cbBufMax,
242      pcbBufOut, 'W');
243}
244