forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cc
More file actions
45 lines (41 loc) · 951 Bytes
/
util.cc
File metadata and controls
45 lines (41 loc) · 951 Bytes
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
#include "libpstack/util.h"
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
std::string g_openPrefix;
std::string
dirname(const std::string &in)
{
auto it = in.rfind('/');
if (it == std::string::npos)
return ".";
return in.substr(0, it);
}
std::string
basename(const std::string &in)
{
auto it = in.rfind('/');
auto out = it == std::string::npos ? in : in.substr(it + 1);
return out;
}
std::string
linkResolve(std::string name)
{
char buf[1024];
int rc;
for (;;) {
rc = readlink(name.c_str(), buf, sizeof buf - 1);
if (rc == -1)
break;
buf[rc] = 0;
if (buf[0] != '/') {
auto lastSlash = name.rfind('/');
name = lastSlash == std::string::npos
? std::string(buf)
: name.substr(0, lastSlash + 1) + std::string(buf);
} else {
name = buf;
}
}
return name;
}