2 * Copyright 2004-2005 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 #include <sys/types.h>
35 const char *cmus_config_dir
= NULL
;
36 const char *home_dir
= NULL
;
37 const char *user_name
= NULL
;
39 char **get_words(const char *text
)
51 while (text
[i
] && text
[i
] != ' ')
53 while (text
[i
] == ' ')
56 words
= xnew(char *, count
+ 1);
63 while (text
[i
] && text
[i
] != ' ')
65 words
[j
++] = xstrndup(text
+ start
, i
- start
);
66 while (text
[i
] == ' ')
73 int strptrcmp(const void *a
, const void *b
)
75 const char *as
= *(char **)a
;
76 const char *bs
= *(char **)b
;
78 return strcmp(as
, bs
);
81 static int dir_exists(const char *dirname
)
85 dir
= opendir(dirname
);
95 static void make_dir(const char *dirname
)
99 rc
= dir_exists(dirname
);
103 die_errno("error: opening `%s'", dirname
);
104 rc
= mkdir(dirname
, 0700);
106 die_errno("error: creating directory `%s'", dirname
);
109 static char *get_non_empty_env(const char *name
)
114 if (val
== NULL
|| val
[0] == 0)
121 home_dir
= get_non_empty_env("HOME");
122 if (home_dir
== NULL
)
123 die("error: environment variable HOME not set\n");
125 user_name
= get_non_empty_env("USER");
126 if (user_name
== NULL
) {
127 user_name
= get_non_empty_env("USERNAME");
128 if (user_name
== NULL
)
129 die("error: neither USER or USERNAME environment variable set\n");
132 cmus_config_dir
= get_non_empty_env("CMUS_HOME");
133 if (cmus_config_dir
== NULL
)
134 cmus_config_dir
= xstrjoin(home_dir
, "/.cmus");
135 make_dir(cmus_config_dir
);