aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorPiotr Pawel Stefaniak <pstef@FreeBSD.org>2023-04-29 20:23:59 +0000
committerPiotr Pawel Stefaniak <pstef@FreeBSD.org>2023-09-12 16:39:20 +0000
commit2fc4a84ed8288bd25be3de35d756f46ec7785030 (patch)
treef3b53285029de15d9db2b57352638454987289d5 /bin
parent63b6e661d25c67b4ab9b7743bf1c68e3876ddc1e (diff)
downloadsrc-2fc4a84ed8288bd25be3de35d756f46ec7785030.tar.gz
src-2fc4a84ed8288bd25be3de35d756f46ec7785030.zip
sh: introduce a function to iterate over all aliases
Currently the data structure holding alias information is opaque for consumers outside alias.c and there is no way to iterate over all aliases, which will become needed by a future commit. The new function "iteralias" takes a null pointer to return the first alias or an existing alias to return the next one, unless there is no alias to return, in which case it returns a null pointer. I slightly changed the static function hashalias so that it returns the index into the array holding link heads, and not the link head directly. In this form it's easier to use by iteralias and the slight adjustment in the three existing callers doesn't look too bad. Differential Revision: https://reviews.freebsd.org/D40619
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/alias.c29
-rw-r--r--bin/sh/alias.h1
2 files changed, 24 insertions, 6 deletions
diff --git a/bin/sh/alias.c b/bin/sh/alias.c
index 4bac525e2678..da860be704b6 100644
--- a/bin/sh/alias.c
+++ b/bin/sh/alias.c
@@ -53,7 +53,7 @@ static int aliases;
static void setalias(const char *, const char *);
static int unalias(const char *);
-static struct alias **hashalias(const char *);
+static size_t hashalias(const char *);
static
void
@@ -62,7 +62,7 @@ setalias(const char *name, const char *val)
struct alias *ap, **app;
unalias(name);
- app = hashalias(name);
+ app = &atab[hashalias(name)];
INTOFF;
ap = ckmalloc(sizeof (struct alias));
ap->name = savestr(name);
@@ -87,7 +87,7 @@ unalias(const char *name)
{
struct alias *ap, **app;
- app = hashalias(name);
+ app = &atab[hashalias(name)];
for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
if (equal(name, ap->name)) {
@@ -145,7 +145,7 @@ lookupalias(const char *name, int check)
if (aliases == 0)
return (NULL);
- for (ap = *hashalias(name); ap; ap = ap->next) {
+ for (ap = atab[hashalias(name)]; ap; ap = ap->next) {
if (equal(name, ap->name)) {
if (check && (ap->flag & ALIASINUSE))
return (NULL);
@@ -242,7 +242,7 @@ unaliascmd(int argc __unused, char **argv __unused)
return (i);
}
-static struct alias **
+static size_t
hashalias(const char *p)
{
unsigned int hashval;
@@ -250,5 +250,22 @@ hashalias(const char *p)
hashval = (unsigned char)*p << 4;
while (*p)
hashval+= *p++;
- return &atab[hashval % ATABSIZE];
+ return (hashval % ATABSIZE);
+}
+
+const struct alias *
+iteralias(const struct alias *index)
+{
+ size_t i = 0;
+
+ if (index != NULL) {
+ if (index->next != NULL)
+ return (index->next);
+ i = hashalias(index->name) + 1;
+ }
+ for (; i < ATABSIZE; i++)
+ if (atab[i] != NULL)
+ return (atab[i]);
+
+ return (NULL);
}
diff --git a/bin/sh/alias.h b/bin/sh/alias.h
index 0c6ea99e8b60..a8108d44be2d 100644
--- a/bin/sh/alias.h
+++ b/bin/sh/alias.h
@@ -42,3 +42,4 @@ struct alias {
};
struct alias *lookupalias(const char *, int);
+const struct alias *iteralias(const struct alias *);