Switch to unified view

a/src/utils/pathut.cpp b/src/utils/pathut.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.2 2004-12-14 17:54:16 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.3 2005-01-31 14:31:10 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
4
5
#ifndef TEST_PATHUT
5
#ifndef TEST_PATHUT
6
6
7
#include <pwd.h>
7
#include <pwd.h>
8
#include <iostream>
8
9
9
#include "pathut.h"
10
#include "pathut.h"
11
using std::string;
10
12
11
std::string path_getfather(const std::string &s) {
13
string path_getfather(const string &s) {
12
    std::string father = s;
14
    string father = s;
13
15
14
    // ??
16
    // ??
15
    if (father.empty())
17
    if (father.empty())
16
    return "./";
18
    return "./";
17
19
...
...
20
    if (father.length() == 1)
22
    if (father.length() == 1)
21
        return father;
23
        return father;
22
    father.erase(father.length()-1);
24
    father.erase(father.length()-1);
23
    }
25
    }
24
26
25
    std::string::size_type slp = father.rfind('/');
27
    string::size_type slp = father.rfind('/');
26
    if (slp == std::string::npos)
28
    if (slp == string::npos)
27
    return "./";
29
    return "./";
28
30
29
    father.erase(slp);
31
    father.erase(slp);
30
    path_catslash(father);
32
    path_catslash(father);
31
    return father;
33
    return father;
32
}
34
}
33
35
34
std::string path_getsimple(const std::string &s) {
36
string path_getsimple(const string &s) {
35
    std::string simple = s;
37
    string simple = s;
36
38
37
    if (simple.empty())
39
    if (simple.empty())
38
    return simple;
40
    return simple;
39
41
40
    std::string::size_type slp = simple.rfind('/');
42
    string::size_type slp = simple.rfind('/');
41
    if (slp == std::string::npos)
43
    if (slp == string::npos)
42
    return simple;
44
    return simple;
43
45
44
    simple.erase(0, slp+1);
46
    simple.erase(0, slp+1);
45
    return simple;
47
    return simple;
46
}
48
}
47
49
48
std::string path_home()
50
string path_home()
49
{
51
{
50
    uid_t uid = getuid();
52
    uid_t uid = getuid();
51
53
52
    struct passwd *entry = getpwuid(uid);
54
    struct passwd *entry = getpwuid(uid);
53
    if (entry == 0)
55
    if (entry == 0) {
56
  const char *cp = getenv("HOME");
57
  if (cp)
58
      return cp;
59
  else 
54
    return "/";
60
    return "/";
61
    }
55
62
56
    std::string homedir = entry->pw_dir;
63
    string homedir = entry->pw_dir;
57
    path_catslash(homedir);
64
    path_catslash(homedir);
58
    return homedir;
65
    return homedir;
66
}
67
68
extern string path_tildexpand(const string &s) 
69
{
70
    if (s.empty() || s[0] != '~')
71
  return s;
72
    string o = s;
73
    if (s.length() == 1) {
74
  o.replace(0, 1, path_home());
75
    } else if  (s[1] == '/') {
76
  o.replace(0, 2, path_home());
77
    } else {
78
  string::size_type pos = s.find('/');
79
  int l = (pos == string::npos) ? s.length() - 1 : pos - 1;
80
  struct passwd *entry = getpwnam(s.substr(1, l).c_str());
81
  if (entry)
82
      o.replace(0, l+1, entry->pw_dir);
83
    }
84
    return o;
59
}
85
}
60
86
61
#else // TEST_PATHUT
87
#else // TEST_PATHUT
62
88
63
#include <iostream>
89
#include <iostream>
...
...
69
             "/dir1/dir2",
95
             "/dir1/dir2",
70
            "./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
96
            "./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
71
            "/dir/.c",
97
            "/dir/.c",
72
};
98
};
73
99
100
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
101
       "~nosuch", "~nosuch/sub"};
102
int nttvec = sizeof(ttvec) / sizeof(string);
103
74
int main(int argc, const char **argv)
104
int main(int argc, const char **argv)
75
{
105
{
76
106
#if 0
77
    for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
107
    for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
78
    cout << tstvec[i] << " FATHER " << path_getfather(tstvec[i]) << endl;
108
    cout << tstvec[i] << " FATHER " << path_getfather(tstvec[i]) << endl;
79
    }
109
    }
80
    for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
110
    for (int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
81
    cout << tstvec[i] << " SIMPLE " << path_getsimple(tstvec[i]) << endl;
111
    cout << tstvec[i] << " SIMPLE " << path_getsimple(tstvec[i]) << endl;
82
    }
112
    }
113
#endif
114
    string s;
115
116
    for (int i = 0; i < nttvec; i++) {
117
  cout << "tildexp: '" << ttvec[i] << "' -> '" << 
118
      path_tildexpand(ttvec[i]) << "'" << endl;
119
    }
120
    
121
122
83
    return 0;
123
    return 0;
84
}
124
}
85
125
86
#endif // TEST_PATHUT
126
#endif // TEST_PATHUT
87
127