Use ~/.cmus/socket instead of /tmp/cmus-$USER
[cmus.git] / path.c
blobb54be1252ac2c9f2565de264f253a9a96dd5bdca
1 /*
2 * Copyright 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 <path.h>
21 #include <xmalloc.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 void path_strip(char *str)
28 int i, s, d;
30 i = 0;
31 if (str[0] == '/')
32 i = 1;
33 while (str[i]) {
34 if (str[i] == '/') {
35 d = i;
36 s = i + 1;
37 while (str[s] && str[s] == '/')
38 s++;
39 s--;
40 do {
41 str[d++] = str[++s];
42 } while (str[s]);
43 } else if (i && str[i] == '.') {
44 if (str[i + 1] == '/') {
45 d = i;
46 s = i + 1;
47 do {
48 str[d++] = str[++s];
49 } while (str[s]);
50 } else if (str[i + 1] == 0) {
51 str[i] = 0;
52 break;
53 } else if (str[i + 1] == '.' &&
54 (str[i + 2] == '/' || str[i + 2] == 0)) {
55 /* aaa/bbb/../ccc */
56 /* aaa/ccc */
57 if (str[i + 2]) {
58 s = i + 3; /* ccc */
59 } else {
60 s = i + 2;
62 d = i - 1; /* /../ccc */
63 do {
64 if (d == 0)
65 break;
66 d--;
67 } while (str[d] != '/');
68 d++;
69 /* std[d] is bbb/../ccc */
70 i = d;
71 s--;
72 do {
73 str[d++] = str[++s];
74 } while (str[s]);
75 } else {
76 while (str[i] && str[i] != '/')
77 i++;
78 if (str[i])
79 i++;
81 } else {
82 while (str[i] && str[i] != '/')
83 i++;
84 if (str[i])
85 i++;
88 if (i > 1 && str[i - 1] == '/')
89 str[i - 1] = 0;
92 char *path_absolute(const char *src)
94 char *str;
96 if (src[0] == '/') {
97 /* already absolute */
98 str = xstrdup(src);
99 } else {
100 char cwd[1024];
101 int src_len;
102 int cwd_len;
104 if (getcwd(cwd, sizeof(cwd)) == NULL)
105 return NULL;
106 src_len = strlen(src);
107 cwd_len = strlen(cwd);
108 str = xnew(char, cwd_len + 1 + src_len + 1);
109 memcpy(str, cwd, cwd_len);
110 str[cwd_len] = '/';
111 memcpy(str + cwd_len + 1, src, src_len + 1);
113 path_strip(str);
114 return str;