(file) Return to lib.c CVS log (file) (dir) Up to [OMI] / omi / base

  1 mike  1.1 /*
  2           **==============================================================================
  3           **
  4           ** Open Management Infrastructure (OMI)
  5           **
  6           ** Copyright (c) Microsoft Corporation
  7           ** 
  8           ** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
  9           ** use this file except in compliance with the License. You may obtain a copy 
 10           ** of the License at 
 11           **
 12           **     http://www.apache.org/licenses/LICENSE-2.0 
 13           **
 14           ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15           ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
 16           ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
 17           ** MERCHANTABLITY OR NON-INFRINGEMENT. 
 18           **
 19           ** See the Apache 2 License for the specific language governing permissions 
 20           ** and limitations under the License.
 21           **
 22 mike  1.1 **==============================================================================
 23           */
 24           
 25           #include "lib.h"
 26           
 27           /* TODO: enhance error reporting (retrieve system error messages) */
 28           
 29           #if defined(CONFIG_OS_WINDOWS)
 30           # include <Windows.h>
 31           #else
 32           # include <dlfcn.h>
 33           #endif
 34           
 35           #if defined(_MSC_VER)
 36           # pragma warning(disable : 4054)
 37           #endif
 38           
 39           void Lib_Format(
 40               char path[MAX_PATH_SIZE],
 41               const char* dirName, 
 42               const char* shortName)
 43 mike  1.1 {
 44               *path = '\0';
 45           
 46               /* Directory */
 47               if (dirName && dirName[0] != '\0')
 48               {
 49                   Strlcat(path, dirName, MAX_PATH_SIZE);
 50                   Strlcat(path, "/", MAX_PATH_SIZE);
 51               }
 52           
 53               /* Prefix */
 54           #if !defined(CONFIG_OS_WINDOWS)
 55               Strlcat(path, "lib", MAX_PATH_SIZE);
 56           #endif
 57           
 58               /* Library name */
 59               Strlcat(path, shortName, MAX_PATH_SIZE);
 60           
 61               /* Suffix */
 62               Strlcat(path, ".", MAX_PATH_SIZE);
 63               Strlcat(path, CONFIG_SHLIBEXT, MAX_PATH_SIZE);
 64 mike  1.1 }
 65           
 66           void* Lib_Open(const char* path)
 67           {
 68           #if defined(CONFIG_OS_WINDOWS)
 69               return LoadLibraryA(path);
 70           #else
 71               int flags = RTLD_NOW | RTLD_LOCAL;
 72           # ifdef hpux
 73               /* this options helps to resolve name conflict problem on HP */
 74               flags |= RTLD_GROUP;
 75           # endif
 76               return dlopen(path, flags);
 77           #endif
 78           }
 79           
 80           MI_Result Lib_Close(void* handle)
 81           {
 82               if (!handle)
 83                   return MI_RESULT_INVALID_PARAMETER;
 84           
 85 mike  1.1 #if defined(CONFIG_OS_WINDOWS)
 86               if (!FreeLibrary(handle))
 87                   return MI_RESULT_FAILED;
 88           #else
 89               if (dlclose(handle) != 0)
 90                   return MI_RESULT_FAILED;
 91           #endif
 92           
 93               return MI_RESULT_OK;
 94           }
 95           
 96           void* Lib_Sym(void* handle, const char* symbol)
 97           {
 98           #if defined(CONFIG_OS_WINDOWS)
 99               FARPROC result;
100               result = GetProcAddress((HMODULE)handle, symbol);
101               return (void*)result;
102           #else
103               return dlsym(handle, symbol);
104           #endif
105           }
106 mike  1.1 
107           char* Lib_Err()
108           {
109           #if defined(CONFIG_OS_WINDOWS)
110               char* err;
111               FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|
112                   FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 
113                   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&err, 0, NULL);
114               return err;
115           #else
116               return Strdup(dlerror());
117           #endif
118           }
119           
120           void Lib_Free(char* err)
121           {
122           #if defined(CONFIG_OS_WINDOWS)
123               LocalFree(err);
124           #else
125               free(err);
126           #endif
127 mike  1.1 }

ViewCVS 0.9.2