commit 055763efe503afc213705564124dbac6465a95cc
parent 280e64a310e2a9a4de16041cb6b3c9856081f948
Author: hhvn <dev@hhvn.uk>
Date: Wed, 20 Apr 2022 17:47:57 +0100
Check for existing aliases before adding.
Diffstat:
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/src/commands.c b/src/commands.c
@@ -1383,7 +1383,8 @@ command_alias) {
ui_error("no such alias: '%s'", alias);
} else {
- alias_add(alias, cmd);
+ if (alias_add(alias, cmd) == -1)
+ ui_error("alias already exists: '%s'", alias);
}
}
@@ -2209,12 +2210,21 @@ alias_add(char *alias, char *cmd) {
if (!alias || !cmd)
return -1;
- p = emalloc(sizeof(struct Alias));
if (*alias != '/') {
tmp = emalloc(strlen(alias) + 2);
snprintf(tmp, strlen(alias) + 2, "/%s", alias);
+ }
+
+ for (p = aliases; p; p = p->next)
+ if (strcmp(p->alias, tmp) == 0)
+ return -1;
+
+ p = emalloc(sizeof(struct Alias));
+ if (*alias != '/')
p->alias = tmp;
- } else p->alias = estrdup(alias);
+ else
+ p->alias = estrdup(alias);
+
if (*cmd != '/') {
tmp = emalloc(strlen(cmd) + 2);
snprintf(tmp, strlen(cmd) + 2, "/%s", cmd);