1a6826fbcSWolfgang Denk /* 2a6826fbcSWolfgang Denk * Declarations for System V style searching functions. 3a6826fbcSWolfgang Denk * Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc. 4a6826fbcSWolfgang Denk * This file is part of the GNU C Library. 5a6826fbcSWolfgang Denk * 6a6826fbcSWolfgang Denk * The GNU C Library is free software; you can redistribute it and/or 7a6826fbcSWolfgang Denk * modify it under the terms of the GNU Lesser General Public 8a6826fbcSWolfgang Denk * License as published by the Free Software Foundation; either 9a6826fbcSWolfgang Denk * version 2.1 of the License, or (at your option) any later version. 10a6826fbcSWolfgang Denk * 11a6826fbcSWolfgang Denk * The GNU C Library is distributed in the hope that it will be useful, 12a6826fbcSWolfgang Denk * but WITHOUT ANY WARRANTY; without even the implied warranty of 13a6826fbcSWolfgang Denk * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14a6826fbcSWolfgang Denk * Lesser General Public License for more details. 15a6826fbcSWolfgang Denk * 16a6826fbcSWolfgang Denk * You should have received a copy of the GNU Lesser General Public 17a6826fbcSWolfgang Denk * License along with the GNU C Library; if not, write to the Free 18a6826fbcSWolfgang Denk * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 19a6826fbcSWolfgang Denk * 02111-1307 USA. 20a6826fbcSWolfgang Denk */ 21a6826fbcSWolfgang Denk 22a6826fbcSWolfgang Denk /* 23a6826fbcSWolfgang Denk * Based on code from uClibc-0.9.30.3 24a6826fbcSWolfgang Denk * Extensions for use within U-Boot 25a6826fbcSWolfgang Denk * Copyright (C) 2010 Wolfgang Denk <wd@denx.de> 26a6826fbcSWolfgang Denk */ 27a6826fbcSWolfgang Denk 28a6826fbcSWolfgang Denk #ifndef _SEARCH_H 29a6826fbcSWolfgang Denk #define _SEARCH_H 1 30a6826fbcSWolfgang Denk 31a6826fbcSWolfgang Denk #include <stddef.h> 32a6826fbcSWolfgang Denk 33a6826fbcSWolfgang Denk #define __set_errno(val) do { errno = val; } while (0) 34a6826fbcSWolfgang Denk 35a6826fbcSWolfgang Denk /* Action which shall be performed in the call the hsearch. */ 36a6826fbcSWolfgang Denk typedef enum { 37a6826fbcSWolfgang Denk FIND, 38a6826fbcSWolfgang Denk ENTER 39a6826fbcSWolfgang Denk } ACTION; 40a6826fbcSWolfgang Denk 41a6826fbcSWolfgang Denk typedef struct entry { 4284b5e802SWolfgang Denk const char *key; 43a6826fbcSWolfgang Denk char *data; 44a6826fbcSWolfgang Denk } ENTRY; 45a6826fbcSWolfgang Denk 46a6826fbcSWolfgang Denk /* Opaque type for internal use. */ 47a6826fbcSWolfgang Denk struct _ENTRY; 48a6826fbcSWolfgang Denk 49a6826fbcSWolfgang Denk /* 50a6826fbcSWolfgang Denk * Family of hash table handling functions. The functions also 51a6826fbcSWolfgang Denk * have reentrant counterparts ending with _r. The non-reentrant 52a6826fbcSWolfgang Denk * functions all work on a signle internal hashing table. 53a6826fbcSWolfgang Denk */ 54a6826fbcSWolfgang Denk 55a6826fbcSWolfgang Denk /* Data type for reentrant functions. */ 56a6826fbcSWolfgang Denk struct hsearch_data { 57a6826fbcSWolfgang Denk struct _ENTRY *table; 58a6826fbcSWolfgang Denk unsigned int size; 59a6826fbcSWolfgang Denk unsigned int filled; 60c5983592SGerlando Falauto /* 61c5983592SGerlando Falauto * Callback function which will check whether the given change for variable 62c5983592SGerlando Falauto * "name" from "oldval" to "newval" may be applied or not, and possibly apply 63c5983592SGerlando Falauto * such change. 64c5983592SGerlando Falauto * When (flag & H_FORCE) is set, it shall not print out any error message and 65c5983592SGerlando Falauto * shall force overwriting of write-once variables. 66c5983592SGerlando Falauto .* Must return 0 for approval, 1 for denial. 67c5983592SGerlando Falauto */ 68c5983592SGerlando Falauto int (*apply)(const char *name, const char *oldval, 69c5983592SGerlando Falauto const char *newval, int flag); 70a6826fbcSWolfgang Denk }; 71a6826fbcSWolfgang Denk 72a6826fbcSWolfgang Denk /* Create a new hashing table which will at most contain NEL elements. */ 73a6826fbcSWolfgang Denk extern int hcreate_r(size_t __nel, struct hsearch_data *__htab); 74a6826fbcSWolfgang Denk 75a6826fbcSWolfgang Denk /* Destroy current internal hashing table. */ 76*c4e0057fSJoe Hershberger extern void hdestroy_r(struct hsearch_data *__htab); 77a6826fbcSWolfgang Denk 78a6826fbcSWolfgang Denk /* 79a6826fbcSWolfgang Denk * Search for entry matching ITEM.key in internal hash table. If 80a6826fbcSWolfgang Denk * ACTION is `FIND' return found entry or signal error by returning 81a6826fbcSWolfgang Denk * NULL. If ACTION is `ENTER' replace existing data (if any) with 82a6826fbcSWolfgang Denk * ITEM.data. 83a6826fbcSWolfgang Denk * */ 84a6826fbcSWolfgang Denk extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval, 85*c4e0057fSJoe Hershberger struct hsearch_data *__htab, int __flag); 86a6826fbcSWolfgang Denk 87560d424bSMike Frysinger /* 88560d424bSMike Frysinger * Search for an entry matching `MATCH'. Otherwise, Same semantics 89560d424bSMike Frysinger * as hsearch_r(). 90560d424bSMike Frysinger */ 91560d424bSMike Frysinger extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval, 92560d424bSMike Frysinger struct hsearch_data *__htab); 93a000b795SKim Phillips /* 94a000b795SKim Phillips * Search for an entry whose key or data contains `MATCH'. Otherwise, 95a000b795SKim Phillips * Same semantics as hsearch_r(). 96a000b795SKim Phillips */ 97a000b795SKim Phillips extern int hstrstr_r(const char *__match, int __last_idx, ENTRY ** __retval, 98a000b795SKim Phillips struct hsearch_data *__htab); 99560d424bSMike Frysinger 100a6826fbcSWolfgang Denk /* Search and delete entry matching ITEM.key in internal hash table. */ 101152874b6SGerlando Falauto extern int hdelete_r(const char *__key, struct hsearch_data *__htab, 102*c4e0057fSJoe Hershberger int __flag); 103a6826fbcSWolfgang Denk 104a6826fbcSWolfgang Denk extern ssize_t hexport_r(struct hsearch_data *__htab, 10537f2fe74SWolfgang Denk const char __sep, char **__resp, size_t __size, 10637f2fe74SWolfgang Denk int argc, char * const argv[]); 107a6826fbcSWolfgang Denk 108348b1f1cSGerlando Falauto /* 109348b1f1cSGerlando Falauto * nvars: length of vars array 110348b1f1cSGerlando Falauto * vars: array of strings (variable names) to import (nvars == 0 means all) 111c5983592SGerlando Falauto * do_apply: whether to call callback function to check the new argument, 112c5983592SGerlando Falauto * and possibly apply changes (false means accept everything) 113348b1f1cSGerlando Falauto */ 114a6826fbcSWolfgang Denk extern int himport_r(struct hsearch_data *__htab, 115a6826fbcSWolfgang Denk const char *__env, size_t __size, const char __sep, 116*c4e0057fSJoe Hershberger int __flag, int nvars, char * const vars[]); 117a6826fbcSWolfgang Denk 118*c4e0057fSJoe Hershberger /* Flags for himport_r(), hdelete_r(), and hsearch_r() */ 119c3f65258SGerlando Falauto #define H_NOCLEAR (1 << 0) /* do not clear hash table before importing */ 120c3f65258SGerlando Falauto #define H_FORCE (1 << 1) /* overwrite read-only/write-once variables */ 121*c4e0057fSJoe Hershberger #define H_INTERACTIVE (1 << 2) /* indicate that an import is user directed */ 122a6826fbcSWolfgang Denk 123a6826fbcSWolfgang Denk #endif /* search.h */ 124