Back to index

lightning-sunbird  0.9+nobinonly
hsearch.c
Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993
00003  *     The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Margo Seltzer.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. ***REMOVED*** - see 
00017  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
00018  * 4. Neither the name of the University nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #if defined(LIBC_SCCS) && !defined(lint)
00036 static char sccsid[] = "@(#)hsearch.c     8.4 (Berkeley) 7/21/94";
00037 #endif /* LIBC_SCCS and not lint */
00038 
00039 #include "watcomfx.h"
00040 
00041 #ifndef macintosh
00042 #include <sys/types.h>
00043 #endif
00044 
00045 #include <fcntl.h>
00046 #include <string.h>
00047 
00048 #include "mcom_db.h"
00049 #include "hsearch.h"
00050 
00051 static DB *dbp = NULL;
00052 static ENTRY retval;
00053 
00054 extern int
00055 hcreate(uint nel)
00056 {
00057        HASHINFO info;
00058 
00059        info.nelem = nel;
00060        info.bsize = 256;
00061        info.ffactor = 8;
00062        info.cachesize = 0;
00063        info.hash = NULL;
00064        info.lorder = 0;
00065        dbp = (DB *)__hash_open(NULL, O_CREAT | O_RDWR, 0600, &info, 0);
00066        return ((int)dbp);
00067 }
00068 
00069 extern ENTRY *
00070 hsearch(ENTRY item, ACTION action)
00071 {
00072        DBT key, val;
00073        int status;
00074 
00075        if (!dbp)
00076               return (NULL);
00077        key.data = (uint8 *)item.key;
00078        key.size = strlen(item.key) + 1;
00079 
00080        if (action == ENTER) {
00081               val.data = (uint8 *)item.data;
00082               val.size = strlen(item.data) + 1;
00083               status = (dbp->put)(dbp, &key, &val, R_NOOVERWRITE);
00084               if (status)
00085                      return (NULL);
00086        } else {
00087               /* FIND */
00088               status = (dbp->get)(dbp, &key, &val, 0);
00089               if (status)
00090                      return (NULL);
00091               else
00092                      item.data = (char *)val.data;
00093        }
00094        retval.key = item.key;
00095        retval.data = item.data;
00096        return (&retval);
00097 }
00098 
00099 extern void
00100 hdestroy()
00101 {
00102        if (dbp) {
00103               (void)(dbp->close)(dbp);
00104               dbp = NULL;
00105        }
00106 }