mirror of
https://github.com/zeek/zeek.git
synced 2025-10-04 15:48:19 +00:00
Fix build on FreeBSD.
basename(3)/dirname(3) const-ness may vary w/ platform.
This commit is contained in:
parent
574018f478
commit
21df25d429
2 changed files with 60 additions and 32 deletions
58
src/util.cc
58
src/util.cc
|
@ -971,35 +971,59 @@ FILE* open_package(string& path, const string& mode)
|
|||
return 0;
|
||||
}
|
||||
|
||||
SafePathOp::SafePathOp(PathOpFn fn, const char* path, bool error_aborts)
|
||||
void SafePathOp::CheckValid(const char* result, const char* path,
|
||||
bool error_aborts)
|
||||
{
|
||||
DoFunc(fn, path ? path : "", error_aborts);
|
||||
}
|
||||
|
||||
SafePathOp::SafePathOp(PathOpFn fn, const string& path, bool error_aborts)
|
||||
if ( result )
|
||||
{
|
||||
DoFunc(fn, path, error_aborts);
|
||||
}
|
||||
|
||||
void SafePathOp::DoFunc(PathOpFn fn, const string& path, bool error_aborts)
|
||||
{
|
||||
char* tmp = copy_string(path.c_str());
|
||||
char* rval = fn(tmp);
|
||||
|
||||
if ( rval )
|
||||
{
|
||||
result = rval;
|
||||
result = result;
|
||||
error = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( error_aborts )
|
||||
reporter->InternalError("Path operation failed on %s: %s",
|
||||
tmp ? tmp : "<null>", strerror(errno));
|
||||
path ? path : "<null>", strerror(errno));
|
||||
else
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
|
||||
SafeDirname::SafeDirname(const char* path, bool error_aborts)
|
||||
: SafePathOp()
|
||||
{
|
||||
DoFunc(path ? path : "", error_aborts);
|
||||
}
|
||||
|
||||
SafeDirname::SafeDirname(const string& path, bool error_aborts)
|
||||
: SafePathOp()
|
||||
{
|
||||
DoFunc(path, error_aborts);
|
||||
}
|
||||
|
||||
void SafeDirname::DoFunc(const string& path, bool error_aborts)
|
||||
{
|
||||
char* tmp = copy_string(path.c_str());
|
||||
CheckValid(dirname(tmp), tmp, error_aborts);
|
||||
delete [] tmp;
|
||||
}
|
||||
|
||||
SafeBasename::SafeBasename(const char* path, bool error_aborts)
|
||||
: SafePathOp()
|
||||
{
|
||||
DoFunc(path ? path : "", error_aborts);
|
||||
}
|
||||
|
||||
SafeBasename::SafeBasename(const string& path, bool error_aborts)
|
||||
: SafePathOp()
|
||||
{
|
||||
DoFunc(path, error_aborts);
|
||||
}
|
||||
|
||||
void SafeBasename::DoFunc(const string& path, bool error_aborts)
|
||||
{
|
||||
char* tmp = copy_string(path.c_str());
|
||||
CheckValid(basename(tmp), tmp, error_aborts);
|
||||
delete [] tmp;
|
||||
}
|
||||
|
||||
|
|
34
src/util.h
34
src/util.h
|
@ -224,34 +224,38 @@ extern std::string bro_prefixes();
|
|||
class SafePathOp {
|
||||
public:
|
||||
|
||||
typedef char*(*PathOpFn)(char*);
|
||||
|
||||
SafePathOp(PathOpFn fn, const char* path, bool error_aborts = true);
|
||||
SafePathOp(PathOpFn fn, const std::string& path, bool error_aborts = true);
|
||||
|
||||
std::string result;
|
||||
bool error;
|
||||
|
||||
private:
|
||||
protected:
|
||||
|
||||
SafePathOp()
|
||||
{ }
|
||||
|
||||
void CheckValid(const char* result, const char* path, bool error_aborts);
|
||||
|
||||
void DoFunc(PathOpFn fn, const std::string& path, bool error_aborts = true);
|
||||
};
|
||||
|
||||
class SafeDirname : public SafePathOp {
|
||||
public:
|
||||
|
||||
SafeDirname(const char* path, bool error_aborts = true)
|
||||
: SafePathOp(&dirname, path, error_aborts) { }
|
||||
SafeDirname(const std::string& path, bool error_aborts = true)
|
||||
: SafePathOp(&dirname, path, error_aborts) { }
|
||||
SafeDirname(const char* path, bool error_aborts = true);
|
||||
SafeDirname(const std::string& path, bool error_aborts = true);
|
||||
|
||||
private:
|
||||
|
||||
void DoFunc(const std::string& path, bool error_aborts = true);
|
||||
};
|
||||
|
||||
class SafeBasename : public SafePathOp {
|
||||
public:
|
||||
SafeBasename(const char* path, bool error_aborts = true)
|
||||
: SafePathOp(&basename, path, error_aborts) { }
|
||||
SafeBasename(const std::string& path, bool error_aborts = true)
|
||||
: SafePathOp(&basename, path, error_aborts) { }
|
||||
|
||||
SafeBasename(const char* path, bool error_aborts = true);
|
||||
SafeBasename(const std::string& path, bool error_aborts = true);
|
||||
|
||||
private:
|
||||
|
||||
void DoFunc(const std::string& path, bool error_aborts = true);
|
||||
};
|
||||
|
||||
std::string implode_string_vector(const std::vector<std::string>& v,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue