Statistics
| Branch: | Revision:

root / src / utils / abspath.cc @ cbd2c699

History | View | Annotate | Download (2.52 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  ABSPATH.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  converts the first argument to absolute path
8
//
9
//
10
//  Author: rhornig
11
//
12
//=========================================================================
13
14
/*--------------------------------------------------------------*
15
  Copyright (C) 2008 Rudolf Hornig
16
  Copyright (C) 2006-2008 OpenSim Ltd.
17

18
  This file is distributed WITHOUT ANY WARRANTY. See the file
19
  `license' for details on this and other legal matters.
20
*--------------------------------------------------------------*/
21
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <string>
25
#ifdef _WIN32
26
#include <direct.h>
27
#include <stdlib.h>
28
#endif
29
30
#include "../common/ver.h"
31
32
std::string toAbsolutePath(const char *pathname)
33
{
34
#ifdef _WIN32
35
    if ((pathname[0] && pathname[1]==':' && (pathname[2]=='/' || pathname[2]=='\\')) ||
36
        ((pathname[0]=='/' || pathname[0]=='\\') && (pathname[1]=='/' || pathname[1]=='\\')))
37
        return std::string(pathname);  // already absolute
38
39
    char wd[_MAX_PATH];
40
    if (pathname[0] && pathname[1]==':') // drive only, must get cwd on that drive
41
    {
42
        if (!_getdcwd(toupper(pathname[0])-'A'+1,wd,_MAX_PATH))
43
            return std::string(pathname);  // error (no such drive?), cannot help
44
        return std::string(wd) + "\\" + (pathname+2);
45
    }
46
    if (pathname[0]=='/' || pathname[0]=='\\')
47
    {
48
        // directory only, must prepend with current drive
49
        wd[0] = 'A'+_getdrive()-1;
50
        wd[1] = ':';
51
        wd[2] = '\0';
52
        return std::string(wd) + pathname;
53
    }
54
    if (!_getcwd(wd,_MAX_PATH))
55
        return std::string(pathname);  // error, cannot help
56
    return std::string(wd) + "\\" + pathname;   //XXX results in double backslash if wd is the root
57
#else
58
    if (pathname[0] == '/')
59
        return std::string(pathname);  // already absolute
60
61
    char wd[1024];
62
    return std::string(getcwd(wd,1024)) + "/" + pathname; //XXX results in double slash if wd is the root
63
#endif
64
}
65
66
int main(int argc, char *argv[])
67
{
68
69
    if (argc==1)
70
    {
71
         fprintf(stderr,
72
               "abspath -- part of " OMNETPP_PRODUCT ", (C) 2006-2008 OpenSim Ltd.\n"
73
               "Version: " OMNETPP_VERSION_STR ", build: " OMNETPP_BUILDID ", edition: " OMNETPP_EDITION "\n"
74
               "\n"
75
               "Returns the absolute path of the argument."
76
                );
77
78
         exit(0);
79
    }
80
81
    printf("%s", toAbsolutePath(argv[1]).c_str());
82
    return 0;
83
}
84