Fix destroyDirectory bug
[oota-llvm.git] / lib / System / Path.cpp
1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file implements the operating system Path concept.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/Path.h"
15 #include "llvm/Config/config.h"
16 #include <cassert>
17
18 namespace llvm {
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //===          independent code. 
24 //===----------------------------------------------------------------------===//
25
26 Path
27 Path::GetLLVMConfigDir() {
28   Path result;
29 #ifdef LLVM_ETCDIR
30   if (result.setDirectory(LLVM_ETCDIR))
31     return result;
32 #endif
33   return GetLLVMDefaultConfigDir();
34 }
35
36 LLVMFileType 
37 sys::IdentifyFileType(const char*magic, unsigned length) {
38   assert(magic && "Invalid magic number string");
39   assert(length >=4 && "Invalid magic number length");
40   switch (magic[0]) {
41     case 'l':
42       if (magic[1] == 'l' && magic[2] == 'v') {
43         if (magic[3] == 'c')
44           return CompressedBytecodeFileType;
45         else if (magic[3] == 'm')
46           return BytecodeFileType;
47       }
48       break;
49
50     case '!':
51       if (length >= 8) {
52         if (memcmp(magic,"!<arch>\n",8) == 0)
53           return ArchiveFileType;
54       }
55       break;
56
57     default:
58       break;
59   }
60   return UnknownFileType;
61 }
62
63 bool
64 Path::isArchive() const {
65   if (readable())
66     return hasMagicNumber("!<arch>\012");
67   return false;
68 }
69
70 bool
71 Path::isDynamicLibrary() const {
72   if (readable()) 
73     return hasMagicNumber("\177ELF");
74   return false;
75 }
76
77 Path
78 Path::FindLibrary(std::string& name) {
79   std::vector<sys::Path> LibPaths;
80   GetSystemLibraryPaths(LibPaths);
81   for (unsigned i = 0; i < LibPaths.size(); ++i) {
82     sys::Path FullPath(LibPaths[i]);
83     FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT);
84     if (FullPath.isDynamicLibrary())
85       return FullPath;
86     FullPath.elideSuffix();
87     FullPath.appendSuffix("a");
88     if (FullPath.isArchive())
89       return FullPath;
90   }
91   return sys::Path();
92 }
93
94 std::string
95 Path::GetDLLSuffix() {
96   return LTDL_SHLIB_EXT;
97 }
98
99 }
100
101 // Include the truly platform-specific parts of this class.
102
103 #if defined(LLVM_ON_UNIX)
104 #include "Unix/Path.inc"
105 #endif
106 #if defined(LLVM_ON_WIN32)
107 #include "Win32/Path.inc"
108 #endif
109
110 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab