[llvm-commits] CVS: llvm/lib/System/Unix/Path.cpp

Reid Spencer reid at x10sys.com
Tue Nov 16 09:14:19 PST 2004



Changes in directory llvm/lib/System/Unix:

Path.cpp updated: 1.13 -> 1.14
---
Log message:

* Use low-level unix I/O interface since we're on Unix.
* Don't use variable length arrays (replaced with alloca)


---
Diffs of the changes:  (+17 -7)

Index: llvm/lib/System/Unix/Path.cpp
diff -u llvm/lib/System/Unix/Path.cpp:1.13 llvm/lib/System/Unix/Path.cpp:1.14
--- llvm/lib/System/Unix/Path.cpp:1.13	Tue Nov 16 00:15:19 2004
+++ llvm/lib/System/Unix/Path.cpp	Tue Nov 16 11:14:08 2004
@@ -17,6 +17,7 @@
 //===----------------------------------------------------------------------===//
 
 #include <llvm/Config/config.h>
+#include <llvm/Config/alloca.h>
 #include "Unix.h"
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -157,20 +158,29 @@
 
 bool Path::hasMagicNumber(const std::string &Magic) const {
   size_t len = Magic.size();
-  char buf[ 1 + len];
-  std::ifstream f(path.c_str());
-  f.read(buf, len);
+  assert(len < 1024 && "Request for magic string too long");
+  char* buf = (char*) alloca(1 + len);
+  int fd = ::open(path.c_str(),O_RDONLY);
+  if (fd < 0)
+    return false;
+  if (0 != ::read(fd, buf, len))
+    return false;
+  close(fd);
   buf[len] = '\0';
-  f.close();
   return Magic == buf;
 }
 
 bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
   if (!isFile())
     return false;
-  char buf[1 + len];
-  std::ifstream f(path.c_str());
-  f.read(buf,len);
+  assert(len < 1024 && "Request for magic string too long");
+  char* buf = (char*) alloca(1 + len);
+  int fd = ::open(path.c_str(),O_RDONLY);
+  if (fd < 0)
+    return false;
+  if (0 != ::read(fd, buf, len))
+    return false;
+  close(fd);
   buf[len] = '\0';
   Magic = buf;
   return true;






More information about the llvm-commits mailing list