Child: [5ca462] (diff)

Download this file

pathut.cpp    69 lines (49 with data), 1.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef lint
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.1 2004-12-10 18:13:14 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#ifndef TEST_PATHUT
#include <pwd.h>
#include "pathut.h"
std::string path_getfather(const std::string &s) {
std::string father = s;
// ??
if (father.empty())
return "./";
if (father[father.length() - 1] == '/') {
// Input ends with /. Strip it, handle special case for root
if (father.length() == 1)
return father;
father.erase(father.length()-1);
}
std::string::size_type slp = father.rfind('/');
if (slp == std::string::npos)
return "./";
father.erase(slp);
path_catslash(father);
return father;
}
std::string path_home()
{
uid_t uid = getuid();
struct passwd *entry = getpwuid(uid);
if (entry == 0)
return "/";
std::string homedir = entry->pw_dir;
path_catslash(homedir);
return homedir;
}
#else // TEST_PATHUT
#include <iostream>
using namespace std;
#include "pathut.h"
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
"/dir1/dir2",
"./dir", "./dir1/", "dir", "../dir"};
int main(int argc, const char **argv)
{
for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
cout << tstvec[i] << " -> " << path_getfather(tstvec[i]) << endl;
}
return 0;
}
#endif // TEST_PATHUT