possible buffer overflow; bug report 4.3BSD-tahoe/usr.sbin/3
[unix-history] / usr / src / usr.bin / rdist / lookup.c
CommitLineData
7172eb74
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
0718fb71
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
7172eb74
DF
16 */
17
837386b3 18#ifndef lint
b36fc510 19static char sccsid[] = "@(#)lookup.c 5.4 (Berkeley) %G%";
0718fb71 20#endif /* not lint */
837386b3
RC
21
22#include "defs.h"
23
0fccdfef
RC
24 /* symbol types */
25#define VAR 1
26#define CONST 2
27
28struct syment {
29 int s_type;
30 char *s_name;
31 struct namelist *s_value;
32 struct syment *s_next;
33};
34
35static struct syment *hashtab[HASHSIZE];
36
837386b3
RC
37/*
38 * Define a variable from a command line argument.
39 */
40define(name)
41 char *name;
42{
43 register char *cp, *s;
0fccdfef
RC
44 register struct namelist *nl;
45 struct namelist *value;
837386b3
RC
46
47 if (debug)
48 printf("define(%s)\n", name);
49
50 cp = index(name, '=');
3024eb6f 51 if (cp == NULL)
837386b3 52 value = NULL;
3024eb6f 53 else if (cp[1] == '\0') {
0fccdfef 54 *cp = '\0';
3024eb6f
RC
55 value = NULL;
56 } else if (cp[1] != '(') {
837386b3 57 *cp++ = '\0';
58157a31 58 value = makenl(cp);
837386b3 59 } else {
0fccdfef 60 nl = NULL;
837386b3
RC
61 *cp++ = '\0';
62 do
63 cp++;
64 while (*cp == ' ' || *cp == '\t');
65 for (s = cp; ; s++) {
66 switch (*s) {
67 case ')':
68 *s = '\0';
69 case '\0':
70 break;
71 case ' ':
72 case '\t':
73 *s++ = '\0';
74 while (*s == ' ' || *s == '\t')
75 s++;
76 if (*s == ')')
77 *s = '\0';
78 break;
79 default:
80 continue;
81 }
0fccdfef
RC
82 if (nl == NULL)
83 value = nl = makenl(cp);
837386b3 84 else {
0fccdfef
RC
85 nl->n_next = makenl(cp);
86 nl = nl->n_next;
837386b3
RC
87 }
88 if (*s == '\0')
89 break;
90 cp = s;
91 }
92 }
0fccdfef 93 (void) lookup(name, REPLACE, value);
837386b3
RC
94}
95
837386b3
RC
96/*
97 * Lookup name in the table and return a pointer to it.
0fccdfef
RC
98 * LOOKUP - just do lookup, return NULL if not found.
99 * INSERT - insert name with value, error if already defined.
100 * REPLACE - insert or replace name with value.
837386b3
RC
101 */
102
0fccdfef
RC
103struct namelist *
104lookup(name, action, value)
837386b3 105 char *name;
0fccdfef
RC
106 int action;
107 struct namelist *value;
837386b3
RC
108{
109 register unsigned n;
110 register char *cp;
0fccdfef 111 register struct syment *s;
55669d5b 112 char buf[256];
837386b3
RC
113
114 if (debug)
0fccdfef 115 printf("lookup(%s, %d, %x)\n", name, action, value);
837386b3
RC
116
117 n = 0;
118 for (cp = name; *cp; )
119 n += *cp++;
120 n %= HASHSIZE;
121
0fccdfef
RC
122 for (s = hashtab[n]; s != NULL; s = s->s_next) {
123 if (strcmp(name, s->s_name))
837386b3 124 continue;
0fccdfef 125 if (action != LOOKUP) {
55669d5b 126 if (action != INSERT || s->s_type != CONST) {
a2061e43 127 (void)sprintf(buf, "%s redefined", name);
55669d5b
RC
128 yyerror(buf);
129 }
837386b3 130 }
0fccdfef 131 return(s->s_value);
837386b3
RC
132 }
133
55669d5b 134 if (action == LOOKUP) {
a2061e43
KB
135 (void)sprintf(buf, "%s undefined", name);
136 yyerror(buf);
55669d5b
RC
137 return(NULL);
138 }
837386b3 139
0fccdfef
RC
140 s = ALLOC(syment);
141 if (s == NULL)
837386b3 142 fatal("ran out of memory\n");
0fccdfef
RC
143 s->s_next = hashtab[n];
144 hashtab[n] = s;
145 s->s_type = action == INSERT ? VAR : CONST;
146 s->s_name = name;
147 s->s_value = value;
148 return(value);
837386b3 149}