3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)alias.c 8.3 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
51 static struct alias *atab[ATABSIZE];
54 static void setalias(const char *, const char *);
55 static int unalias(const char *);
56 static size_t hashalias(const char *);
60 setalias(const char *name, const char *val)
62 struct alias *ap, **app;
65 app = &atab[hashalias(name)];
67 ap = ckmalloc(sizeof (struct alias));
68 ap->name = savestr(name);
69 ap->val = savestr(val);
78 freealias(struct alias *ap)
86 unalias(const char *name)
88 struct alias *ap, **app;
90 app = &atab[hashalias(name)];
92 for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
93 if (equal(name, ap->name)) {
95 * if the alias is currently in use (i.e. its
96 * buffer is being used by the input routine) we
97 * just null out the name instead of freeing it.
98 * We could clear it out later, but this situation
99 * is so rare that it hardly seems worth it.
101 if (ap->flag & ALIASINUSE)
120 struct alias *ap, **app;
124 for (i = 0; i < ATABSIZE; i++) {
128 if (ap->flag & ALIASINUSE) {
142 lookupalias(const char *name, int check)
148 for (ap = atab[hashalias(name)]; ap; ap = ap->next) {
149 if (equal(name, ap->name)) {
150 if (check && (ap->flag & ALIASINUSE))
160 comparealiases(const void *p1, const void *p2)
162 const struct alias *const *a1 = p1;
163 const struct alias *const *a2 = p2;
165 return strcmp((*a1)->name, (*a2)->name);
169 printalias(const struct alias *a)
171 out1fmt("%s=", a->name);
180 struct alias **sorted, *ap;
183 sorted = ckmalloc(aliases * sizeof(*sorted));
185 for (i = 0; i < ATABSIZE; i++)
186 for (ap = atab[i]; ap; ap = ap->next)
187 if (*ap->name != '\0')
189 qsort(sorted, aliases, sizeof(*sorted), comparealiases);
190 for (i = 0; i < aliases; i++) {
191 printalias(sorted[i]);
200 aliascmd(int argc __unused, char **argv __unused)
208 if (*argptr == NULL) {
212 while ((n = *argptr++) != NULL) {
213 if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
214 if ((ap = lookupalias(n, 0)) == NULL) {
215 warning("%s: not found", n);
229 unaliascmd(int argc __unused, char **argv __unused)
233 while ((i = nextopt("a")) != '\0') {
239 for (i = 0; *argptr; argptr++)
240 i |= unalias(*argptr);
246 hashalias(const char *p)
248 unsigned int hashval;
250 hashval = (unsigned char)*p << 4;
253 return (hashval % ATABSIZE);
257 iteralias(const struct alias *index)
262 if (index->next != NULL)
263 return (index->next);
264 i = hashalias(index->name) + 1;
266 for (; i < ATABSIZE; i++)