]> git.sur5r.net Git - openldap/blob - build/unproto/strsave.c
Initial revision
[openldap] / build / unproto / strsave.c
1 /*++
2 /* NAME
3 /*      strsave 3
4 /* SUMMARY
5 /*      maintain unique copy of a string
6 /* SYNOPSIS
7 /*      char *strsave(string)
8 /*      char *string;
9 /* DESCRIPTION
10 /*      This function returns a pointer to an unique copy of its
11 /*      argument.
12 /* DIAGNOSTISC
13 /*      strsave() calls fatal() when it runs out of memory.
14 /* AUTHOR(S)
15 /*      Wietse Venema
16 /*      Eindhoven University of Technology
17 /*      Department of Mathematics and Computer Science
18 /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
19 /* LAST MODIFICATION
20 /*      92/01/15 21:53:13
21 /* VERSION/RELEASE
22 /*      1.1
23 /*--*/
24
25 static char strsave_sccsid[] = "@(#) strsave.c 1.1 92/01/15 21:53:13";
26
27 /* C library */
28
29 extern char *strcpy();
30 extern char *malloc();
31
32 /* Application-specific stuff */
33
34 #include "error.h"
35
36 #define STR_TABSIZE     100
37
38 struct string {
39     char   *strval;                     /* unique string copy */
40     struct string *next;                /* next one in hash chain */
41 };
42
43 static struct string *str_tab[STR_TABSIZE] = {0,};
44
45 /* More string stuff. Maybe it should go to an #include file. */
46
47 #define STREQ(x,y)      (*(x) == *(y) && strcmp((x),(y)) == 0)
48
49 /* strsave - save unique copy of string */
50
51 char   *strsave(str)
52 register char *str;
53 {
54     register struct string *s;
55     register int where = hash(str, STR_TABSIZE);
56
57     /* Look for existing entry. */
58
59     for (s = str_tab[where]; s; s = s->next)
60         if (STREQ(str, s->strval))
61             return (s->strval);
62
63     /* Add new entry. */
64
65     if ((s = (struct string *) malloc(sizeof(*s))) == 0
66         || (s->strval = malloc(strlen(str) + 1)) == 0)
67         fatal("out of memory");
68     s->next = str_tab[where];
69     str_tab[where] = s;
70     return (strcpy(s->strval, str));
71 }