5 /* maintain unique copy of a string
7 /* char *strsave(string)
10 /* This function returns a pointer to an unique copy of its
13 /* strsave() calls fatal() when it runs out of memory.
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
25 static char strsave_sccsid
[] = "@(#) strsave.c 1.1 92/01/15 21:53:13";
29 extern char *strcpy();
30 extern char *malloc();
32 /* Application-specific stuff */
36 #define STR_TABSIZE 100
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 */
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
))
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
];
70 return (strcpy(s
->strval
, str
));