wavpack: Pass correct sample count to format_samples()
[cmus.git] / path.c
blobd71449f08fb446ff2fdf40f99100e491a079bf10
1 #include "path.h"
2 #include "xmalloc.h"
4 #include <stdlib.h>
5 #include <unistd.h>
7 void path_strip(char *str)
9 int i, s, d;
11 i = 0;
12 if (str[0] == '/')
13 i = 1;
14 while (str[i]) {
15 if (str[i] == '/') {
16 d = i;
17 s = i + 1;
18 while (str[s] && str[s] == '/')
19 s++;
20 s--;
21 do {
22 str[d++] = str[++s];
23 } while (str[s]);
24 } else if (i && str[i] == '.') {
25 if (str[i + 1] == '/') {
26 d = i;
27 s = i + 1;
28 do {
29 str[d++] = str[++s];
30 } while (str[s]);
31 } else if (str[i + 1] == 0) {
32 str[i] = 0;
33 break;
34 } else if (str[i + 1] == '.' &&
35 (str[i + 2] == '/' || str[i + 2] == 0)) {
36 /* aaa/bbb/../ccc */
37 /* aaa/ccc */
38 if (str[i + 2]) {
39 s = i + 3; /* ccc */
40 } else {
41 s = i + 2;
43 d = i - 1; /* /../ccc */
44 do {
45 if (d == 0)
46 break;
47 d--;
48 } while (str[d] != '/');
49 d++;
50 /* std[d] is bbb/../ccc */
51 i = d;
52 s--;
53 do {
54 str[d++] = str[++s];
55 } while (str[s]);
56 } else {
57 while (str[i] && str[i] != '/')
58 i++;
59 if (str[i])
60 i++;
62 } else {
63 while (str[i] && str[i] != '/')
64 i++;
65 if (str[i])
66 i++;
69 if (i > 1 && str[i - 1] == '/')
70 str[i - 1] = 0;
73 char *path_absolute_cwd(const char *src, const char *cwd)
75 char *str;
77 if (src[0] == '/') {
78 /* already absolute */
79 str = xstrdup(src);
80 } else {
81 int src_len;
82 int cwd_len;
84 src_len = strlen(src);
85 cwd_len = strlen(cwd);
86 str = xnew(char, cwd_len + 1 + src_len + 1);
87 memcpy(str, cwd, cwd_len);
88 str[cwd_len] = '/';
89 memcpy(str + cwd_len + 1, src, src_len + 1);
91 path_strip(str);
92 return str;
95 char *path_absolute(const char *src)
97 char cwd[1024];
99 if (!getcwd(cwd, sizeof(cwd))) {
100 cwd[0] = '/';
101 cwd[1] = 0;
103 return path_absolute_cwd(src, cwd);