@@ -34,6 +34,8 @@ THE SOFTWARE.
34
34
#include <stdio.h>
35
35
#include <limits.h>
36
36
#include <assert.h>
37
+ #include <sys/types.h>
38
+ #include <pwd.h>
37
39
38
40
#include <piano.h>
39
41
@@ -43,32 +45,58 @@ THE SOFTWARE.
43
45
44
46
#define streq (a , b ) (strcmp (a, b) == 0)
45
47
46
- /* tries to guess your config dir; somehow conforming to
47
- * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
48
- * @param name of the config file (can contain subdirs too)
49
- * @param store the whole path here
50
- * @param but only up to this size
51
- * @return nothing
48
+ /* Get current user’s home directory
52
49
*/
53
- void BarGetXdgConfigDir (const char * filename , char * retDir ,
54
- size_t retDirN ) {
55
- char * xdgConfigDir = NULL ;
50
+ static char * BarSettingsGetHome () {
51
+ char * home ;
52
+
53
+ /* try environment variable */
54
+ if ((home = getenv ("HOME" )) != NULL && strlen (home ) > 0 ) {
55
+ return strdup (home );
56
+ }
57
+
58
+ /* try passwd mechanism */
59
+ struct passwd * const pw = getpwuid (getuid ());
60
+ if (pw != NULL && pw -> pw_dir != NULL && strlen (pw -> pw_dir ) > 0 ) {
61
+ return strdup (pw -> pw_dir );
62
+ }
63
+
64
+ return NULL ;
65
+ }
66
+
67
+ /* Get XDG config directory, which is set by BarSettingsRead (if not set)
68
+ */
69
+ static char * BarGetXdgConfigDir (const char * const filename ) {
70
+ assert (filename != NULL );
71
+
72
+ char * xdgConfigDir ;
56
73
57
74
if ((xdgConfigDir = getenv ("XDG_CONFIG_HOME" )) != NULL &&
58
75
strlen (xdgConfigDir ) > 0 ) {
59
- /* special dir: $xdg_config_home */
60
- snprintf (retDir , retDirN , "%s/%s" , xdgConfigDir , filename );
61
- } else {
62
- if ((xdgConfigDir = getenv ("HOME" )) != NULL &&
63
- strlen (xdgConfigDir ) > 0 ) {
64
- /* standard config dir: $home/.config */
65
- snprintf (retDir , retDirN , "%s/.config/%s" , xdgConfigDir ,
66
- filename );
67
- } else {
68
- /* fallback: working dir */
69
- snprintf (retDir , retDirN , "%s" , filename );
70
- }
76
+ const size_t len = (strlen (xdgConfigDir ) + 1 +
77
+ strlen (filename ) + 1 );
78
+ char * const concat = malloc (len * sizeof (* concat ));
79
+ snprintf (concat , len , "%s/%s" , xdgConfigDir , filename );
80
+ return concat ;
81
+ }
82
+
83
+ return NULL ;
84
+ }
85
+
86
+ /* Expand ~/ to user’s home directory
87
+ */
88
+ char * BarSettingsExpandTilde (const char * const path , const char * const home ) {
89
+ assert (path != NULL );
90
+ assert (home != NULL );
91
+
92
+ if (strncmp (path , "~/" , 2 ) == 0 ) {
93
+ char * const expanded = malloc ((strlen (home ) + 1 + strlen (path )- 2 + 1 ) *
94
+ sizeof (* expanded ));
95
+ sprintf (expanded , "%s/%s" , home , & path [2 ]);
96
+ return expanded ;
71
97
}
98
+
99
+ return strdup (path );
72
100
}
73
101
74
102
/* initialize settings structure
@@ -115,7 +143,14 @@ void BarSettingsDestroy (BarSettings_t *settings) {
115
143
* @return nothing yet
116
144
*/
117
145
void BarSettingsRead (BarSettings_t * settings ) {
118
- char * configfiles [] = {PACKAGE "/state" , PACKAGE "/config" };
146
+ char * const configfiles [] = {PACKAGE "/state" , PACKAGE "/config" };
147
+ char * const userhome = BarSettingsGetHome ();
148
+ assert (userhome != NULL );
149
+ /* set xdg config path (if not set) */
150
+ char * const defaultxdg = malloc (strlen (userhome ) + strlen ("/.config" ) + 1 );
151
+ sprintf (defaultxdg , "%s/.config" , userhome );
152
+ setenv ("XDG_CONFIG_HOME" , defaultxdg , 0 );
153
+ free (defaultxdg );
119
154
120
155
assert (sizeof (settings -> keys ) / sizeof (* settings -> keys ) ==
121
156
sizeof (dispatchActions ) / sizeof (* dispatchActions ));
@@ -140,8 +175,8 @@ void BarSettingsRead (BarSettings_t *settings) {
140
175
settings -> device = strdup ("android-generic" );
141
176
settings -> inkey = strdup ("R=U!LH$O2B#" );
142
177
settings -> outkey = strdup ("6#26FRL$ZWD" );
143
- settings -> fifo = malloc ( PATH_MAX * sizeof ( * settings -> fifo ) );
144
- BarGetXdgConfigDir ( PACKAGE "/ctl" , settings -> fifo , PATH_MAX );
178
+ settings -> fifo = BarGetXdgConfigDir ( PACKAGE "/ctl" );
179
+ assert ( settings -> fifo != NULL );
145
180
memcpy (settings -> tlsFingerprint , "\x2D\x0A\xFD\xAF\xA1\x6F\x4B\x5C\x0A"
146
181
"\x43\xF3\xCB\x1D\x47\x52\xF9\x53\x55\x07\xC0" ,
147
182
sizeof (settings -> tlsFingerprint ));
@@ -168,11 +203,13 @@ void BarSettingsRead (BarSettings_t *settings) {
168
203
/* read config files */
169
204
for (size_t j = 0 ; j < sizeof (configfiles ) / sizeof (* configfiles ); j ++ ) {
170
205
static const char * formatMsgPrefix = "format_msg_" ;
171
- char key [256 ], val [256 ], path [ PATH_MAX ] ;
206
+ char key [256 ], val [256 ];
172
207
FILE * configfd ;
173
208
174
- BarGetXdgConfigDir (configfiles [j ], path , sizeof (path ));
209
+ char * const path = BarGetXdgConfigDir (configfiles [j ]);
210
+ assert (path != NULL );
175
211
if ((configfd = fopen (path , "r" )) == NULL ) {
212
+ free (path );
176
213
continue ;
177
214
}
178
215
@@ -283,7 +320,7 @@ void BarSettingsRead (BarSettings_t *settings) {
283
320
settings -> listSongFormat = strdup (val );
284
321
} else if (streq ("fifo" , key )) {
285
322
free (settings -> fifo );
286
- settings -> fifo = strdup (val );
323
+ settings -> fifo = BarSettingsExpandTilde (val , userhome );
287
324
} else if (streq ("autoselect" , key )) {
288
325
settings -> autoselect = atoi (val );
289
326
} else if (streq ("tls_fingerprint" , key )) {
@@ -330,6 +367,7 @@ void BarSettingsRead (BarSettings_t *settings) {
330
367
}
331
368
332
369
fclose (configfd );
370
+ free (path );
333
371
}
334
372
335
373
/* check environment variable if proxy is not set explicitly */
@@ -339,18 +377,21 @@ void BarSettingsRead (BarSettings_t *settings) {
339
377
settings -> proxy = strdup (tmpProxy );
340
378
}
341
379
}
380
+
381
+ free (userhome );
342
382
}
343
383
344
384
/* write statefile
345
385
*/
346
386
void BarSettingsWrite (PianoStation_t * station , BarSettings_t * settings ) {
347
- char path [PATH_MAX ];
348
387
FILE * fd ;
349
388
350
389
assert (settings != NULL );
351
390
352
- BarGetXdgConfigDir (PACKAGE "/state" , path , sizeof (path ));
391
+ char * const path = BarGetXdgConfigDir (PACKAGE "/state" );
392
+ assert (path != NULL );
353
393
if ((fd = fopen (path , "w" )) == NULL ) {
394
+ free (path );
354
395
return ;
355
396
}
356
397
@@ -361,5 +402,6 @@ void BarSettingsWrite (PianoStation_t *station, BarSettings_t *settings) {
361
402
}
362
403
363
404
fclose (fd );
405
+ free (path );
364
406
}
365
407
0 commit comments