summaryrefslogtreecommitdiff
path: root/unproto/strsave.c
blob: c2a4b15e5bfebcd43bc4421f418ae6351ab40161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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));
}