<div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 26, 2019, 8:32 PM Vaibhav Gupta <<a href="mailto:vaibhavgupta40@gmail.com">vaibhavgupta40@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">---<br>
newlib/libc/include/ndbm.h | 84 +++++++++++++<br>
newlib/libc/search/Makefile.am | 1 +<br>
newlib/libc/search/ndbm.c | 215 +++++++++++++++++++++++++++++++++<br>
3 files changed, 300 insertions(+)<br>
create mode 100644 newlib/libc/include/ndbm.h<br>
create mode 100644 newlib/libc/search/ndbm.c<br>
<br>
diff --git a/newlib/libc/include/ndbm.h b/newlib/libc/include/ndbm.h<br>
new file mode 100644<br>
index 000000000..3b42c60b0<br>
--- /dev/null<br>
+++ b/newlib/libc/include/ndbm.h<br>
@@ -0,0 +1,84 @@<br>
+/*-<br>
+ * SPDX-License-Identifier: BSD-3-Clause<br>
+ *<br>
+ * Copyright (c) 1990, 1993<br>
+ * The Regents of the University of California. All rights reserved.<br>
+ *<br>
+ * This code is derived from software contributed to Berkeley by<br>
+ * Margo Seltzer.<br>
+ *<br>
+ * Redistribution and use in source and binary forms, with or without<br>
+ * modification, are permitted provided that the following conditions<br>
+ * are met:<br>
+ * 1. Redistributions of source code must retain the above copyright<br>
+ * notice, this list of conditions and the following disclaimer.<br>
+ * 2. Redistributions in binary form must reproduce the above copyright<br>
+ * notice, this list of conditions and the following disclaimer in the<br>
+ * documentation and/or other materials provided with the distribution.<br>
+ * 3. Neither the name of the University nor the names of its contributors<br>
+ * may be used to endorse or promote products derived from this software<br>
+ * without specific prior written permission.<br>
+ *<br>
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND<br>
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE<br>
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL<br>
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS<br>
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br>
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT<br>
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY<br>
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>
+ * SUCH DAMAGE.<br>
+ *<br>
+ * @(#)ndbm.h 8.1 (Berkeley) 6/2/93<br>
+ * $FreeBSD$<br>
+ */<br>
+<br>
+#ifndef _NDBM_H_<br>
+#define _NDBM_H_<br>
+<br>
+#define __DBINTERFACE_PRIVATE<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">If you are using this guard then you need to include db_local.h. </div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+/* Map dbm interface onto db(3). */<br>
+#define DBM_RDONLY O_RDONLY<br>
+<br>
+/* Flags to dbm_store(). */<br>
+#define DBM_INSERT 0<br>
+#define DBM_REPLACE 1<br>
+<br>
+/*<br>
+ * The db(3) support for ndbm always appends this suffix to the<br>
+ * file name to avoid overwriting the user's original database.<br>
+ */<br>
+#define DBM_SUFFIX ".db"<br>
+<br>
+typedef struct {<br>
+ void *dptr;<br>
+ int dsize; /* XXX Should be size_t according to 1003.1-2008. */<br>
+} datum;<br>
+<br>
+struct __db;<br>
+typedef struct __db DB;<br>
+typedef DB DBM;<br>
+#define dbm_pagfno(a) DBM_PAGFNO_NOT_AVAILABLE<br>
+<br>
+__BEGIN_DECLS<br>
+int dbm_clearerr(DBM *);<br>
+void dbm_close(DBM *);<br>
+int dbm_delete(DBM *, datum);<br>
+int dbm_error(DBM *);<br>
+datum dbm_fetch(DBM *, datum);<br>
+datum dbm_firstkey(DBM *);<br>
+#if __BSD_VISIBLE<br>
+long dbm_forder(DBM *, datum);<br>
+#endif<br>
+datum dbm_nextkey(DBM *);<br>
+DBM *dbm_open(const char *, int, mode_t);<br>
+int dbm_store(DBM *, datum, datum, int);<br>
+#if __BSD_VISIBLE<br>
+int dbm_dirfno(DBM *);<br>
+#endif<br>
+__END_DECLS<br>
+<br>
+#endif /* !_NDBM_H_ */<br>
diff --git a/newlib/libc/search/Makefile.am b/newlib/libc/search/Makefile.am<br>
index 98920c3f4..a61107fb9 100644<br>
--- a/newlib/libc/search/Makefile.am<br>
+++ b/newlib/libc/search/Makefile.am<br>
@@ -9,6 +9,7 @@ GENERAL_SOURCES = \<br>
db_local.h \<br>
extern.h \<br>
hash.h \<br>
+ ndbm.c \<br>
page.h \<br>
qsort.c<br>
<br>
diff --git a/newlib/libc/search/ndbm.c b/newlib/libc/search/ndbm.c<br>
new file mode 100644<br>
index 000000000..8ac6d1f40<br>
--- /dev/null<br>
+++ b/newlib/libc/search/ndbm.c<br>
@@ -0,0 +1,215 @@<br>
+/*-<br>
+ * SPDX-License-Identifier: BSD-3-Clause<br>
+ *<br>
+ * Copyright (c) 1990, 1993<br>
+ * The Regents of the University of California. All rights reserved.<br>
+ *<br>
+ * This code is derived from software contributed to Berkeley by<br>
+ * Margo Seltzer.<br>
+ *<br>
+ * Redistribution and use in source and binary forms, with or without<br>
+ * modification, are permitted provided that the following conditions<br>
+ * are met:<br>
+ * 1. Redistributions of source code must retain the above copyright<br>
+ * notice, this list of conditions and the following disclaimer.<br>
+ * 2. Redistributions in binary form must reproduce the above copyright<br>
+ * notice, this list of conditions and the following disclaimer in the<br>
+ * documentation and/or other materials provided with the distribution.<br>
+ * 3. Neither the name of the University nor the names of its contributors<br>
+ * may be used to endorse or promote products derived from this software<br>
+ * without specific prior written permission.<br>
+ *<br>
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND<br>
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE<br>
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL<br>
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS<br>
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br>
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT<br>
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY<br>
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>
+ * SUCH DAMAGE.<br>
+ */<br>
+<br>
+#if defined(LIBC_SCCS) && !defined(lint)<br>
+static char sccsid[] = "@(#)ndbm.c 8.4 (Berkeley) 7/21/94";<br>
+#endif /* LIBC_SCCS and not lint */<br>
+#include <sys/cdefs.h><br>
+__FBSDID("$FreeBSD$");<br>
+</blockquote></div></div><div dir="auto">You can also use svn mirror. It will help newlib maintainer to track history of changes in this file. </div><div dir="auto"><br></div><div dir="auto">For example, See here: <a href="https://opensource.apple.com/source/Libc/Libc-498/db/hash/FreeBSD/ndbm.c.auto.html">https://opensource.apple.com/source/Libc/Libc-498/db/hash/FreeBSD/ndbm.c.auto.html</a></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+/*<br>
+ * This package provides a dbm compatible interface to the new hashing<br>
+ * package described in db(3).<br>
+ */<br>
+<br>
+#include <sys/param.h><br>
+<br>
+#include <stdio.h><br>
+#include <string.h><br>
+#include <errno.h><br>
+<br>
+#include <ndbm.h><br>
+#include "db_local.h"<br>
+#include "hash.h"<br>
+<br>
+/*<br>
+ * Returns:<br>
+ * *DBM on success<br>
+ * NULL on failure<br>
+ */<br>
+extern DBM *<br>
+dbm_open(const char *file, int flags, mode_t mode)<br>
+{<br>
+ HASHINFO info;<br>
+ char path[MAXPATHLEN];<br>
+<br>
+ info.bsize = 4096;<br>
+ info.ffactor = 40;<br>
+ info.nelem = 1;<br>
+ info.cachesize = 0;<br>
+ info.hash = NULL;<br>
+ info.lorder = 0;<br>
+<br>
+ if( strlen(file) >= sizeof(path) - strlen(DBM_SUFFIX)) {<br>
+ errno = ENAMETOOLONG;<br>
+ return(NULL);<br>
+ }<br>
+ (void)strcpy(path, file);<br>
+ (void)strcat(path, DBM_SUFFIX);<br>
+ return ((DBM *)__hash_open(path, flags, mode, 0, &info));<br>
+}<br>
+<br></blockquote></div></div><div dir="auto">Make sure you have changed the order of arguments. </div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+extern void<br>
+dbm_close(DBM *db)<br>
+{<br>
+ (void)(db->close)(db);<br>
+}<br>
+<br>
+/*<br>
+ * Returns:<br>
+ * DATUM on success<br>
+ * NULL on failure<br>
+ */<br>
+extern datum<br>
+dbm_fetch(DBM *db, datum key)<br>
+{<br>
+ datum retdata;<br>
+ int status;<br>
+ DBT dbtkey, dbtretdata;<br>
+<br>
+ dbtkey.data = key.dptr;<br>
+ dbtkey.size = key.dsize;<br>
+ status = (db->get)(db, &dbtkey, &dbtretdata, 0);<br>
+ if (status) {<br>
+ dbtretdata.data = NULL;<br>
+ dbtretdata.size = 0;<br>
+ }<br>
+ retdata.dptr = dbtretdata.data;<br>
+ retdata.dsize = dbtretdata.size;<br>
+ return (retdata);<br>
+}<br>
+<br>
+/*<br>
+ * Returns:<br>
+ * DATUM on success<br>
+ * NULL on failure<br>
+ */<br>
+extern datum<br>
+dbm_firstkey(DBM *db)<br>
+{<br>
+ int status;<br>
+ datum retkey;<br>
+ DBT dbtretkey, dbtretdata;<br>
+<br>
+ status = (db->seq)(db, &dbtretkey, &dbtretdata, R_FIRST);<br>
+ if (status)<br>
+ dbtretkey.data = NULL;<br>
+ retkey.dptr = dbtretkey.data;<br>
+ retkey.dsize = dbtretkey.size;<br>
+ return (retkey);<br>
+}<br>
+<br>
+/*<br>
+ * Returns:<br>
+ * DATUM on success<br>
+ * NULL on failure<br>
+ */<br>
+extern datum<br>
+dbm_nextkey(DBM *db)<br>
+{<br>
+ int status;<br>
+ datum retkey;<br>
+ DBT dbtretkey, dbtretdata;<br>
+<br>
+ status = (db->seq)(db, &dbtretkey, &dbtretdata, R_NEXT);<br>
+ if (status)<br>
+ dbtretkey.data = NULL;<br>
+ retkey.dptr = dbtretkey.data;<br>
+ retkey.dsize = dbtretkey.size;<br>
+ return (retkey);<br>
+}<br>
+<br>
+/*<br>
+ * Returns:<br>
+ * 0 on success<br>
+ * <0 failure<br>
+ */<br>
+extern int<br>
+dbm_delete(DBM *db, datum key)<br>
+{<br>
+ int status;<br>
+ DBT dbtkey;<br>
+<br>
+ dbtkey.data = key.dptr;<br>
+ dbtkey.size = key.dsize;<br>
+ status = (db->del)(db, &dbtkey, 0);<br>
+ if (status)<br>
+ return (-1);<br>
+ else<br>
+ return (0);<br>
+}<br>
+<br>
+/*<br>
+ * Returns:<br>
+ * 0 on success<br>
+ * <0 failure<br>
+ * 1 if DBM_INSERT and entry exists<br>
+ */<br>
+extern int<br>
+dbm_store(DBM *db, datum key, datum data, int flags)<br>
+{<br>
+ DBT dbtkey, dbtdata;<br>
+<br>
+ dbtkey.data = key.dptr;<br>
+ dbtkey.size = key.dsize;<br>
+ dbtdata.data = data.dptr;<br>
+ dbtdata.size = data.dsize;<br>
+ return ((db->put)(db, &dbtkey, &dbtdata,<br>
+ (flags == DBM_INSERT) ? R_NOOVERWRITE : 0));<br>
+}<br>
+<br>
+extern int<br>
+dbm_error(DBM *db)<br>
+{<br>
+ HTAB *hp;<br>
+<br>
+ hp = (HTAB *)db->internal;<br>
+ return (hp->error);<br>
+}<br>
+<br>
+extern int<br>
+dbm_clearerr(DBM *db)<br>
+{<br>
+ HTAB *hp;<br>
+<br>
+ hp = (HTAB *)db->internal;<br>
+ hp->error = 0;<br>
+ return (0);<br>
+}<br>
+<br>
+extern int<br>
+dbm_dirfno(DBM *db)<br>
+{<br>
+ return(((HTAB *)db->internal)->fp);<br>
+}<br>
-- <br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">I would like to suggest you to first send the freebsd original source of ndbm on newlib mailing list and then this patch. </div><div dir="auto"><br></div><div dir="auto">Rest of the things are good. </div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
2.21.0<br>
<br>
</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br></blockquote></div></div></div>