1/*
2 *  SQLRemoveDriver.c
3 *
4 *  $Id: SQLRemoveDriver.c,v 1.8 2006/01/20 15:58:35 source Exp $
5 *
6 *  Remove a driver
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 "inifile.h"
83#include "misc.h"
84#include "iodbc_error.h"
85
86
87BOOL INSTAPI
88SQLRemoveDriver (LPCSTR lpszDriver, BOOL fRemoveDSN, LPDWORD lpdwUsageCount)
89{
90  /*char *szDriverFile = NULL; */
91  BOOL retcode = FALSE;
92  PCONFIG pCfg = NULL, pInstCfg = NULL;
93  LPSTR entries = (LPSTR) malloc (sizeof (char) * 65535), curr;
94  int len = 0, i = 0;
95
96  /* Check input parameters */
97  CLEAR_ERROR ();
98  if (!lpszDriver || !STRLEN (lpszDriver))
99    {
100      PUSH_ERROR (ODBC_ERROR_INVALID_NAME);
101      goto quit;
102    }
103
104  /* Else go through user/system odbcinst.ini */
105  switch (configMode)
106    {
107    case ODBC_BOTH_DSN:
108    case ODBC_USER_DSN:
109      wSystemDSN = USERDSN_ONLY;
110      break;
111
112    case ODBC_SYSTEM_DSN:
113      wSystemDSN = SYSTEMDSN_ONLY;
114      break;
115    }
116
117  if (_iodbcdm_cfg_search_init (&pCfg, "odbc.ini", FALSE))
118    {
119      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
120      goto done;
121    }
122
123  if (_iodbcdm_cfg_search_init (&pInstCfg, "odbcinst.ini", FALSE))
124    {
125      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
126      goto done;
127    }
128
129  if (fRemoveDSN)
130    {
131#ifdef WIN32
132      if (entries &&
133	  (len = _iodbcdm_list_entries (pCfg, "ODBC 32 bit Data Sources",
134		  entries, 65535)))
135#else
136      if (entries
137	  && (len =
138	      _iodbcdm_list_entries (pCfg, "ODBC Data Sources", entries,
139		  65535)))
140#endif
141	{
142	  for (curr = entries; i < len;
143	      i += (STRLEN (curr) + 1), curr += (STRLEN (curr) + 1))
144	    {
145	      int nCursor = pCfg->cursor;
146
147	      if (_iodbcdm_cfg_rewind (pCfg))
148		{
149		  PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
150		  goto done;
151		}
152
153#ifdef WIN32
154	      if (_iodbcdm_cfg_find (pCfg, "ODBC 32 bit Data Sources", curr))
155#else
156	      if (_iodbcdm_cfg_find (pCfg, "ODBC Data Sources", curr))
157#endif
158		{
159		  if (_iodbcdm_cfg_rewind (pCfg))
160		    {
161		      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
162		      goto done;
163		    }
164		  pCfg->cursor = nCursor;
165		  continue;
166		}
167
168	      if (!strcmp (pCfg->value, lpszDriver))
169		{
170		  if (_iodbcdm_cfg_write (pCfg, curr, NULL, NULL))
171		    {
172		      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
173		      goto done;
174		    }
175
176#ifdef WIN32
177		  if (_iodbcdm_cfg_write (pCfg, "ODBC 32 bit Data Sources",
178			  curr, NULL))
179#else
180		  if (_iodbcdm_cfg_write (pCfg, "ODBC Data Sources", curr,
181			  NULL))
182#endif
183		    {
184		      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
185		      goto done;
186		    }
187		}
188
189	      pCfg->cursor = nCursor;
190	    }
191	}
192    }
193
194  if (_iodbcdm_cfg_write (pInstCfg, (char *) lpszDriver, NULL, NULL))
195    {
196      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
197      goto done;
198    }
199
200#ifdef WIN32
201  if (_iodbcdm_cfg_write (pInstCfg, "ODBC 32 bit Drivers", lpszDriver, NULL))
202#else
203  if (_iodbcdm_cfg_write (pInstCfg, "ODBC Drivers", (LPSTR) lpszDriver, NULL))
204#endif
205    {
206      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
207      goto done;
208    }
209
210  if (_iodbcdm_cfg_commit (pCfg) || _iodbcdm_cfg_commit (pInstCfg))
211    {
212      PUSH_ERROR (ODBC_ERROR_GENERAL_ERR);
213      goto done;
214    }
215
216  retcode = TRUE;
217
218done:
219  if (pCfg)
220    _iodbcdm_cfg_done (pCfg);
221  if (pInstCfg)
222    _iodbcdm_cfg_done (pInstCfg);
223  if (entries)
224    free (entries);
225
226quit:
227  wSystemDSN = USERDSN_ONLY;
228  configMode = ODBC_BOTH_DSN;
229
230  return retcode;
231}
232
233BOOL INSTAPI
234SQLRemoveDriverW (LPCWSTR lpszDriver, BOOL fRemoveDSN, LPDWORD lpdwUsageCount)
235{
236  char *_driver_u8 = NULL;
237  BOOL retcode = FALSE;
238
239  _driver_u8 = (char *) dm_SQL_WtoU8 ((SQLWCHAR *) lpszDriver, SQL_NTS);
240  if (_driver_u8 == NULL && lpszDriver)
241    {
242      PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
243      goto done;
244    }
245
246  retcode = SQLRemoveDriver (_driver_u8, fRemoveDSN, lpdwUsageCount);
247
248done:
249  MEM_FREE (_driver_u8);
250
251  return retcode;
252}
253