From be31d2ad781efa4e1d134ef18ef1ec5d9848572f Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Tue, 16 Nov 2004 17:14:08 +0000 Subject: * Use low-level unix I/O interface since we're on Unix. * Don't use variable length arrays (replaced with alloca) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17901 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/System/Unix/Path.cpp | 24 +++++++++++++++++------- lib/System/Unix/Path.inc | 24 +++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/System/Unix/Path.cpp b/lib/System/Unix/Path.cpp index 55ef666..70cc4f0 100644 --- a/lib/System/Unix/Path.cpp +++ b/lib/System/Unix/Path.cpp @@ -17,6 +17,7 @@ //===----------------------------------------------------------------------===// #include +#include #include "Unix.h" #include #include @@ -157,20 +158,29 @@ Path::getBasename() const { 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; diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc index 55ef666..70cc4f0 100644 --- a/lib/System/Unix/Path.inc +++ b/lib/System/Unix/Path.inc @@ -17,6 +17,7 @@ //===----------------------------------------------------------------------===// #include +#include #include "Unix.h" #include #include @@ -157,20 +158,29 @@ Path::getBasename() const { 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; -- cgit v1.1