-
Notifications
You must be signed in to change notification settings - Fork 36
/
adddsn.c
138 lines (130 loc) · 2.81 KB
/
adddsn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @file adddsn.c
* DSN creation utility for Win32.
*
* $Id: adddsn.c,v 1.8 2013/01/11 12:19:55 chw Exp chw $
*
* Copyright (c) 2003-2013 Christian Werner <[email protected]>
*
* See the file "license.terms" for information on usage
* and redistribution of this file and for a
* DISCLAIMER OF ALL WARRANTIES.
*/
#ifndef _WIN32
#error "only WIN32 supported"
#endif
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>
#include <winver.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
/**
* Handler for ODBC installation error messages.
* @param name name of API function for which to show error messages
*/
static BOOL
ProcessErrorMessages(char *name)
{
WORD err = 1;
DWORD code;
char errmsg[301];
WORD errlen, errmax = sizeof (errmsg) - 1;
int rc;
BOOL ret = FALSE;
do {
errmsg[0] = '\0';
rc = SQLInstallerError(err, &code, errmsg, errmax, &errlen);
if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
MessageBox(NULL, errmsg, name,
MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
ret = TRUE;
}
err++;
} while (rc != SQL_NO_DATA);
return ret;
}
/**
* Main function of DSN utility.
* This is the Win32 GUI main entry point.
* It (un)installs a DSN.
*
* Example usage:
*
* add[sys]dsn "SQLite ODBC Driver" DSN=foobar;Database=C:/FOOBAR
* rem[sys]dsn "SQLite ODBC Driver" DSN=foobar
*/
int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
char tmp[1024], *p, *drv, *cfg, *msg;
int i, op;
GetModuleFileName(NULL, tmp, sizeof (tmp));
p = tmp;
while (*p) {
*p = tolower(*p);
++p;
}
p = strrchr(tmp, '\\');
if (p == NULL) {
p = tmp;
}
op = ODBC_ADD_DSN;
msg = "Adding DSN";
if (strstr(p, "rem") != NULL) {
msg = "Removing DSN";
op = ODBC_REMOVE_DSN;
}
if (strstr(p, "sys") != NULL) {
if (op == ODBC_REMOVE_DSN) {
op = ODBC_REMOVE_SYS_DSN;
} else {
op = ODBC_ADD_SYS_DSN;
}
}
strncpy(tmp, lpszCmdLine, sizeof (tmp));
/* get driver argument */
i = strspn(tmp, "\"");
drv = tmp + i;
if (i > 0) {
i = strcspn(drv, "\"");
drv[i] = '\0';
cfg = drv + i + 1;
} else {
i = strcspn(drv, " \t");
if (i > 0) {
drv[i] = '\0';
cfg = drv + i + 1;
} else {
cfg = "\0\0";
}
}
if (strlen(drv) == 0) {
MessageBox(NULL, "No driver name given", msg,
MB_ICONERROR|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
exit(1);
}
i = strspn(cfg, " \t;");
cfg += i;
i = strlen(cfg);
cfg[i + 1] = '\0';
if (i > 0) {
p = cfg;
do {
p = strchr(p, ';');
if (p != NULL) {
p[0] = '\0';
p += 1;
}
} while (p != NULL);
}
p = cfg;
if (SQLConfigDataSource(NULL, (WORD) op, drv, cfg)) {
exit(0);
}
ProcessErrorMessages(msg);
exit(1);
}