-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathklib_path.c
executable file
·347 lines (306 loc) · 9.09 KB
/
klib_path.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*===========================================================================
klib
klib_path.c
(c)2000-2012 Kevin Boone
============================================================================*/
#ifdef WIN32
#include <windows.h>
#endif
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "klib_defs.h"
#include "klib_log.h"
#include "klib_string.h"
#include "klib_path.h"
#include "klib_error.h"
/*===========================================================================
private data
============================================================================*/
extern void klib_path_init (klib_Object *self);
void klib_path_dispose (klib_Object *self);
static klib_Spec klib_spec_path =
{
.obj_size = sizeof (klib_Path),
.init_fn = klib_path_init,
.class_name = "klib_Path"
};
typedef struct _klib_Path_priv
{
struct stat sb;
BOOL stat_valid;
} klib_Path_priv;
/*===========================================================================
klib_path_init
============================================================================*/
void klib_path_init (klib_Object *_self)
{
KLIB_IN
klib_string_init (_self);
_self->dispose = klib_path_dispose;
klib_Path *self = (klib_Path *)_self;
self->priv = (klib_Path_priv *) malloc (sizeof (klib_Path_priv));
memset (self->priv, 0, sizeof (klib_Path_priv));
KLIB_OUT
}
/*===========================================================================
klib_path_new
============================================================================*/
klib_Path *klib_path_new (const char *s)
{
KLIB_IN
klib_Path *self = (klib_Path *)klib_object_new (&klib_spec_path);
klib_string_set ((klib_String *)self, s);
self->priv->stat_valid = FALSE;
KLIB_OUT
return self;
}
/*===========================================================================
klib_path_new
============================================================================*/
void klib_path_append (klib_Path *self, const char *file)
{
klib_String *_self = (klib_String *)self;
const char *s_self = klib_string_cstr (_self);
if (strlen (s_self) == 0)
{
// Don't accidentally make an absolute path by appending a
// separator onto an empty string
}
else
{
#ifdef WIN32
if (s_self[strlen(s_self) - 1] != '\\')
klib_string_append (_self, "\\");
#else
if (s_self[strlen(s_self) - 1] != '/')
klib_string_append (_self, "/");
#endif
}
klib_string_append (_self, file);
self->priv->stat_valid = FALSE;
}
/*===========================================================================
klib_path_new
============================================================================*/
void klib_path_free (klib_Path *self)
{
klib_object_unref ((klib_Object *)self);
}
/*===========================================================================
klib_path_new
============================================================================*/
const char *klib_path_cstr (const klib_Path *self)
{
return klib_string_cstr ((klib_String *)self);
}
/*===========================================================================
klib_path_refresh_info
============================================================================*/
void klib_path_refresh_info (klib_Path *self, klib_Error **error)
{
self->priv->stat_valid = FALSE;
char *_path = strdup (klib_path_cstr (self));
#ifdef WIN32
if (_path[strlen(_path) - 1] == '\\')
_path[strlen(_path) - 1] = 0;
#endif
if (stat (_path, &self->priv->sb) == 0)
{
self->priv->stat_valid = TRUE;
}
else
{
*error = klib_error_new (errno, "Error %d: %s (%s)",
klib_error_strerror (errno), "Stat call failed",
klib_path_cstr (self));
}
free (_path);
}
/*===========================================================================
klib_path_get_mtime
============================================================================*/
time_t klib_path_get_mtime (klib_Path *self)
{
if (!self->priv->stat_valid)
{
klib_Error *error = NULL;
klib_path_refresh_info (self, &error);
if (error) klib_error_free (error);
}
return self->priv->sb.st_mtime;
}
/*===========================================================================
klib_path_get_is_file
============================================================================*/
BOOL klib_path_is_file (klib_Path *self)
{
if (!self->priv->stat_valid)
{
klib_Error *error = NULL;
klib_path_refresh_info (self, &error);
if (error) klib_error_free (error);
}
return S_ISREG (self->priv->sb.st_mode);
}
/*===========================================================================
klib_path_get_is_dir
============================================================================*/
BOOL klib_path_is_dir (klib_Path *self)
{
if (!self->priv->stat_valid)
{
klib_Error *error = NULL;
klib_path_refresh_info (self, &error);
if (error)
if (error) klib_error_free (error);
}
return S_ISDIR (self->priv->sb.st_mode);
}
/*===========================================================================
klib_path_dispose
============================================================================*/
void klib_path_dispose (klib_Object *_self)
{
KLIB_IN
klib_Path *self = (klib_Path *)_self;
if (!self->disposing)
{
self->disposing = TRUE;
if (self->priv)
{
free (self->priv);
}
}
klib_string_dispose ((klib_Object *)self);
KLIB_OUT
}
/*===========================================================================
_klib_path_get_shortname
============================================================================*/
char *_klib_path_get_shortname (const char *path)
{
char *ret = NULL;
KLIB_IN
if (path)
{
#ifdef WIN32
const char *p = strrchr (path, '\\');
#else
const char *p = strrchr (path, '/');
#endif
if (p)
{
ret = strdup (p + 1);
}
else
ret = strdup (path);
}
KLIB_OUT
return ret;
}
/*===========================================================================
klib_path_get_shortname
============================================================================*/
char *klib_path_get_shortname (const klib_Path *self)
{
KLIB_IN
char *ret = _klib_path_get_shortname (klib_path_cstr (self));
KLIB_OUT
return ret;
}
/*===========================================================================
_klib_path_get_dir
============================================================================*/
char *_klib_path_get_dir (const klib_Path *self)
{
KLIB_IN
char *ret = NULL;
char *path = strdup (klib_path_cstr (self));
if (path)
{
#ifdef WIN32
char *p = strrchr (path, '\\');
#else
char *p = strrchr (path, '/');
#endif
if (p)
{
*(p) = 0;
ret = strdup (path);
}
else
ret = strdup (""); // No path component
}
free (path);
KLIB_OUT
return ret;
}
/*===========================================================================
_klib_path_get_dir
============================================================================*/
klib_Path *klib_path_get_dir (const klib_Path *self)
{
KLIB_IN
char *dir = _klib_path_get_dir (self);
klib_Path *ret = klib_path_new (dir);
free (dir);
KLIB_OUT
return ret;
}
/*===========================================================================
klib_path_get_full_argv0
============================================================================*/
// The argument passed as argv[0] on Linux will not be very useful,
// as it is not guaranteed to be a full path to anything. In fact, it's
// not guaranteed even to be a relative path to anything, if we found
// the file via $PATH. /proc/id/exe is the standard solution to
// this problem on Linux
// Caller must free the path object returned
klib_Path *klib_path_get_full_argv0 (void)
{
#ifdef WIN32
char buff[1024];
GetModuleFileName (NULL, buff, sizeof (buff));
return klib_path_new (buff);
#else
char szTmp[32];
char *buff = (char *) malloc (1024); // Should never be this long!
memset (buff, 0, 1024);
snprintf(szTmp, sizeof (szTmp), "/proc/%d/exe", getpid());
readlink (szTmp, buff, 1023);
klib_Path *p = klib_path_new (buff);
free (buff);
return p;
#endif
}
/*==========================================================================
klib_path_get_home_dir
Returns a home directory appropriate for the plaform
============================================================================*/
klib_Path *klib_path_get_home_dir (void)
{
#ifdef WIN32
return klib_path_new (getenv("APPDATA"));
#else
return klib_path_new (getenv("HOME"));
#endif
}
/*==========================================================================
klib_path_get_ext
============================================================================*/
char *klib_path_get_ext (const klib_Path *self)
{
if (!self) return strdup ("");
const char *path = klib_path_cstr (self);
if (!path) return strdup ("");
char *p = strrchr (path, '.');
if (p) return strdup (p + 1);
return strdup ("");
}