3 // Various OS specific functions
7 #include "support/filetools.h"
8 #define INCL_DOSFILEMGR
9 #define INCL_DOSMODULEMGR
10 #define INCL_DOSPROCESS
12 #define INCL_DOSERRORS
20 os::shell_type shell_ = os::UNIX;
21 unsigned long cp_ = 0;
29 void init(int * argc, char ** argv[])
31 if (argc != 0 /* This is a hack! */) {
32 _wildcard(argc, argv);
33 PTIB ptib = new TIB[1];
34 PPIB ppib = new PIB[1];
35 APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
38 char* tmp = new char[256];
39 // This is the only reliable way to retrieve the executable name.
40 rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp);
45 binname_ = OnlyFilename(p);
46 binname_.erase(binname_.length()-4, string::npos);
47 binpath_ = OnlyPath(p);
49 // OS/2 cmd.exe has another use for '&'
50 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
52 // COMSPEC is set, unless user unsets
53 sh = OnlyFilename(GetEnvPath("COMSPEC"));
57 sh = lowercase(sh); // DosMapCase() is an overkill here
58 if (contains(sh, "cmd.exe") || contains(sh, "4os2.exe"))
64 static bool initialized = false;
69 ULONG CPList[3] = {0};
71 APIRET rc = DosQueryCp(3 * sizeof(ULONG), CPList, &CPList_size);
74 // CPList[0] == process current codepage,
75 // CPList[1] == system default codepage, the rest are auxilary.
76 // Once cp_ is correctly set, you can call other routines.
81 void warn(string const & /*mesg*/)
92 rc = DosQueryCurrentDisk(&drv_num, &drv_map);
95 char drive = 'A' + drv_num -1;
102 string::size_type common_path(string const & p1, string const & p2)
104 static bool initialized = false;
112 cntry.codepage = cp_;
113 string temp1 = slashify_path(p1);
114 string temp2 = slashify_path(p2);
115 char * tmp1 = const_cast<char *> (temp1.c_str());
116 char * tmp2 = const_cast<char *> (temp2.c_str());
117 /* rc = */ DosMapCase(p1.length(), &cntry, tmp1);
118 // if (rc != NO_ERROR)
120 /* rc = */ DosMapCase(p2.length(), &cntry, tmp2);
121 // if (rc != NO_ERROR)
123 // This algorithm works only if paths are slashified on DBCS systems.
124 string::size_type i = 0;
125 string::size_type p1_len = p1.length();
126 string::size_type p2_len = p2.length();
127 while (i < p1_len && i < p2_len && tmp1[i] == tmp2[i])
129 if ((i < p1_len && i < p2_len)
130 || (i < p1_len && tmp1[i] != '/' && i == p2_len)
131 || (i < p2_len && tmp2[i] != '/' && i == p1_len))
134 --i; // here was the last match
135 while (i && tmp1[i] != '/')
142 string slashify_path(string const & p)
144 static bool initialized = false;
145 static bool leadbyte[256] = {false};
150 cntry.codepage = cp_;
151 unsigned char *DBCSinfo = new unsigned char[12];
152 /* rc = */ DosQueryDBCSEnv(12, &cntry, (char*) DBCSinfo);
153 // if (rc != NO_ERROR)
155 for (int j = 1; j < 12; j += 2)
158 bool isLeadByte = false;
159 while (*DBCSinfo != 0) {
160 if (i == *DBCSinfo) {
161 isLeadByte = !isLeadByte;
164 leadbyte[i++] = isLeadByte;
168 string::iterator lit = p.begin();
169 string::iterator end = p.end();
170 for (; lit < end; ++lit) {
171 if (leadbyte[(*lit)])
176 p = subst(p, "//", "/");
181 string external_path(string const & p)
187 string internal_path(string const & p)
193 bool is_absolute_path(string const & p)
195 return (p.length() > 1
196 && isalpha(static_cast<unsigned char>(p[0]))
201 // returns a string suitable to be passed to popen when
203 char const * popen_read_mode()
221 void setTmpDir(string const & p)
238 } // end namespace os