-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cc
More file actions
53 lines (47 loc) · 1.08 KB
/
Copy pathutils.cc
File metadata and controls
53 lines (47 loc) · 1.08 KB
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
/*
* utils.cc
* Copyright (C) 2022 youfa.song <vsyfar@gmail.com>
*
* Distributed under terms of the GPLv2 license.
*/
#include "utils.h"
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdio>
namespace ave {
namespace base {
std::string nameForFd(int fd) {
const size_t SIZE = 256;
char buffer[SIZE];
std::string result;
snprintf(buffer, SIZE, "/proc/%d/fd/%d", getpid(), fd);
struct stat s {};
if (lstat(buffer, &s) == 0) {
if ((s.st_mode & S_IFMT) == S_IFLNK) {
char linkto[256];
int len = readlink(buffer, linkto, sizeof(linkto));
if (len > 0) {
if (len > 255) {
linkto[252] = '.';
linkto[253] = '.';
linkto[254] = '.';
linkto[255] = 0;
} else {
linkto[len] = 0;
}
result.append(linkto);
}
} else {
result.append("unexpected type for ");
result.append(buffer);
}
} else {
result.append("couldn't open ");
result.append(buffer);
}
return result;
}
} // namespace base
} /* namespace ave */