mirror of
https://github.com/zoriya/ash.git
synced 2026-06-04 03:06:03 +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_alias(char **args, env_t *env);
|
||||||
int builtin_unalias(char **args, env_t *env);
|
int builtin_unalias(char **args, env_t *env);
|
||||||
int builtin_echo(char **args, env_t *env);
|
int builtin_echo(char **args, env_t *env);
|
||||||
//utility
|
//which / where
|
||||||
bool find_path_in_builtins(char *cmd);
|
bool find_path_in_builtins(char *cmd);
|
||||||
char **get_envpath(env_t *env);
|
char **get_envpath(env_t *env);
|
||||||
char *check_executable(char *cmd, char *folder);
|
char *check_executable(char *cmd, char *folder);
|
||||||
char **get_paths_from_envpath(char *cmd, char **envpath);
|
char **get_paths_from_envpath(char *cmd, char **envpath);
|
||||||
char **get_envpath(env_t *env);
|
char **get_envpath(env_t *env);
|
||||||
|
void fill_path_arr(char *cmd, char **envpath, char **res);
|
||||||
//history
|
//history
|
||||||
int add_to_history(char *cmd, env_t *env);
|
int add_to_history(char *cmd, env_t *env);
|
||||||
int show_history(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)
|
if (!arg_array)
|
||||||
return (NULL);
|
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);
|
arg_array[i] = get_alias_command(arg_array[i], alias);
|
||||||
if (!arg_array[i])
|
if (!arg_array[i])
|
||||||
return (NULL);
|
return (NULL);
|
||||||
@@ -54,8 +54,8 @@ char *get_alias_command(char *cmd, alias_t *alias)
|
|||||||
|
|
||||||
tmp = alias;
|
tmp = alias;
|
||||||
for (; tmp; tmp = tmp->next) {
|
for (; tmp; tmp = tmp->next) {
|
||||||
if (!strcmp(cmd, alias->alias)) {
|
if (!strcmp(cmd, tmp->alias)) {
|
||||||
cmd = strdup(alias->command);
|
cmd = strdup(tmp->command);
|
||||||
tmp = alias;
|
tmp = alias;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-17
@@ -15,9 +15,18 @@
|
|||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
#include "shell.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)
|
char *check_executable(char *cmd, char *folder)
|
||||||
{
|
{
|
||||||
struct stat st_buff;
|
struct stat st_buff = {};
|
||||||
char *path = catpath(folder, cmd);
|
char *path = catpath(folder, cmd);
|
||||||
char *res = path;
|
char *res = path;
|
||||||
|
|
||||||
@@ -58,23 +67,14 @@ void fill_path_arr(char *cmd, char **envpath, char **res)
|
|||||||
res[counter] = NULL;
|
res[counter] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char **get_paths_from_envpath(char *cmd, char **envpath)
|
void print_path_no_stop(char *cmd, char **envpath, env_t *env)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
|
char *dup_cmd = strdup(cmd);
|
||||||
char **paths = NULL;
|
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);
|
find_path_in_builtins(cmd);
|
||||||
if (!envpath)
|
if (!envpath)
|
||||||
return;
|
return;
|
||||||
@@ -101,7 +101,7 @@ int builtin_where(char **argv, env_t *env)
|
|||||||
if (strchr(argv[i], '/'))
|
if (strchr(argv[i], '/'))
|
||||||
printf("where: / in command makes no sense\n");
|
printf("where: / in command makes no sense\n");
|
||||||
else
|
else
|
||||||
print_path_no_stop(argv[i], envpath);
|
print_path_no_stop(argv[i], envpath, env);
|
||||||
}
|
}
|
||||||
free(envpath);
|
free(envpath);
|
||||||
return (0);
|
return (0);
|
||||||
|
|||||||
@@ -12,6 +12,28 @@
|
|||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
#include "builtin.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)
|
bool find_path_in_builtins(char *cmd)
|
||||||
{
|
{
|
||||||
extern const builtin builtins[];
|
extern const builtin builtins[];
|
||||||
@@ -24,11 +46,17 @@ bool find_path_in_builtins(char *cmd)
|
|||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_path(char *cmd, char **envpath)
|
void print_path(char *cmd, char **envpath, env_t *env)
|
||||||
{
|
{
|
||||||
char **res = NULL;
|
char **res = NULL;
|
||||||
|
char *dup_cmd = strdup(cmd);
|
||||||
int len = 0;
|
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))
|
if (find_path_in_builtins(cmd))
|
||||||
return;
|
return;
|
||||||
res = get_paths_from_envpath(cmd, envpath);
|
res = get_paths_from_envpath(cmd, envpath);
|
||||||
@@ -52,7 +80,7 @@ int builtin_which(char **argv, env_t *env)
|
|||||||
}
|
}
|
||||||
envpath = get_envpath(env);
|
envpath = get_envpath(env);
|
||||||
for (int i = 1; argv[i]; i++)
|
for (int i = 1; argv[i]; i++)
|
||||||
print_path(argv[i], envpath);
|
print_path(argv[i], envpath, env);
|
||||||
free(envpath);
|
free(envpath);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,6 @@ char **get_envpath(env_t *env)
|
|||||||
if (pathstr) {
|
if (pathstr) {
|
||||||
path_cpy = strdup(pathstr);
|
path_cpy = strdup(pathstr);
|
||||||
envpath = to_array(path_cpy);
|
envpath = to_array(path_cpy);
|
||||||
free(path_cpy);
|
|
||||||
}
|
}
|
||||||
return (envpath);
|
return (envpath);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user