Back to index

lightning-sunbird  0.9+nobinonly
libfilename.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 **
00040 ** Name: libfilename.c
00041 **
00042 ** Description: test PR_GetLibraryFilePathname.
00043 **
00044 ***********************************************************************/
00045 
00046 #include "nspr.h"
00047 #include "pprio.h"
00048 #include <stdio.h>
00049 #include <stdlib.h>
00050 #include <string.h>
00051 
00052 PRBool debug_mode = PR_FALSE;
00053 
00054 static PRStatus RunTest(const char *name, PRFuncPtr addr)
00055 {
00056     char *pathname;
00057     PRFileDesc *fd;
00058 
00059     pathname = PR_GetLibraryFilePathname(name, addr);
00060     if (pathname == NULL) {
00061         fprintf(stderr, "PR_GetLibraryFilePathname failed\n");
00062         /* we let this test pass if this function is not implemented */
00063         if (PR_GetError() == PR_NOT_IMPLEMENTED_ERROR) {
00064             return PR_SUCCESS;
00065         }
00066         return PR_FAILURE;
00067     }
00068 
00069     if (debug_mode) printf("Pathname is %s\n", pathname);
00070     fd = PR_OpenFile(pathname, PR_RDONLY, 0);
00071     if (fd == NULL) {
00072         fprintf(stderr, "PR_Open failed: %d\n", (int)PR_GetError());
00073         return PR_FAILURE;
00074     }
00075     if (PR_Close(fd) == PR_FAILURE) {
00076         fprintf(stderr, "PR_Close failed: %d\n", (int)PR_GetError());
00077         return PR_FAILURE;
00078     }
00079     PR_Free(pathname);
00080     return PR_SUCCESS;
00081 }
00082 
00083 int main(int argc, char *argv[])
00084 {
00085     char *name;
00086     PRFuncPtr addr;
00087     PRLibrary *lib;
00088     PRBool failed = PR_FALSE;
00089 
00090     if (argc >= 2 && strcmp(argv[1], "-d") == 0) {
00091         debug_mode = PR_TRUE;
00092     }
00093 
00094     /* First test a library that is implicitly linked. */
00095 #ifdef WINNT
00096     name = PR_Malloc(strlen("libnspr4.dll")+1);
00097     strcpy(name, "libnspr4.dll");
00098 #else
00099     name = PR_GetLibraryName(NULL, "nspr4");
00100 #endif
00101     addr = (PRFuncPtr)PR_GetTCPMethods()->close;
00102     if (RunTest(name, addr) == PR_FAILURE) {
00103         failed = PR_TRUE;
00104     }
00105     PR_FreeLibraryName(name);
00106 
00107     /* Next test a library that is dynamically loaded. */
00108     name = PR_GetLibraryName("dll", "my");
00109     if (debug_mode) printf("Loading library %s\n", name);
00110     lib = PR_LoadLibrary(name);
00111     if (!lib) {
00112         fprintf(stderr, "PR_LoadLibrary failed\n");
00113         exit(1);
00114     }
00115     PR_FreeLibraryName(name);
00116     name = PR_GetLibraryName(NULL, "my");
00117     addr = PR_FindFunctionSymbol(lib, "My_GetValue");
00118     if (RunTest(name, addr) == PR_FAILURE) {
00119         failed = PR_TRUE;
00120     }
00121     PR_FreeLibraryName(name);
00122     PR_UnloadLibrary(lib);
00123     if (failed) {
00124         printf("FAIL\n");
00125         return 1;
00126     }
00127     printf("PASS\n");
00128     return 0;
00129 }