* added compilers lcc and bcc (linux86)
[mascara-docs.git] / compilers / linux86-0.16.17 / unproto / strsave.c
blobc2a4b15e5bfebcd43bc4421f418ae6351ab40161
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 /*--*/
25 static char strsave_sccsid[] = "@(#) strsave.c 1.1 92/01/15 21:53:13";
27 /* C library */
29 extern char *strcpy();
30 extern char *malloc();
32 /* Application-specific stuff */
34 #include "error.h"
36 #define STR_TABSIZE 100
38 struct string {
39 char *strval; /* unique string copy */
40 struct string *next; /* next one in hash chain */
43 static struct string *str_tab[STR_TABSIZE] = {0,};
45 /* More string stuff. Maybe it should go to an #include file. */
47 #define STREQ(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0)
49 /* strsave - save unique copy of string */
51 char *strsave(str)
52 register char *str;
54 register struct string *s;
55 register int where = hash(str, STR_TABSIZE);
57 /* Look for existing entry. */
59 for (s = str_tab[where]; s; s = s->next)
60 if (STREQ(str, s->strval))
61 return (s->strval);
63 /* Add new entry. */
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));