1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system Path concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/Path.h"
15 #include "llvm/Config/config.h"
22 //===----------------------------------------------------------------------===//
23 //=== WARNING: Implementation here must contain only TRULY operating system
24 //=== independent code.
25 //===----------------------------------------------------------------------===//
27 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
28 strm << aPath.toString();
33 Path::GetLLVMConfigDir() {
36 if (result.set(LLVM_ETCDIR))
39 return GetLLVMDefaultConfigDir();
43 sys::IdentifyFileType(const char*magic, unsigned length) {
44 assert(magic && "Invalid magic number string");
45 assert(length >=4 && "Invalid magic number length");
48 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
49 return Bitcode_FileType;
53 if (memcmp(magic,"!<arch>\n",8) == 0)
54 return Archive_FileType;
58 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
59 if (length >= 18 && magic[17] == 0)
62 case 1: return ELF_Relocatable_FileType;
63 case 2: return ELF_Executable_FileType;
64 case 3: return ELF_SharedObject_FileType;
65 case 4: return ELF_Core_FileType;
71 // This is complicated by an overlap with Java class files.
72 // See the Mach-O section in /usr/share/file/magic for details.
73 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
74 magic[3] == char(0xBE)) {
75 return Mach_O_DynamicallyLinkedSharedLib_FileType;
77 // FIXME: How does this work?
78 if (length >= 14 && magic[13] == 0)
81 case 1: return Mach_O_Object_FileType;
82 case 2: return Mach_O_Executable_FileType;
83 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
84 case 4: return Mach_O_Core_FileType;
85 case 5: return Mach_O_PreloadExectuable_FileType;
86 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
87 case 7: return Mach_O_DynamicLinker_FileType;
88 case 8: return Mach_O_Bundle_FileType;
89 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
94 case 0xF0: // PowerPC Windows
95 case 0x83: // Alpha 32-bit
96 case 0x84: // Alpha 64-bit
97 case 0x66: // MPS R4000 Windows
99 case 0x4c: // 80386 Windows
100 if (magic[1] == 0x01)
101 return COFF_FileType;
103 case 0x90: // PA-RISC Windows
104 case 0x68: // mc68K Windows
105 if (magic[1] == 0x02)
106 return COFF_FileType;
112 return Unknown_FileType;
116 Path::isArchive() const {
118 return hasMagicNumber("!<arch>\012");
123 Path::isDynamicLibrary() const {
126 if (getMagicNumber(Magic, 64))
127 switch (IdentifyFileType(Magic.c_str(),
128 static_cast<unsigned>(Magic.length()))) {
129 default: return false;
130 case Mach_O_FixedVirtualMemorySharedLib_FileType:
131 case Mach_O_DynamicallyLinkedSharedLib_FileType:
132 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
133 case ELF_SharedObject_FileType:
134 case COFF_FileType: return true;
141 Path::FindLibrary(std::string& name) {
142 std::vector<sys::Path> LibPaths;
143 GetSystemLibraryPaths(LibPaths);
144 for (unsigned i = 0; i < LibPaths.size(); ++i) {
145 sys::Path FullPath(LibPaths[i]);
146 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
147 if (FullPath.isDynamicLibrary())
149 FullPath.eraseSuffix();
150 FullPath.appendSuffix("a");
151 if (FullPath.isArchive())
157 std::string Path::GetDLLSuffix() {
158 return LTDL_SHLIB_EXT;
162 Path::isBitcodeFile() const {
163 std::string actualMagic;
164 if (!getMagicNumber(actualMagic, 4))
166 return actualMagic == "BC\xC0\xDE";
169 bool Path::hasMagicNumber(const std::string &Magic) const {
170 std::string actualMagic;
171 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
172 return Magic == actualMagic;
177 Path::getSuffix() const {
178 return path.substr(path.rfind('.') + 1);
181 static void getPathList(const char*path, std::vector<Path>& Paths) {
182 const char* at = path;
183 const char* delim = strchr(at, PathSeparator);
186 std::string tmp(at, size_t(delim-at));
187 if (tmpPath.set(tmp))
188 if (tmpPath.canRead())
189 Paths.push_back(tmpPath);
191 delim = strchr(at, PathSeparator);
195 if (tmpPath.set(std::string(at)))
196 if (tmpPath.canRead())
197 Paths.push_back(tmpPath);
200 static std::string getDirnameCharSep(const std::string& path, char Sep) {
205 // If the path is all slashes, return a single slash.
206 // Otherwise, remove all trailing slashes.
208 signed pos = static_cast<signed>(path.size()) - 1;
210 while (pos >= 0 && path[pos] == Sep)
214 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
219 while (i < pos && path[i] != Sep)
222 if (i == pos) // No slashes? Return "."
225 // There is at least one slash left. Remove all trailing non-slashes.
226 while (pos >= 0 && path[pos] != Sep)
229 // Remove any trailing slashes.
230 while (pos >= 0 && path[pos] == Sep)
234 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
236 return path.substr(0, pos+1);
239 // Include the truly platform-specific parts of this class.
240 #if defined(LLVM_ON_UNIX)
241 #include "Unix/Path.inc"
243 #if defined(LLVM_ON_WIN32)
244 #include "Win32/Path.inc"