Bump version to 13
[a2jmidid.git] / paths.c
blobcefae210799f0bc217b448caa49d68a3a52bca11
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * ALSA SEQ < - > JACK MIDI bridge
5 * Copyright (c) 2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/stat.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
27 #include "paths.h"
28 #include "log.h"
30 #define DEFAULT_XDG_LOG "/.log"
31 #define DEFAULT_XDG_CONF "/.config"
32 #define A2J_XDG_SUBDIR "/a2j"
33 #define A2J_XDG_LOG "/a2j.log"
34 #define A2J_XDG_CONF "/a2j.conf"
36 char * g_a2j_log_path = NULL;
37 char * g_a2j_conf_path = NULL;
39 char *
40 catdup(
41 const char * str1,
42 const char * str2)
44 char * str;
45 size_t str1_len;
46 size_t str2_len;
48 str1_len = strlen(str1);
49 str2_len = strlen(str2);
51 str = malloc(str1_len + str2_len + 1);
52 if (str == NULL)
54 return NULL;
57 memcpy(str, str1, str1_len);
58 memcpy(str + str1_len, str2, str2_len);
59 str[str1_len + str2_len] = 0;
61 return str;
64 bool
65 ensure_dir_exist(
66 const char * dirname,
67 int mode)
69 struct stat st;
70 if (stat(dirname, &st) != 0)
72 if (errno == ENOENT)
74 a2j_info("Directory \"%s\" does not exist. Creating...", dirname);
75 if (mkdir(dirname, mode) != 0)
77 a2j_error("Failed to create \"%s\" directory: %d (%s)", dirname, errno, strerror(errno));
78 return false;
81 else
83 a2j_error("Failed to stat \"%s\": %d (%s)", dirname, errno, strerror(errno));
84 return false;
87 else
89 if (!S_ISDIR(st.st_mode))
91 a2j_error("\"%s\" exists but is not directory.", dirname);
92 return false;
96 return true;
99 char *
100 a2j_path_init(
101 const char * home_dir,
102 const char * purpose_subdir,
103 const char * file)
105 char * dir1;
106 char * dir2;
107 char * filepath;
109 filepath = NULL;
111 dir1 = catdup(home_dir, purpose_subdir);
112 if (dir1 == NULL)
114 a2j_error("Out of memory");
115 goto exit;
118 dir2 = catdup(dir1, A2J_XDG_SUBDIR);
119 if (dir2 == NULL)
121 a2j_error("Out of memory");
122 goto free_dir1;
125 if (!ensure_dir_exist(dir1, 0700))
127 goto free_dir1;
130 if (!ensure_dir_exist(dir2, 0700))
132 goto free_dir2;
135 filepath = catdup(dir2, file);
136 if (filepath == NULL)
138 a2j_error("Out of memory");
141 free_dir2:
142 free(dir2);
144 free_dir1:
145 free(dir1);
147 exit:
148 return filepath;
151 bool
152 a2j_paths_init(void)
154 const char * home_dir;
156 home_dir = getenv("HOME");
157 if (home_dir == NULL)
159 a2j_error("Environment variable HOME not set");
160 goto exit;
163 g_a2j_log_path = a2j_path_init(home_dir, DEFAULT_XDG_LOG, A2J_XDG_LOG);
164 if (g_a2j_log_path == NULL)
166 goto exit;
169 g_a2j_conf_path = a2j_path_init(home_dir, DEFAULT_XDG_CONF, A2J_XDG_CONF);
170 if (g_a2j_conf_path == NULL)
172 goto free_log_path;
175 return true;
177 free_log_path:
178 free(g_a2j_log_path);
180 exit:
181 return false;
184 void
185 a2j_paths_uninit(void)
187 if (g_a2j_conf_path != NULL)
189 free(g_a2j_conf_path);
192 if (g_a2j_log_path != NULL)
194 free(g_a2j_log_path);