/*
* InspIRCd -- Internet Relay Chat Daemon
*
- * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
- * Copyright (C) 2007 Oliver Lupton <oliverlupton@gmail.com>
+ * Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
+ * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
+ * Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ * Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
+ * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
* Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
* Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
- * Copyright (C) 2003, 2006 Craig Edwards <craigedwards@brainbox.cc>
+ * Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
#include "inspircd.h"
-#include "dynamic.h"
+
#ifndef _WIN32
#include <dlfcn.h>
#else
h = dlopen(fname, RTLD_NOW|RTLD_LOCAL);
if (!h)
{
-#ifdef _WIN32
RetrieveLastError();
-#else
- err = dlerror();
-#endif
}
}
dlclose(h);
}
-union init_t {
- void* vptr;
- Module* (*fptr)();
-};
-
Module* DLLManager::CallInit()
{
- if (!h)
- return NULL;
-
- init_t initfn;
- initfn.vptr = dlsym(h, MODULE_INIT_STR);
- if (!initfn.vptr)
+ union
{
-#ifdef _WIN32
- RetrieveLastError();
-#else
- err = dlerror();
-#endif
+ void* vptr;
+ Module* (*fptr)();
+ };
+
+ vptr = GetSymbol(MODULE_INIT_STR);
+ if (!vptr)
return NULL;
- }
- return (*initfn.fptr)();
+ return (*fptr)();
}
-std::string DLLManager::GetVersion()
+void* DLLManager::GetSymbol(const char* name)
{
- if (!h)
- return "";
+ return h ? dlsym(h, name) : NULL;
+}
- const char* srcver = (char*)dlsym(h, "inspircd_src_version");
- if (srcver)
- return srcver;
- return "Unversioned module";
+std::string DLLManager::GetVersion()
+{
+ const char* srcver = static_cast<const char*>(GetSymbol("inspircd_src_version"));
+ return srcver ? srcver : "";
}
-#ifdef _WIN32
void DLLManager::RetrieveLastError()
{
- CHAR errmsg[100];
- FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), errmsg, 100, 0);
+#if defined _WIN32
+ char errmsg[500];
+ DWORD dwErrorCode = GetLastError();
+ if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0)
+ sprintf_s(errmsg, _countof(errmsg), "Error code: %u", dwErrorCode);
SetLastError(ERROR_SUCCESS);
err = errmsg;
-}
+#else
+ const char* errmsg = dlerror();
+ err = errmsg ? errmsg : "Unknown error";
#endif
+
+ std::string::size_type p;
+ while ((p = err.find_last_of("\r\n")) != std::string::npos)
+ err.erase(p, 1);
+}