mirror of
https://github.com/zoriya/ash.git
synced 2026-05-27 16:43:22 +00:00
Merge pull request #63 from AnonymusRaccoon/which/where+alias
Which/where+alias
This commit is contained in:
+2
-1
@@ -29,12 +29,13 @@ int builtin_history(char **args, env_t *env);
|
||||
int builtin_alias(char **args, env_t *env);
|
||||
int builtin_unalias(char **args, env_t *env);
|
||||
int builtin_echo(char **args, env_t *env);
|
||||
//utility
|
||||
//which / where
|
||||
bool find_path_in_builtins(char *cmd);
|
||||
char **get_envpath(env_t *env);
|
||||
char *check_executable(char *cmd, char *folder);
|
||||
char **get_paths_from_envpath(char *cmd, char **envpath);
|
||||
char **get_envpath(env_t *env);
|
||||
void fill_path_arr(char *cmd, char **envpath, char **res);
|
||||
//history
|
||||
int add_to_history(char *cmd, env_t *env);
|
||||
int show_history(env_t *env);
|
||||
|
||||
+3
-3
@@ -39,7 +39,7 @@ char *replace_alias(char *cmd, alias_t *alias)
|
||||
|
||||
if (!arg_array)
|
||||
return (NULL);
|
||||
for (int i = 0; arg_array[i]; i++) {
|
||||
for (int i = 0; i == 0; i++) {
|
||||
arg_array[i] = get_alias_command(arg_array[i], alias);
|
||||
if (!arg_array[i])
|
||||
return (NULL);
|
||||
@@ -54,8 +54,8 @@ char *get_alias_command(char *cmd, alias_t *alias)
|
||||
|
||||
tmp = alias;
|
||||
for (; tmp; tmp = tmp->next) {
|
||||
if (!strcmp(cmd, alias->alias)) {
|
||||
cmd = strdup(alias->command);
|
||||
if (!strcmp(cmd, tmp->alias)) {
|
||||
cmd = strdup(tmp->command);
|
||||
tmp = alias;
|
||||
}
|
||||
}
|
||||
|
||||
+17
-17
@@ -15,9 +15,18 @@
|
||||
#include "utility.h"
|
||||
#include "shell.h"
|
||||
|
||||
void print_alias_where(char *cmd, char *aliased)
|
||||
{
|
||||
if (aliased[0] == '(') {
|
||||
aliased = &aliased[1];
|
||||
aliased[strlen(aliased) - 1] = 0;
|
||||
}
|
||||
printf("%s is aliased to %s\n", cmd, aliased);
|
||||
}
|
||||
|
||||
char *check_executable(char *cmd, char *folder)
|
||||
{
|
||||
struct stat st_buff;
|
||||
struct stat st_buff = {};
|
||||
char *path = catpath(folder, cmd);
|
||||
char *res = path;
|
||||
|
||||
@@ -58,23 +67,14 @@ void fill_path_arr(char *cmd, char **envpath, char **res)
|
||||
res[counter] = NULL;
|
||||
}
|
||||
|
||||
char **get_paths_from_envpath(char *cmd, char **envpath)
|
||||
{
|
||||
int len = 0;
|
||||
char **res = NULL;
|
||||
|
||||
for (; envpath[len]; len++);
|
||||
res = malloc(sizeof(char *) * (len + 2));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
fill_path_arr(cmd, envpath, res);
|
||||
return (res);
|
||||
}
|
||||
|
||||
void print_path_no_stop(char *cmd, char **envpath)
|
||||
void print_path_no_stop(char *cmd, char **envpath, env_t *env)
|
||||
{
|
||||
char *dup_cmd = strdup(cmd);
|
||||
char **paths = NULL;
|
||||
//check_alias_no_stop
|
||||
char *aliased = get_alias_command(cmd, env->alias);
|
||||
|
||||
if (strcmp(aliased, dup_cmd))
|
||||
print_alias_where(dup_cmd, aliased);
|
||||
find_path_in_builtins(cmd);
|
||||
if (!envpath)
|
||||
return;
|
||||
@@ -101,7 +101,7 @@ int builtin_where(char **argv, env_t *env)
|
||||
if (strchr(argv[i], '/'))
|
||||
printf("where: / in command makes no sense\n");
|
||||
else
|
||||
print_path_no_stop(argv[i], envpath);
|
||||
print_path_no_stop(argv[i], envpath, env);
|
||||
}
|
||||
free(envpath);
|
||||
return (0);
|
||||
|
||||
@@ -12,6 +12,28 @@
|
||||
#include "utility.h"
|
||||
#include "builtin.h"
|
||||
|
||||
char **get_paths_from_envpath(char *cmd, char **envpath)
|
||||
{
|
||||
int len = 0;
|
||||
char **res = NULL;
|
||||
|
||||
for (; envpath[len]; len++);
|
||||
res = malloc(sizeof(char *) * (len + 2));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
fill_path_arr(cmd, envpath, res);
|
||||
return (res);
|
||||
}
|
||||
|
||||
void print_alias_which(char *cmd, char *aliased)
|
||||
{
|
||||
if (aliased[0] == '(') {
|
||||
aliased = &aliased[1];
|
||||
aliased[strlen(aliased) - 1] = 0;
|
||||
}
|
||||
printf("%s:\taliased to %s\n", cmd, aliased);
|
||||
}
|
||||
|
||||
bool find_path_in_builtins(char *cmd)
|
||||
{
|
||||
extern const builtin builtins[];
|
||||
@@ -24,11 +46,17 @@ bool find_path_in_builtins(char *cmd)
|
||||
return (false);
|
||||
}
|
||||
|
||||
void print_path(char *cmd, char **envpath)
|
||||
void print_path(char *cmd, char **envpath, env_t *env)
|
||||
{
|
||||
char **res = NULL;
|
||||
char *dup_cmd = strdup(cmd);
|
||||
int len = 0;
|
||||
//check_alias
|
||||
char *aliased = get_alias_command(cmd, env->alias);
|
||||
|
||||
if (strcmp(aliased, dup_cmd)) {
|
||||
print_alias_which(dup_cmd, aliased);
|
||||
return;
|
||||
}
|
||||
if (find_path_in_builtins(cmd))
|
||||
return;
|
||||
res = get_paths_from_envpath(cmd, envpath);
|
||||
@@ -52,7 +80,7 @@ int builtin_which(char **argv, env_t *env)
|
||||
}
|
||||
envpath = get_envpath(env);
|
||||
for (int i = 1; argv[i]; i++)
|
||||
print_path(argv[i], envpath);
|
||||
print_path(argv[i], envpath, env);
|
||||
free(envpath);
|
||||
return (0);
|
||||
}
|
||||
@@ -28,7 +28,6 @@ char **get_envpath(env_t *env)
|
||||
if (pathstr) {
|
||||
path_cpy = strdup(pathstr);
|
||||
envpath = to_array(path_cpy);
|
||||
free(path_cpy);
|
||||
}
|
||||
return (envpath);
|
||||
}
|
||||
Reference in New Issue
Block a user