hashtable.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * This implementation is based on code from uClibc-0.9.30.3 but was
  3. * modified and extended for use within U-Boot.
  4. *
  5. * Copyright (C) 2010 Wolfgang Denk <wd@denx.de>
  6. *
  7. * Original license header:
  8. *
  9. * Copyright (C) 1993, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
  10. * This file is part of the GNU C Library.
  11. * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993.
  12. *
  13. * The GNU C Library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * The GNU C Library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with the GNU C Library; if not, write to the Free
  25. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  26. * 02111-1307 USA.
  27. */
  28. #include <errno.h>
  29. #include <malloc.h>
  30. #ifdef USE_HOSTCC /* HOST build */
  31. # include <string.h>
  32. # include <assert.h>
  33. # ifndef debug
  34. # ifdef DEBUG
  35. # define debug(fmt,args...) printf(fmt ,##args)
  36. # else
  37. # define debug(fmt,args...)
  38. # endif
  39. # endif
  40. #else /* U-Boot build */
  41. # include <common.h>
  42. # include <linux/string.h>
  43. #endif
  44. #ifndef CONFIG_ENV_MIN_ENTRIES /* minimum number of entries */
  45. #define CONFIG_ENV_MIN_ENTRIES 64
  46. #endif
  47. #ifndef CONFIG_ENV_MAX_ENTRIES /* maximum number of entries */
  48. #define CONFIG_ENV_MAX_ENTRIES 512
  49. #endif
  50. #include "search.h"
  51. /*
  52. * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
  53. * [Knuth] The Art of Computer Programming, part 3 (6.4)
  54. */
  55. /*
  56. * The reentrant version has no static variables to maintain the state.
  57. * Instead the interface of all functions is extended to take an argument
  58. * which describes the current status.
  59. */
  60. typedef struct _ENTRY {
  61. int used;
  62. ENTRY entry;
  63. } _ENTRY;
  64. /*
  65. * hcreate()
  66. */
  67. /*
  68. * For the used double hash method the table size has to be a prime. To
  69. * correct the user given table size we need a prime test. This trivial
  70. * algorithm is adequate because
  71. * a) the code is (most probably) called a few times per program run and
  72. * b) the number is small because the table must fit in the core
  73. * */
  74. static int isprime(unsigned int number)
  75. {
  76. /* no even number will be passed */
  77. unsigned int div = 3;
  78. while (div * div < number && number % div != 0)
  79. div += 2;
  80. return number % div != 0;
  81. }
  82. /*
  83. * Before using the hash table we must allocate memory for it.
  84. * Test for an existing table are done. We allocate one element
  85. * more as the found prime number says. This is done for more effective
  86. * indexing as explained in the comment for the hsearch function.
  87. * The contents of the table is zeroed, especially the field used
  88. * becomes zero.
  89. */
  90. int hcreate_r(size_t nel, struct hsearch_data *htab)
  91. {
  92. /* Test for correct arguments. */
  93. if (htab == NULL) {
  94. __set_errno(EINVAL);
  95. return 0;
  96. }
  97. /* There is still another table active. Return with error. */
  98. if (htab->table != NULL)
  99. return 0;
  100. /* Change nel to the first prime number not smaller as nel. */
  101. nel |= 1; /* make odd */
  102. while (!isprime(nel))
  103. nel += 2;
  104. htab->size = nel;
  105. htab->filled = 0;
  106. /* allocate memory and zero out */
  107. htab->table = (_ENTRY *) calloc(htab->size + 1, sizeof(_ENTRY));
  108. if (htab->table == NULL)
  109. return 0;
  110. /* everything went alright */
  111. return 1;
  112. }
  113. /*
  114. * hdestroy()
  115. */
  116. /*
  117. * After using the hash table it has to be destroyed. The used memory can
  118. * be freed and the local static variable can be marked as not used.
  119. */
  120. void hdestroy_r(struct hsearch_data *htab)
  121. {
  122. int i;
  123. /* Test for correct arguments. */
  124. if (htab == NULL) {
  125. __set_errno(EINVAL);
  126. return;
  127. }
  128. /* free used memory */
  129. for (i = 1; i <= htab->size; ++i) {
  130. if (htab->table[i].used > 0) {
  131. ENTRY *ep = &htab->table[i].entry;
  132. free((void *)ep->key);
  133. free(ep->data);
  134. }
  135. }
  136. free(htab->table);
  137. /* the sign for an existing table is an value != NULL in htable */
  138. htab->table = NULL;
  139. }
  140. /*
  141. * hsearch()
  142. */
  143. /*
  144. * This is the search function. It uses double hashing with open addressing.
  145. * The argument item.key has to be a pointer to an zero terminated, most
  146. * probably strings of chars. The function for generating a number of the
  147. * strings is simple but fast. It can be replaced by a more complex function
  148. * like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
  149. *
  150. * We use an trick to speed up the lookup. The table is created by hcreate
  151. * with one more element available. This enables us to use the index zero
  152. * special. This index will never be used because we store the first hash
  153. * index in the field used where zero means not used. Every other value
  154. * means used. The used field can be used as a first fast comparison for
  155. * equality of the stored and the parameter value. This helps to prevent
  156. * unnecessary expensive calls of strcmp.
  157. *
  158. * This implementation differs from the standard library version of
  159. * this function in a number of ways:
  160. *
  161. * - While the standard version does not make any assumptions about
  162. * the type of the stored data objects at all, this implementation
  163. * works with NUL terminated strings only.
  164. * - Instead of storing just pointers to the original objects, we
  165. * create local copies so the caller does not need to care about the
  166. * data any more.
  167. * - The standard implementation does not provide a way to update an
  168. * existing entry. This version will create a new entry or update an
  169. * existing one when both "action == ENTER" and "item.data != NULL".
  170. * - Instead of returning 1 on success, we return the index into the
  171. * internal hash table, which is also guaranteed to be positive.
  172. * This allows us direct access to the found hash table slot for
  173. * example for functions like hdelete().
  174. */
  175. /*
  176. * hstrstr_r - return index to entry whose key and/or data contains match
  177. */
  178. int hstrstr_r(const char *match, int last_idx, ENTRY ** retval,
  179. struct hsearch_data *htab)
  180. {
  181. unsigned int idx;
  182. for (idx = last_idx + 1; idx < htab->size; ++idx) {
  183. if (htab->table[idx].used <= 0)
  184. continue;
  185. if (strstr(htab->table[idx].entry.key, match) ||
  186. strstr(htab->table[idx].entry.data, match)) {
  187. *retval = &htab->table[idx].entry;
  188. return idx;
  189. }
  190. }
  191. __set_errno(ESRCH);
  192. *retval = NULL;
  193. return 0;
  194. }
  195. int hmatch_r(const char *match, int last_idx, ENTRY ** retval,
  196. struct hsearch_data *htab)
  197. {
  198. unsigned int idx;
  199. size_t key_len = strlen(match);
  200. for (idx = last_idx + 1; idx < htab->size; ++idx) {
  201. if (htab->table[idx].used <= 0)
  202. continue;
  203. if (!strncmp(match, htab->table[idx].entry.key, key_len)) {
  204. *retval = &htab->table[idx].entry;
  205. return idx;
  206. }
  207. }
  208. __set_errno(ESRCH);
  209. *retval = NULL;
  210. return 0;
  211. }
  212. int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval,
  213. struct hsearch_data *htab)
  214. {
  215. unsigned int hval;
  216. unsigned int count;
  217. unsigned int len = strlen(item.key);
  218. unsigned int idx;
  219. unsigned int first_deleted = 0;
  220. /* Compute an value for the given string. Perhaps use a better method. */
  221. hval = len;
  222. count = len;
  223. while (count-- > 0) {
  224. hval <<= 4;
  225. hval += item.key[count];
  226. }
  227. /*
  228. * First hash function:
  229. * simply take the modul but prevent zero.
  230. */
  231. hval %= htab->size;
  232. if (hval == 0)
  233. ++hval;
  234. /* The first index tried. */
  235. idx = hval;
  236. if (htab->table[idx].used) {
  237. /*
  238. * Further action might be required according to the
  239. * action value.
  240. */
  241. unsigned hval2;
  242. if (htab->table[idx].used == -1
  243. && !first_deleted)
  244. first_deleted = idx;
  245. if (htab->table[idx].used == hval
  246. && strcmp(item.key, htab->table[idx].entry.key) == 0) {
  247. /* Overwrite existing value? */
  248. if ((action == ENTER) && (item.data != NULL)) {
  249. free(htab->table[idx].entry.data);
  250. htab->table[idx].entry.data =
  251. strdup(item.data);
  252. if (!htab->table[idx].entry.data) {
  253. __set_errno(ENOMEM);
  254. *retval = NULL;
  255. return 0;
  256. }
  257. }
  258. /* return found entry */
  259. *retval = &htab->table[idx].entry;
  260. return idx;
  261. }
  262. /*
  263. * Second hash function:
  264. * as suggested in [Knuth]
  265. */
  266. hval2 = 1 + hval % (htab->size - 2);
  267. do {
  268. /*
  269. * Because SIZE is prime this guarantees to
  270. * step through all available indices.
  271. */
  272. if (idx <= hval2)
  273. idx = htab->size + idx - hval2;
  274. else
  275. idx -= hval2;
  276. /*
  277. * If we visited all entries leave the loop
  278. * unsuccessfully.
  279. */
  280. if (idx == hval)
  281. break;
  282. /* If entry is found use it. */
  283. if ((htab->table[idx].used == hval)
  284. && strcmp(item.key, htab->table[idx].entry.key) == 0) {
  285. /* Overwrite existing value? */
  286. if ((action == ENTER) && (item.data != NULL)) {
  287. free(htab->table[idx].entry.data);
  288. htab->table[idx].entry.data =
  289. strdup(item.data);
  290. if (!htab->table[idx].entry.data) {
  291. __set_errno(ENOMEM);
  292. *retval = NULL;
  293. return 0;
  294. }
  295. }
  296. /* return found entry */
  297. *retval = &htab->table[idx].entry;
  298. return idx;
  299. }
  300. }
  301. while (htab->table[idx].used);
  302. }
  303. /* An empty bucket has been found. */
  304. if (action == ENTER) {
  305. /*
  306. * If table is full and another entry should be
  307. * entered return with error.
  308. */
  309. if (htab->filled == htab->size) {
  310. __set_errno(ENOMEM);
  311. *retval = NULL;
  312. return 0;
  313. }
  314. /*
  315. * Create new entry;
  316. * create copies of item.key and item.data
  317. */
  318. if (first_deleted)
  319. idx = first_deleted;
  320. htab->table[idx].used = hval;
  321. htab->table[idx].entry.key = strdup(item.key);
  322. htab->table[idx].entry.data = strdup(item.data);
  323. if (!htab->table[idx].entry.key ||
  324. !htab->table[idx].entry.data) {
  325. __set_errno(ENOMEM);
  326. *retval = NULL;
  327. return 0;
  328. }
  329. ++htab->filled;
  330. /* return new entry */
  331. *retval = &htab->table[idx].entry;
  332. return 1;
  333. }
  334. __set_errno(ESRCH);
  335. *retval = NULL;
  336. return 0;
  337. }
  338. /*
  339. * hdelete()
  340. */
  341. /*
  342. * The standard implementation of hsearch(3) does not provide any way
  343. * to delete any entries from the hash table. We extend the code to
  344. * do that.
  345. */
  346. int hdelete_r(const char *key, struct hsearch_data *htab)
  347. {
  348. ENTRY e, *ep;
  349. int idx;
  350. debug("hdelete: DELETE key \"%s\"\n", key);
  351. e.key = (char *)key;
  352. if ((idx = hsearch_r(e, FIND, &ep, htab)) == 0) {
  353. __set_errno(ESRCH);
  354. return 0; /* not found */
  355. }
  356. /* free used ENTRY */
  357. debug("hdelete: DELETING key \"%s\"\n", key);
  358. free((void *)ep->key);
  359. free(ep->data);
  360. htab->table[idx].used = -1;
  361. --htab->filled;
  362. return 1;
  363. }
  364. /*
  365. * hexport()
  366. */
  367. /*
  368. * Export the data stored in the hash table in linearized form.
  369. *
  370. * Entries are exported as "name=value" strings, separated by an
  371. * arbitrary (non-NUL, of course) separator character. This allows to
  372. * use this function both when formatting the U-Boot environment for
  373. * external storage (using '\0' as separator), but also when using it
  374. * for the "printenv" command to print all variables, simply by using
  375. * as '\n" as separator. This can also be used for new features like
  376. * exporting the environment data as text file, including the option
  377. * for later re-import.
  378. *
  379. * The entries in the result list will be sorted by ascending key
  380. * values.
  381. *
  382. * If the separator character is different from NUL, then any
  383. * separator characters and backslash characters in the values will
  384. * be escaped by a preceeding backslash in output. This is needed for
  385. * example to enable multi-line values, especially when the output
  386. * shall later be parsed (for example, for re-import).
  387. *
  388. * There are several options how the result buffer is handled:
  389. *
  390. * *resp size
  391. * -----------
  392. * NULL 0 A string of sufficient length will be allocated.
  393. * NULL >0 A string of the size given will be
  394. * allocated. An error will be returned if the size is
  395. * not sufficient. Any unused bytes in the string will
  396. * be '\0'-padded.
  397. * !NULL 0 The user-supplied buffer will be used. No length
  398. * checking will be performed, i. e. it is assumed that
  399. * the buffer size will always be big enough. DANGEROUS.
  400. * !NULL >0 The user-supplied buffer will be used. An error will
  401. * be returned if the size is not sufficient. Any unused
  402. * bytes in the string will be '\0'-padded.
  403. */
  404. static int cmpkey(const void *p1, const void *p2)
  405. {
  406. ENTRY *e1 = *(ENTRY **) p1;
  407. ENTRY *e2 = *(ENTRY **) p2;
  408. return (strcmp(e1->key, e2->key));
  409. }
  410. ssize_t hexport_r(struct hsearch_data *htab, const char sep,
  411. char **resp, size_t size)
  412. {
  413. ENTRY *list[htab->size];
  414. char *res, *p;
  415. size_t totlen;
  416. int i, n;
  417. /* Test for correct arguments. */
  418. if ((resp == NULL) || (htab == NULL)) {
  419. __set_errno(EINVAL);
  420. return (-1);
  421. }
  422. debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, size = %d\n",
  423. htab, htab->size, htab->filled, size);
  424. /*
  425. * Pass 1:
  426. * search used entries,
  427. * save addresses and compute total length
  428. */
  429. for (i = 1, n = 0, totlen = 0; i <= htab->size; ++i) {
  430. if (htab->table[i].used > 0) {
  431. ENTRY *ep = &htab->table[i].entry;
  432. list[n++] = ep;
  433. totlen += strlen(ep->key) + 2;
  434. if (sep == '\0') {
  435. totlen += strlen(ep->data);
  436. } else { /* check if escapes are needed */
  437. char *s = ep->data;
  438. while (*s) {
  439. ++totlen;
  440. /* add room for needed escape chars */
  441. if ((*s == sep) || (*s == '\\'))
  442. ++totlen;
  443. ++s;
  444. }
  445. }
  446. totlen += 2; /* for '=' and 'sep' char */
  447. }
  448. }
  449. #ifdef DEBUG
  450. /* Pass 1a: print unsorted list */
  451. printf("Unsorted: n=%d\n", n);
  452. for (i = 0; i < n; ++i) {
  453. printf("\t%3d: %p ==> %-10s => %s\n",
  454. i, list[i], list[i]->key, list[i]->data);
  455. }
  456. #endif
  457. /* Sort list by keys */
  458. qsort(list, n, sizeof(ENTRY *), cmpkey);
  459. /* Check if the user supplied buffer size is sufficient */
  460. if (size) {
  461. if (size < totlen + 1) { /* provided buffer too small */
  462. debug("### buffer too small: %d, but need %d\n",
  463. size, totlen + 1);
  464. __set_errno(ENOMEM);
  465. return (-1);
  466. }
  467. } else {
  468. size = totlen + 1;
  469. }
  470. /* Check if the user provided a buffer */
  471. if (*resp) {
  472. /* yes; clear it */
  473. res = *resp;
  474. memset(res, '\0', size);
  475. } else {
  476. /* no, allocate and clear one */
  477. *resp = res = calloc(1, size);
  478. if (res == NULL) {
  479. __set_errno(ENOMEM);
  480. return (-1);
  481. }
  482. }
  483. /*
  484. * Pass 2:
  485. * export sorted list of result data
  486. */
  487. for (i = 0, p = res; i < n; ++i) {
  488. const char *s;
  489. s = list[i]->key;
  490. while (*s)
  491. *p++ = *s++;
  492. *p++ = '=';
  493. s = list[i]->data;
  494. while (*s) {
  495. if ((*s == sep) || (*s == '\\'))
  496. *p++ = '\\'; /* escape */
  497. *p++ = *s++;
  498. }
  499. *p++ = sep;
  500. }
  501. *p = '\0'; /* terminate result */
  502. return size;
  503. }
  504. /*
  505. * himport()
  506. */
  507. /*
  508. * Import linearized data into hash table.
  509. *
  510. * This is the inverse function to hexport(): it takes a linear list
  511. * of "name=value" pairs and creates hash table entries from it.
  512. *
  513. * Entries without "value", i. e. consisting of only "name" or
  514. * "name=", will cause this entry to be deleted from the hash table.
  515. *
  516. * The "flag" argument can be used to control the behaviour: when the
  517. * H_NOCLEAR bit is set, then an existing hash table will kept, i. e.
  518. * new data will be added to an existing hash table; otherwise, old
  519. * data will be discarded and a new hash table will be created.
  520. *
  521. * The separator character for the "name=value" pairs can be selected,
  522. * so we both support importing from externally stored environment
  523. * data (separated by NUL characters) and from plain text files
  524. * (entries separated by newline characters).
  525. *
  526. * To allow for nicely formatted text input, leading white space
  527. * (sequences of SPACE and TAB chars) is ignored, and entries starting
  528. * (after removal of any leading white space) with a '#' character are
  529. * considered comments and ignored.
  530. *
  531. * [NOTE: this means that a variable name cannot start with a '#'
  532. * character.]
  533. *
  534. * When using a non-NUL separator character, backslash is used as
  535. * escape character in the value part, allowing for example for
  536. * multi-line values.
  537. *
  538. * In theory, arbitrary separator characters can be used, but only
  539. * '\0' and '\n' have really been tested.
  540. */
  541. int himport_r(struct hsearch_data *htab,
  542. const char *env, size_t size, const char sep, int flag)
  543. {
  544. char *data, *sp, *dp, *name, *value;
  545. /* Test for correct arguments. */
  546. if (htab == NULL) {
  547. __set_errno(EINVAL);
  548. return 0;
  549. }
  550. /* we allocate new space to make sure we can write to the array */
  551. if ((data = malloc(size)) == NULL) {
  552. debug("himport_r: can't malloc %d bytes\n", size);
  553. __set_errno(ENOMEM);
  554. return 0;
  555. }
  556. memcpy(data, env, size);
  557. dp = data;
  558. if ((flag & H_NOCLEAR) == 0) {
  559. /* Destroy old hash table if one exists */
  560. debug("Destroy Hash Table: %p table = %p\n", htab,
  561. htab->table);
  562. if (htab->table)
  563. hdestroy_r(htab);
  564. }
  565. /*
  566. * Create new hash table (if needed). The computation of the hash
  567. * table size is based on heuristics: in a sample of some 70+
  568. * existing systems we found an average size of 39+ bytes per entry
  569. * in the environment (for the whole key=value pair). Assuming a
  570. * size of 8 per entry (= safety factor of ~5) should provide enough
  571. * safety margin for any existing environment definitions and still
  572. * allow for more than enough dynamic additions. Note that the
  573. * "size" argument is supposed to give the maximum enviroment size
  574. * (CONFIG_ENV_SIZE). This heuristics will result in
  575. * unreasonably large numbers (and thus memory footprint) for
  576. * big flash environments (>8,000 entries for 64 KB
  577. * envrionment size), so we clip it to a reasonable value.
  578. * On the other hand we need to add some more entries for free
  579. * space when importing very small buffers. Both boundaries can
  580. * be overwritten in the board config file if needed.
  581. */
  582. if (!htab->table) {
  583. int nent = CONFIG_ENV_MIN_ENTRIES + size / 8;
  584. if (nent > CONFIG_ENV_MAX_ENTRIES)
  585. nent = CONFIG_ENV_MAX_ENTRIES;
  586. debug("Create Hash Table: N=%d\n", nent);
  587. if (hcreate_r(nent, htab) == 0) {
  588. free(data);
  589. return 0;
  590. }
  591. }
  592. /* Parse environment; allow for '\0' and 'sep' as separators */
  593. do {
  594. ENTRY e, *rv;
  595. /* skip leading white space */
  596. while ((*dp == ' ') || (*dp == '\t'))
  597. ++dp;
  598. /* skip comment lines */
  599. if (*dp == '#') {
  600. while (*dp && (*dp != sep))
  601. ++dp;
  602. ++dp;
  603. continue;
  604. }
  605. /* parse name */
  606. for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp)
  607. ;
  608. /* deal with "name" and "name=" entries (delete var) */
  609. if (*dp == '\0' || *(dp + 1) == '\0' ||
  610. *dp == sep || *(dp + 1) == sep) {
  611. if (*dp == '=')
  612. *dp++ = '\0';
  613. *dp++ = '\0'; /* terminate name */
  614. debug("DELETE CANDIDATE: \"%s\"\n", name);
  615. if (hdelete_r(name, htab) == 0)
  616. debug("DELETE ERROR ##############################\n");
  617. continue;
  618. }
  619. *dp++ = '\0'; /* terminate name */
  620. /* parse value; deal with escapes */
  621. for (value = sp = dp; *dp && (*dp != sep); ++dp) {
  622. if ((*dp == '\\') && *(dp + 1))
  623. ++dp;
  624. *sp++ = *dp;
  625. }
  626. *sp++ = '\0'; /* terminate value */
  627. ++dp;
  628. /* enter into hash table */
  629. e.key = name;
  630. e.data = value;
  631. hsearch_r(e, ENTER, &rv, htab);
  632. if (rv == NULL) {
  633. printf("himport_r: can't insert \"%s=%s\" into hash table\n",
  634. name, value);
  635. return 0;
  636. }
  637. debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n",
  638. htab, htab->filled, htab->size,
  639. rv, name, value);
  640. } while ((dp < data + size) && *dp); /* size check needed for text */
  641. /* without '\0' termination */
  642. debug("INSERT: free(data = %p)\n", data);
  643. free(data);
  644. debug("INSERT: done\n");
  645. return 1; /* everything OK */
  646. }