+const char *get_default_configdir()
+{
+#if defined(HAVE_WIN32)
+ HRESULT hr;
+ static char szConfigDir[MAX_PATH + 1] = { 0 };
+
+ if (!p_SHGetFolderPath) {
+ bstrncpy(szConfigDir, DEFAULT_CONFIGDIR, sizeof(szConfigDir));
+ return szConfigDir;
+ }
+
+ if (szConfigDir[0] == '\0') {
+ hr = p_SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szConfigDir);
+
+ if (SUCCEEDED(hr)) {
+ bstrncat(szConfigDir, "\\Bacula", sizeof(szConfigDir));
+ } else {
+ bstrncpy(szConfigDir, DEFAULT_CONFIGDIR, sizeof(szConfigDir));
+ }
+ }
+ return szConfigDir;
+#else
+ return SYSCONFDIR;
+#endif
+}
+
+/*
+ * Returns false on error
+ * true on OK, with full_path set to where config file should be
+ */
+static bool
+find_config_file(const char *config_file, char *full_path, int max_path)
+{
+ int file_length = strlen(config_file) + 1;
+
+ /* If a full path specified, use it */
+ if (first_path_separator(config_file) != NULL) {
+ if (file_length > max_path) {
+ return false;
+ }
+ bstrncpy(full_path, config_file, file_length);
+ return true;
+ }
+
+ /* config_file is default file name, now find default dir */
+ const char *config_dir = get_default_configdir();
+ int dir_length = strlen(config_dir);
+
+ if ((dir_length + 1 + file_length) > max_path) {
+ return false;
+ }
+
+ memcpy(full_path, config_dir, dir_length + 1);
+
+ if (!IsPathSeparator(full_path[dir_length - 1])) {
+ full_path[dir_length++] = '/';
+ }
+
+ memcpy(&full_path[dir_length], config_file, file_length);
+
+ return true;
+}
+