File: determineinitialdir.cc

package info (click to toggle)
xd 3.11.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 304 kB
  • ctags: 102
  • sloc: cpp: 703; makefile: 139
file content (60 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download
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
#include "alternatives.ih"

string Alternatives::determineInitialDirectory()
{
    string cwd;
    auto_ptr<char> buffer;
    bool rescan = false;

    switch (d_command.action())
    {
        case Command::FROM_CONFIG:
            cwd = d_home ? d_homeDir : string("/");
            rescan = true;
        break;

        case Command::FROM_HOME:
            cwd = d_homeDir;
        break;

        case Command::FROM_ROOT:
            cwd = "/";
        break;

        case Command::FROM_CWD:
            getCwd(&buffer);
            cwd = buffer.get();
        break;

        case Command::FROM_PARENT:
            getCwd(&buffer);
            cwd = buffer.get();
            size_t pos = cwd.length();
            for (size_t idx = d_command.parent(); pos && idx--; )
                pos = cwd.rfind('/', pos - 1);

            if (pos > 0)
                cwd.resize(pos);
            else
                cwd = '/';
        break;
    }

    if (d_addRoot != FALSE && (!d_home || !rescan))
    {
        msg() << "Search does not start at the home dir: "
                "no additional search from the root" << info;

        d_addRoot = FALSE;
    }

    msg() << "Resolved Cwd as: " << cwd << info;

    if (*cwd.rbegin() != '/')         // all dirs end in /
        cwd += '/';

    return cwd;
}