Handle streams separately in tree_add_track()
[cmus.git] / misc.c
blob8775b390545d6bbd4049025af2ddc7545aa35167
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
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
17 * 02111-1307, USA.
20 #include "misc.h"
21 #include "prog.h"
22 #include "xmalloc.h"
23 #include "xstrjoin.h"
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <dirent.h>
33 #include <stdarg.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)
41 char **words;
42 int i, j, count;
44 while (*text == ' ')
45 text++;
47 count = 0;
48 i = 0;
49 while (text[i]) {
50 count++;
51 while (text[i] && text[i] != ' ')
52 i++;
53 while (text[i] == ' ')
54 i++;
56 words = xnew(char *, count + 1);
58 i = 0;
59 j = 0;
60 while (text[i]) {
61 int start = i;
63 while (text[i] && text[i] != ' ')
64 i++;
65 words[j++] = xstrndup(text + start, i - start);
66 while (text[i] == ' ')
67 i++;
69 words[j] = NULL;
70 return words;
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)
83 DIR *dir;
85 dir = opendir(dirname);
86 if (dir == NULL) {
87 if (errno == ENOENT)
88 return 0;
89 return -1;
91 closedir(dir);
92 return 1;
95 static void make_dir(const char *dirname)
97 int rc;
99 rc = dir_exists(dirname);
100 if (rc == 1)
101 return;
102 if (rc == -1)
103 die_errno("error: opening `%s'", dirname);
104 rc = mkdir(dirname, 0700);
105 if (rc == -1)
106 die_errno("error: creating directory `%s'", dirname);
109 static char *get_non_empty_env(const char *name)
111 const char *val;
113 val = getenv(name);
114 if (val == NULL || val[0] == 0)
115 return NULL;
116 return xstrdup(val);
119 int misc_init(void)
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);
136 return 0;