File: chdir.cc

package info (click to toggle)
icmake 13.05.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,132 kB
  • sloc: cpp: 11,595; fortran: 883; makefile: 853; sh: 546; pascal: 342
file content (41 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download | duplicates (3)
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
//#define XERR
#include "builtin.ih"

// This function expects a string to change-dir to as second
// argument on the stack.  The stack top is the mode, 
// P_CHECK or P_NOCHECK.  
// 
// If the directory name is not empty, then the current working
// directory is set to the indicated path.  If the requested directory is an
// empty string, then a change-dir is performed to the startup directory.
// 
// Mode P_CHECK causes this function to abort upon failure.
// 
// The return register is set to the resulting working directory.

void Builtin::chdir()
{
    auto   mode = d_stack[2].value();
    string const &requestDir = d_stack[3].str();
    string dir = requestDir;

    if (dir.empty())
        dir = d_orgDir;

    error_code ec;
    dir = filesystem::absolute(dir, ec);        // abs. destination dir

    filesystem::current_path(dir, ec);          // change to the path

    dir = filesystem::current_path().string();  // obtain the cwd's path

    if (ec and mode == P_CHECK)         // chdir failed
            throw Exception{} << "can't chdir to `" << requestDir << '\'';

    if (dir.back() != '/')              // make sure there's a final /
        dir += '/';

    d_reg = move(dir);                  // assign the return register
}