|
@@ -10,6 +10,10 @@
|
10
|
10
|
#include <stdio.h>
|
11
|
11
|
#include <string.h>
|
12
|
12
|
|
|
13
|
+#if defined(unix) || defined(__APPLE__)
|
|
14
|
+#include <wordexp.h>
|
|
15
|
+#endif
|
|
16
|
+
|
13
|
17
|
#include "utils/strings.h"
|
14
|
18
|
|
15
|
19
|
bool stringEndsWith(const char *str, const char *suffix) {
|
|
@@ -56,64 +60,56 @@ char *bufferString(const char *string, ...) {
|
56
|
60
|
}
|
57
|
61
|
|
58
|
62
|
|
59
|
|
-char *fullPath(const char *path, char end)
|
60
|
|
-{
|
61
|
|
- unsigned int i, lenPath, lenEnv, len;
|
62
|
|
- char *env, *dir;
|
63
|
|
-
|
|
63
|
+char *fullPath(const char *path, char end) {
|
|
64
|
+ unsigned int lenPath, offset;
|
|
65
|
+ wordexp_t word;
|
|
66
|
+ char *dir;
|
64
|
67
|
|
65
|
68
|
if (!path || !path[0])
|
66
|
|
- return 0;
|
|
69
|
+ return NULL;
|
67
|
70
|
|
68
|
|
- if (path[0] == '~')
|
69
|
|
- {
|
|
71
|
+ if (path[0] == '~') {
|
70
|
72
|
#if defined(unix) || defined(__APPLE__)
|
71
|
|
- env = getenv("HOME");
|
72
|
|
-
|
73
|
|
- if (!env || !env[0])
|
74
|
|
- {
|
75
|
|
- return 0;
|
|
73
|
+ // Expand string into segments
|
|
74
|
+ if (wordexp(path, &word, 0) != 0) {
|
|
75
|
+ return NULL;
|
76
|
76
|
}
|
77
|
77
|
|
78
|
|
- lenEnv = strlen(env);
|
79
|
|
- lenPath = strlen(path);
|
80
|
|
- len = lenEnv + lenPath;
|
|
78
|
+ // Get length of complete string
|
|
79
|
+ lenPath = 0;
|
|
80
|
+ for (unsigned int i = 0; i < word.we_wordc; i++) {
|
|
81
|
+ lenPath += strlen(word.we_wordv[i]);
|
|
82
|
+ }
|
81
|
83
|
|
82
|
|
- dir = new char[len+1];
|
|
84
|
+ // Allocate new string
|
|
85
|
+ dir = new char[lenPath + 2]; // space for end char
|
83
|
86
|
|
84
|
|
- // Mongoose 2002.08.17, Copy ENV, strip '~', append rest of path
|
85
|
|
- for (i = 0; i < len; ++i)
|
86
|
|
- {
|
87
|
|
- if (i < lenEnv)
|
88
|
|
- {
|
89
|
|
- dir[i] = env[i];
|
90
|
|
- }
|
91
|
|
- else
|
92
|
|
- {
|
93
|
|
- dir[i] = path[1+(i-lenEnv)];
|
94
|
|
- }
|
|
87
|
+ // Copy segments into new string
|
|
88
|
+ offset = 0;
|
|
89
|
+ for (unsigned int i = 0; i < word.we_wordc; i++) {
|
|
90
|
+ unsigned int len = strlen(word.we_wordv[i]);
|
|
91
|
+ strncpy(dir + offset, word.we_wordv[i], len);
|
|
92
|
+ offset += len;
|
95
|
93
|
}
|
|
94
|
+
|
|
95
|
+ wordfree(&word);
|
96
|
96
|
#else
|
97
|
97
|
#error Platform not supported!
|
98
|
98
|
#endif
|
99
|
|
- }
|
100
|
|
- else
|
101
|
|
- {
|
|
99
|
+ } else {
|
102
|
100
|
lenPath = strlen(path);
|
103
|
|
- dir = new char[lenPath+1];
|
|
101
|
+ dir = new char[lenPath + 2]; // space for end char
|
104
|
102
|
strncpy(dir, path, lenPath);
|
105
|
|
-
|
106
|
|
- i = lenPath;
|
107
|
103
|
}
|
108
|
104
|
|
109
|
105
|
// Make sure ends in "end" char
|
110
|
|
- if (end && dir[i-1] != end)
|
111
|
|
- {
|
112
|
|
- dir[i++] = end;
|
|
106
|
+ if (end && (dir[lenPath - 1] != end)) {
|
|
107
|
+ dir[lenPath] = end;
|
|
108
|
+ dir[lenPath + 1] = 0;
|
|
109
|
+ } else {
|
|
110
|
+ dir[lenPath] = 0;
|
113
|
111
|
}
|
114
|
112
|
|
115
|
|
- dir[i] = 0;
|
116
|
|
-
|
117
|
113
|
return dir;
|
118
|
114
|
}
|
119
|
115
|
|