dish/builtin.c

94 lines
2 KiB
C
Raw Normal View History

2022-09-18 06:46:23 +00:00
/*
* @Author: 1ridic
* @Date: 2022-09-18 14:16:19
2022-09-18 06:46:23 +00:00
* @Last Modified by: 1ridic
* @Last Modified time: 2022-09-18 23:32:50
2022-09-18 06:46:23 +00:00
*/
#include "builtin.h"
2022-09-18 06:46:23 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
2022-09-18 06:46:23 +00:00
#include <sys/types.h>
#include <unistd.h>
int dish_cd(char **args);
int dish_help(char **args);
int dish_exit(char **args);
2022-09-18 06:53:38 +00:00
int dish_clear(char **args);
int dish_setenv(char **args);
2022-09-20 12:25:49 +00:00
int dish_getenv(char **args);
int dish_unsetenv(char **args);
int dish_echo(char **args);
int dish_laststatus(char **args);
2022-09-18 06:46:23 +00:00
2022-09-20 12:25:49 +00:00
char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv",
"getenv", "unsetenv", "echo", "laststatus"};
2022-09-20 12:25:49 +00:00
int (*builtin_func[])(char **) = {
&dish_cd, &dish_help, &dish_exit, &dish_clear, &dish_setenv,
&dish_getenv, &dish_unsetenv, &dish_echo, &dish_laststatus};
int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); }
int dish_cd(char **args) {
2022-09-18 06:46:23 +00:00
if (args[1] == NULL) {
fprintf(stderr, "dish: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("dish");
}
}
return 0;
}
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;
2022-09-18 06:46:23 +00:00
}
int dish_setenv(char **args) {
2022-09-20 12:25:49 +00:00
if (args[1] == NULL || args[2] == NULL) {
fprintf(stderr, "dish: expected 2 arguments to \"setenv\"\n");
return 1;
}
2022-09-18 06:46:23 +00:00
2022-09-20 12:25:49 +00:00
setenv(args[1], args[2], 1);
return 0;
2022-09-18 06:53:38 +00:00
}
2022-09-20 12:25:49 +00:00
int dish_getenv(char **args) {
fprintf(stdout, "%s\n", getenv(args[1]));
return 0;
}
2022-09-20 12:25:49 +00:00
int dish_unsetenv(char **args) {
long i = 1;
while (args[i] != NULL) {
unsetenv(args[i]);
i++;
}
2022-09-18 06:53:38 +00:00
return 0;
}
2022-09-20 12:25:49 +00:00
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_laststatus(char **args) {
extern int status;
2022-09-20 12:25:49 +00:00
fprintf(stdout, "%d\n", status);
return 0;
2022-09-18 06:46:23 +00:00
}