Newer
Older
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
72
73
74
75
76
77
78
79
80
81
/*
** builtin_alias.c for 42sh
**
** Made by seblu
** Login <seblu@epita.fr>
**
** Started on Thu Nov 16 17:18:24 2006 seblu
** Last update Thu Nov 16 19:27:01 2006 seblu
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "builtin.h"
#include "../shell/shell.h"
static int show_all_alias(void);
static int show_alias(const char *name);
static int getoption(char *argv[], int *argp, int *p);
int builtin_alias(char *argv[])
{
int argp;
char *carg;
char *equal;
int show = 0;
assert(argv && argv[0]);
if (!getoption(argv, &argp, &show))
return 2;
if (argv[1] == NULL || show)
return show_all_alias();
for (; (carg = argv[argp]); ++argp) {
if (!(equal = strchr(carg, '=')))
show_alias(carg);
else {
*equal = 0;
alias_add(shell->alias, strdup(carg), strdup(equal + 1));
*equal = '=';
}
}
return 0;
}
static int getoption(char *argv[], int *argp, int *p)
{
char *carg;
for (*argp = 1; (carg = argv[*argp]); ++*argp)
if (*carg == '-')
for (++carg; *carg; ++carg)
if (*carg == 'p')
*p = 1;
else {
fprintf(stderr, "%s: alias: -%c: invalid option.\n",
shell->name, *carg);
fprintf(stderr, "alias: usage: alias [-p] [name[=value]] ... ]\n");
return 0;
}
return 1;
}
static int show_alias(const char *name)
{
for (size_t i = 0; i < shell->alias->count; ++i)
if (!strcmp(shell->alias->table[i].name, name)) {
printf("alias %s=%s\n", shell->alias->table[i].name,
shell->alias->table[i].value);
return 1;
}
printf("%s: alias: %s: not found.\n", shell->name, name);
return 0;
}
static int show_all_alias(void)
{
for (size_t i = 0; i < shell->alias->count; ++i)
printf("alias %s=%s\n", shell->alias->table[i].name,
shell->alias->table[i].value);
return 0;
}