blob: 090727d510283d3d844e922a529fe1dd03dbee51 (
plain) (
blame)
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
|
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <ct/path.h>
#include <ct/debug.h>
char *ct_basename(const char *file)
{
size_t l = strlen(file);
size_t n = l - 1;
while (--n) {
if (file[n] == '/')
break;
}
if (n == 0)
return strdup(file);
return strndup(file + n + 1, l - n);
}
char *ct_dirname(const char *file)
{
size_t l = strlen(file);
size_t n = l - 1;
while (--n) {
if (file[n] == '/')
break;
}
return strndup(file, n);
}
char *ct_cwdname()
{
size_t size;
long path_max = pathconf(".", _PC_PATH_MAX);
if (path_max == -1)
size = 1024;
else
size = (size_t)path_max;
char *buf = malloc(size);
if (!buf)
return NULL;
if (!getcwd(buf, size)) {
error("%s\n", strerror(errno));
free(buf);
return NULL;
}
return buf;
}
|