Skip to content

Commit 85fbecb

Browse files
committed
Fix config file loading on Windows
Fix config file loading on Windows which has become unreliable due _pgmptr deprecation, instead use GetModuleFileName. Also: * Use PATH_MAX instead of magic 1024 * Correctly test for sizes when manipulating buffers. * Use the correct path seperator on Windows.
1 parent 14999e0 commit 85fbecb

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

config.c

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#include <string.h>
1414
#include <stdlib.h>
1515
#include <ctype.h>
16+
#ifndef _WIN32
17+
#include <unistd.h>
18+
#include <sys/param.h>
19+
#endif
20+
1621

1722
#include "config.h"
1823

@@ -26,10 +31,14 @@
2631
#define strcasecmp stricmp
2732
#endif
2833

34+
#define CONFIG_FILE "qstat.cfg"
35+
2936
#ifdef _WIN32
30-
#define HOME_CONFIG_FILE "qstat.cfg"
37+
#define HOME_CONFIG_FILE CONFIG_FILE
38+
#define SEP "\\"
3139
#else
3240
#define HOME_CONFIG_FILE ".qstatrc"
41+
#define SEP "/"
3342
#endif
3443

3544
static server_type **config_types;
@@ -177,12 +186,17 @@ print_location()
177186
}
178187

179188

189+
/*
190+
* 1. $QSTAT_CONFIG
191+
* 2. UNIX: $HOME/.qstatrc WIN: $HOME/qstat.cfg
192+
* 3. UNIX: sysconfdir/qstat.cfg WIN: qstat.exe-dir/qstat.cfg
193+
*/
180194
int
181195
qsc_load_default_config_files()
182196
{
183197
int rc = 0;
184198
char *filename = NULL, *var;
185-
char path[1024];
199+
char path[PATH_MAX];
186200

187201
var = getenv("QSTAT_CONFIG");
188202
if ((var != NULL) && (var[0] != '\0')) {
@@ -195,70 +209,47 @@ qsc_load_default_config_files()
195209
var = getenv("HOME");
196210
if ((var != NULL) && (var[0] != '\0')) {
197211
int len = strlen(var);
198-
if (len > 900) {
199-
len = 900;
212+
if (len > PATH_MAX - strlen(HOME_CONFIG_FILE) + 2) {
213+
fprintf(stderr, "Path for HOME \"%s\" too long\n", var);
214+
return (-1);
200215
}
201216
strncpy(path, var, len);
202217
path[len] = '\0';
203-
strcat(path, "/");
204-
strcat(path, HOME_CONFIG_FILE);
205-
/* sprintf( path, "%s/%s", var, HOME_CONFIG_FILE); */
218+
strcat(path, SEP HOME_CONFIG_FILE);
206219
rc = try_load_config_file(path, 0);
207220
if ((rc == 0) || (rc == -1)) {
208221
return (rc);
209222
}
210223
}
211224

212225
#ifdef sysconfdir
213-
strcpy(path, sysconfdir "/qstat.cfg");
214-
filename = path;
226+
strcpy(path, sysconfdir SEP CONFIG_FILE);
227+
filename = path;
215228
#elif defined(_WIN32)
216-
if ((filename == NULL) && _pgmptr && strchr(_pgmptr, '\\')) {
217-
char *slash = strrchr(_pgmptr, '\\');
218-
strncpy(path, _pgmptr, slash - _pgmptr);
219-
path[slash - _pgmptr] = '\0';
220-
strcat(path, "\\qstat.cfg");
221-
filename = path;
222-
}
229+
// Look in the binaries directory
230+
rc = GetModuleFileName(NULL, path, PATH_MAX);
231+
if (rc == PATH_MAX) {
232+
fprintf(stderr, "Module path too long\n");
233+
return (1);
234+
}
235+
var = strrchr(path, '\\');
236+
if (var == NULL) {
237+
fprintf(stderr, "Unexpected module path \"%s\" (no seperator %s)\n", path, SEP);
238+
return (-1);
239+
}
240+
*var = '\0';
241+
if (strlen(path) >= PATH_MAX - 11) {
242+
fprintf(stderr, "Module path \"%s\" too long\n", path);
243+
return (-1);
244+
}
245+
strcat(path, SEP CONFIG_FILE);
246+
filename = path;
223247
#endif
224248

225249
if (filename != NULL) {
226250
rc = try_load_config_file(filename, 0);
227-
if ((rc == 0) || (rc == -1)) {
228-
return (rc);
229-
}
230251
}
231252
return (rc);
232-
233-
/*
234-
* if ( rc == -2 && show_error) {
235-
* perror( filename);
236-
* fprintf( stderr, "Error: Could not open config file \"%s\"\n",
237-
* filename);
238-
* }
239-
* else if ( rc == -1 && show_error)
240-
* fprintf( stderr, "Error: Error loading $QSTAT_CONFIG file\n");
241-
* return rc;
242-
*/
243-
244-
#ifdef foo
245-
filename = getenv("HOME");
246-
if ((filename != NULL) && (filename[0] != '\0')) {
247-
char path[1024];
248-
snprintf(path, (sizeof(path) - 1), "%s/%s", var, HOME_CONFIG_FILE);
249-
}
250-
251-
/* 1. $QSTAT_CONFIG
252-
* 2. UNIX: $HOME/.qstatrc WIN: $HOME/qstat.cfg
253-
* 3. UNIX: sysconfdir/qstat.cfg WIN: qstat.exe-dir/qstat.cfg
254-
*/
255-
256-
rc = load_config_file("qstat.cfg");
257-
if (rc == -1) {
258-
fprintf(stderr, "Warning: Error loading default qstat.cfg\n");
259-
}
260-
return (0);
261-
#endif
262253
}
263254

264255

0 commit comments

Comments
 (0)