OpenCBM
WINDOWS/configuration_name.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version
5  * 2 of the License, or (at your option) any later version.
6  *
7  * Copyright 2007,2008,2012 Spiro Trikaliotis
8  *
9 */
10 
20 #include "configuration.h"
21 #include "libmisc.h"
22 #include "opencbm-plugin-install.h"
23 #include "version.h"
24 
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <windows.h>
31 
33 #define DBG_USERMODE
34 
36 /* #define DBG_DLL */
37 
39 #define DBG_PROGNAME "OPENCBM.DLL"
40 
42 /* #define DBG_IS_DEBUG_C */
43 
44 #include "debug.h"
45 
46 #ifndef INVALID_FILE_ATTRIBUTES
47 #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
48 #endif
49 
51 #define FILENAME_CONFIGFILE "opencbm.conf"
52 
53 static char *
54 get_environment(const char * environment)
55 {
56  char * buffer = NULL;
57 
58  DWORD length;
59 
60  length = GetEnvironmentVariable(environment, NULL, 0);
61 
62  while (length > 0) {
63 
64  DWORD length2;
65 
66  free(buffer);
67  buffer = malloc(length + 1);
68 
69  length2 = GetEnvironmentVariable(environment, buffer, length + 1);
70 
71  if (length2 > length) {
72  length = length2;
73  }
74  else {
75  length = 0;
76  }
77  }
78  return buffer;
79 }
80 
81 static char *
82 get_windir()
83 {
84  return get_environment("WINDIR");
85 }
86 
87 static char *
88 get_userdir()
89 {
90  return get_environment("USERPROFILE");
91 }
92 
115 static char *
116 configuration_get_default_filename_candidate(unsigned int index)
117 {
118  char * buffer = NULL;
119 
120  switch (index)
121  {
122  case INDEX_DEFAULT_FILENAME_LOCAL /* 0 */:
124  break;
125 
126  case INDEX_DEFAULT_FILENAME_USERDIR /* 1 */:
127  {
128  char * userdir = get_userdir();
129  buffer = cbmlibmisc_strcat(userdir, "/" FILENAME_CONFIGFILE);
130  free(userdir);
131  }
132  break;
133 
134  case INDEX_DEFAULT_FILENAME_WINDIR /* 2 */:
135  {
136  char * windir = get_windir();
137  buffer = cbmlibmisc_strcat(windir, "/System32/" FILENAME_CONFIGFILE);
138  free(windir);
139  }
140  break;
141 
142  default:
143  /* no more candidates */
144  buffer = NULL;
145  break;
146  }
147 
148  return buffer;
149 }
150 
151 static int
152 FileExists(const char * filename)
153 {
154  DWORD attrib = GetFileAttributes(filename);
155 
156  return (attrib != INVALID_FILE_ATTRIBUTES && !(attrib & FILE_ATTRIBUTE_DIRECTORY));
157 }
158 
173 static unsigned int
174 configuration_get_default_filename_index(const char ** pstring_candidate)
175 {
176  unsigned int index = 0;
177  *pstring_candidate = NULL;
178 
179  while (NULL != (*pstring_candidate = configuration_get_default_filename_candidate(index)))
180  {
181  if (FileExists(*pstring_candidate)) {
182 DBG_PRINT((DBG_PREFIX "Candidate #%u '%s' does exist, DONE!", index, *pstring_candidate));
183  break;
184  };
185 
186  ++index;
187  };
188 
189  return (*pstring_candidate == NULL) ? -1 : index;
190 }
191 
199 const char *
201 {
202  char * string_candidate;
203 
204  configuration_get_default_filename_index(&string_candidate);
205 
206  return string_candidate;
207 }
208 
223 const char *
225 {
226  return configuration_get_default_filename_candidate(DefaultLocation);
227 }
228 
229 
#define FILENAME_CONFIGFILE
The filename of the configuration file.
const char * configuration_get_default_filename_for_install(unsigned int DefaultLocation)
Get the default filename for the configuration file on installation.
char * cbmlibmisc_strdup(const char *const OldString)
Duplicate a given string.
Definition: libstring.c:84
Define makros for debugging purposes.
#define INVALID_FILE_ATTRIBUTES
Shared library / DLL for accessing the driver Read configuration file.
char * cbmlibmisc_strcat(const char *First, const char *Second)
Concatenate two strings.
Definition: libstring.c:202
Plugin DLL interface.
Defining OpenCBM version.
#define DBG_PREFIX
Definition: debug.h:320
const char * configuration_get_default_filename(void)
Get the default filename for the configuration file.
Some functions for string handling.
#define DBG_PRINT(_xxx)
Definition: debug.h:403