feat(builtin): add built-in functions
add setenv, unset, echo command to check env
This commit is contained in:
parent
329262e47b
commit
4a018bad5d
1 changed files with 47 additions and 27 deletions
74
builtin.c
74
builtin.c
|
@ -1,11 +1,13 @@
|
|||
/*
|
||||
* @Author: 1ridic
|
||||
* @Date: 2022-09-18 14:16:19
|
||||
* @Author: 1ridic
|
||||
* @Date: 2022-09-18 14:16:19
|
||||
* @Last Modified by: 1ridic
|
||||
* @Last Modified time: 2022-09-18 14:52:17
|
||||
* @Last Modified time: 2022-09-18 20:46:12
|
||||
*/
|
||||
#include "builtin.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -13,23 +15,19 @@ int dish_cd(char **args);
|
|||
int dish_help(char **args);
|
||||
int dish_exit(char **args);
|
||||
int dish_clear(char **args);
|
||||
int dish_setenv(char **args);
|
||||
int dish_echo(char **args);
|
||||
int dish_unset(char **args);
|
||||
|
||||
char* builtin_cmd[] = {
|
||||
"cd",
|
||||
"help",
|
||||
"exit",
|
||||
"clear"
|
||||
};
|
||||
char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv", "echo", "unset"};
|
||||
|
||||
int (*builtin_func[]) (char **) = {
|
||||
&dish_cd,
|
||||
&dish_help,
|
||||
&dish_exit,
|
||||
&dish_clear
|
||||
};
|
||||
int (*builtin_func[])(char **) = {&dish_cd, &dish_help, &dish_exit,
|
||||
&dish_clear, &dish_setenv, &dish_echo,
|
||||
&dish_unset};
|
||||
|
||||
int dish_cd(char **args)
|
||||
{
|
||||
int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); }
|
||||
|
||||
int dish_cd(char **args) {
|
||||
if (args[1] == NULL) {
|
||||
fprintf(stderr, "dish: expected argument to \"cd\"\n");
|
||||
} else {
|
||||
|
@ -40,22 +38,44 @@ int dish_cd(char **args)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int dish_help(char **args)
|
||||
int dish_help(char **args) {
|
||||
fprintf(stdout, "Yet another Shell with C.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dish_exit(char **args) { exit(EXIT_SUCCESS); }
|
||||
|
||||
int dish_clear(char **args) {
|
||||
fprintf(stdout, "\033[H\033[J");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dish_setenv(char **args) {
|
||||
if (args[1] == NULL || args[2] == NULL)
|
||||
{
|
||||
fprintf(stdout, "Yet another Shell with C.\n");
|
||||
return 0;
|
||||
fprintf(stderr, "dish: expected 2 arguments to \"setenv\"\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dish_exit(char **args)
|
||||
{
|
||||
exit(EXIT_SUCCESS);
|
||||
setenv(args[1], args[2], 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getBuiltinNum() {
|
||||
return sizeof(builtin_cmd) / sizeof(char *);
|
||||
int dish_echo(char **args) {
|
||||
if (args[1][0] == '$') {
|
||||
char *env = getenv(args[1] + sizeof(char));
|
||||
if (env != NULL)
|
||||
fprintf(stdout, "%s\n", env);
|
||||
} else
|
||||
fprintf(stdout, "%s\n", args[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dish_clear(char **args){
|
||||
fprintf(stdout,"\033[H\033[J");
|
||||
int dish_unset(char **args) {
|
||||
long i = 1;
|
||||
while (args[i] != NULL) {
|
||||
unsetenv(args[i]);
|
||||
i++;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue