diff options
author | Robert de Bath <rdebath@poboxes.com> | 1997-02-25 20:42:19 +0100 |
---|---|---|
committer | Lubomir Rintel <lkundrak@v3.sk> | 2013-10-23 23:38:07 +0200 |
commit | 4c36e9a0c125ccfff37aa440dab2cf58c4152fff (patch) | |
tree | a5d9c84ba2661029ddb2223dacd50529a361c3d5 /unproto/strsave.c | |
parent | f8de35da65c5d93bb733073cf40da154bc1c0748 (diff) | |
parent | 9696d7b0e1f3a1b0f5fd4a0428eb75afe8ad4ed6 (diff) | |
download | dev86-4c36e9a0c125ccfff37aa440dab2cf58c4152fff.tar.gz |
Import Dev86src-0.0.11.tar.gzv0.0.11
Diffstat (limited to 'unproto/strsave.c')
-rw-r--r-- | unproto/strsave.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/unproto/strsave.c b/unproto/strsave.c new file mode 100644 index 0000000..c2a4b15 --- /dev/null +++ b/unproto/strsave.c @@ -0,0 +1,71 @@ +/*++ +/* NAME +/* strsave 3 +/* SUMMARY +/* maintain unique copy of a string +/* SYNOPSIS +/* char *strsave(string) +/* char *string; +/* DESCRIPTION +/* This function returns a pointer to an unique copy of its +/* argument. +/* DIAGNOSTISC +/* strsave() calls fatal() when it runs out of memory. +/* AUTHOR(S) +/* Wietse Venema +/* Eindhoven University of Technology +/* Department of Mathematics and Computer Science +/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands +/* LAST MODIFICATION +/* 92/01/15 21:53:13 +/* VERSION/RELEASE +/* 1.1 +/*--*/ + +static char strsave_sccsid[] = "@(#) strsave.c 1.1 92/01/15 21:53:13"; + +/* C library */ + +extern char *strcpy(); +extern char *malloc(); + +/* Application-specific stuff */ + +#include "error.h" + +#define STR_TABSIZE 100 + +struct string { + char *strval; /* unique string copy */ + struct string *next; /* next one in hash chain */ +}; + +static struct string *str_tab[STR_TABSIZE] = {0,}; + +/* More string stuff. Maybe it should go to an #include file. */ + +#define STREQ(x,y) (*(x) == *(y) && strcmp((x),(y)) == 0) + +/* strsave - save unique copy of string */ + +char *strsave(str) +register char *str; +{ + register struct string *s; + register int where = hash(str, STR_TABSIZE); + + /* Look for existing entry. */ + + for (s = str_tab[where]; s; s = s->next) + if (STREQ(str, s->strval)) + return (s->strval); + + /* Add new entry. */ + + if ((s = (struct string *) malloc(sizeof(*s))) == 0 + || (s->strval = malloc(strlen(str) + 1)) == 0) + fatal("out of memory"); + s->next = str_tab[where]; + str_tab[where] = s; + return (strcpy(s->strval, str)); +} |