diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-02-09 17:35:25 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-02-09 17:35:25 -0600 |
| commit | 5f08fd6e7794dcdfb74c93af16dfbc903458b0ea (patch) | |
| tree | c10834b004e83afcde5228f9a9d0f9c91b29500e /src/lsh_builtins.c | |
| parent | ed16684cf50e2525072861cfd06e19ca4410b54f (diff) | |
split. better hostname
Diffstat (limited to 'src/lsh_builtins.c')
| -rw-r--r-- | src/lsh_builtins.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/lsh_builtins.c b/src/lsh_builtins.c new file mode 100644 index 0000000..34aced5 --- /dev/null +++ b/src/lsh_builtins.c @@ -0,0 +1,91 @@ +#ifndef shell_builtins +#define shell_builtins + +#include "lsh_main_func.c" +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +/* + Function Declarations for builtin shell commands: + */ + +int lsh_cd(char **args); +int lsh_help(char **args); +int lsh_exit(char **args); + +/* + List of builtin commands, followed by their corresponding functions. + */ +char *builtin_str[] = { + "cd", + "help", + "exit" +}; + +int (*builtin_func[]) (char **) = { + &lsh_cd, + &lsh_help, + &lsh_exit +}; + +int lsh_num_builtins() { + return sizeof(builtin_str) / sizeof(char *); +} + +/* + Builtin function implementations. +*/ +int lsh_cd(char **args) +{ + if (args[1] == NULL) { + fprintf(stderr, "lsh: expected argument to \"cd\"\n"); + } else { + if (chdir(args[1]) != 0) { + perror("lsh"); + } + } + return 1; +} + +int lsh_help(char **args) +{ + int i; + printf("Stephen Brennan's LSH\n"); + printf("Type program names and arguments, and hit enter.\n"); + printf("The following are built in:\n"); + + for (i = 0; i < lsh_num_builtins(); i++) { + printf(" %s\n", builtin_str[i]); + } + + printf("Use the man command for information on other programs.\n"); + return 1; +} + +int lsh_exit(char **args) +{ + return 0; +} + + + +int lsh_execute(char **args) +{ + int i; + + if (args[0] == NULL) { + // An empty command was entered. + return 1; + } + + for (i = 0; i < lsh_num_builtins(); i++) { + if (strcmp(args[0], builtin_str[i]) == 0) { + return (*builtin_func[i])(args); + } + } + + return lsh_launch(args); +} + +#endif |
