ipdbg: fix double free of virtual-ir data
[openocd.git] / src / helper / configuration.c
blob16732eb3dd9f187554d964957b30409f5abf597c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2004, 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 ***************************************************************************/
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
15 #include "configuration.h"
16 #include "log.h"
17 #include "replacements.h"
19 static size_t num_config_files;
20 static char **config_file_names;
22 static size_t num_script_dirs;
23 static char **script_search_dirs;
25 void add_script_search_dir(const char *dir)
27 num_script_dirs++;
28 script_search_dirs = realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
30 script_search_dirs[num_script_dirs-1] = strdup(dir);
31 script_search_dirs[num_script_dirs] = NULL;
33 LOG_DEBUG("adding %s", dir);
36 void add_config_command(const char *cfg)
38 num_config_files++;
39 config_file_names = realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
41 config_file_names[num_config_files-1] = strdup(cfg);
42 config_file_names[num_config_files] = NULL;
45 void free_config(void)
47 while (num_config_files)
48 free(config_file_names[--num_config_files]);
50 free(config_file_names);
51 config_file_names = NULL;
53 while (num_script_dirs)
54 free(script_search_dirs[--num_script_dirs]);
56 free(script_search_dirs);
57 script_search_dirs = NULL;
60 /* return full path or NULL according to search rules */
61 char *find_file(const char *file)
63 FILE *fp = NULL;
64 char **search_dirs = script_search_dirs;
65 char *dir;
66 char const *mode = "r";
67 char *full_path;
69 /* Check absolute and relative to current working dir first.
70 * This keeps full_path reporting belowing working. */
71 full_path = alloc_printf("%s", file);
72 fp = fopen(full_path, mode);
74 while (!fp) {
75 free(full_path);
76 full_path = NULL;
77 dir = *search_dirs++;
79 if (!dir)
80 break;
82 full_path = alloc_printf("%s/%s", dir, file);
83 fp = fopen(full_path, mode);
86 if (fp) {
87 fclose(fp);
88 LOG_DEBUG("found %s", full_path);
89 return full_path;
92 free(full_path);
94 return NULL;
97 FILE *open_file_from_path(const char *file, const char *mode)
99 if (mode[0] != 'r')
100 return fopen(file, mode);
101 else {
102 char *full_path = find_file(file);
103 if (!full_path)
104 return NULL;
105 FILE *fp = NULL;
106 fp = fopen(full_path, mode);
107 free(full_path);
108 return fp;
112 int parse_config_file(struct command_context *cmd_ctx)
114 int retval;
115 char **cfg;
117 if (!config_file_names) {
118 command_run_line(cmd_ctx, "script openocd.cfg");
119 return ERROR_OK;
122 cfg = config_file_names;
124 while (*cfg) {
125 retval = command_run_line(cmd_ctx, *cfg);
126 if (retval != ERROR_OK)
127 return retval;
128 cfg++;
131 return ERROR_OK;
134 #ifndef _WIN32
135 #include <pwd.h>
136 #endif
138 char *get_home_dir(const char *append_path)
140 #ifdef _WIN32
141 char homepath[MAX_PATH];
142 #endif
144 char *home = getenv("HOME");
146 if (!home) {
148 #ifdef _WIN32
149 home = getenv("USERPROFILE");
151 if (!home) {
152 char *drive = getenv("HOMEDRIVE");
153 char *path = getenv("HOMEPATH");
154 if (drive && path) {
155 snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
156 home = homepath;
159 #else
160 struct passwd *pwd = getpwuid(getuid());
161 if (pwd)
162 home = pwd->pw_dir;
164 #endif
167 if (!home)
168 return home;
170 char *home_path;
172 if (append_path)
173 home_path = alloc_printf("%s/%s", home, append_path);
174 else
175 home_path = alloc_printf("%s", home);
177 return home_path;