environment vairables. set and get

This commit is contained in:
2026-02-09 19:36:03 -06:00
parent 5f08fd6e77
commit f1f01cbbf9
3 changed files with 148 additions and 114 deletions

View File

@@ -7,85 +7,109 @@
#include <unistd.h> #include <unistd.h>
/* /*
Function Declarations for builtin shell commands: Function Declarations for builtin shell commands:
*/ */
int lsh_cd(char **args); int lsh_cd(char **args);
int lsh_help(char **args); int lsh_help(char **args);
int lsh_exit(char **args); int lsh_exit(char **args);
int export(char **args);
/* /*
List of builtin commands, followed by their corresponding functions. List of builtin commands, followed by their corresponding functions.
*/ */
char *builtin_str[] = { char *builtin_str[] = {
"cd", "cd",
"help", "help",
"exit" "exit",
"export"
}; };
int (*builtin_func[]) (char **) = { int (*builtin_func[]) (char **) = {
&lsh_cd, &lsh_cd,
&lsh_help, &lsh_help,
&lsh_exit &lsh_exit,
&export
}; };
int lsh_num_builtins() { int lsh_num_builtins() {
return sizeof(builtin_str) / sizeof(char *); return sizeof(builtin_str) / sizeof(char *);
} }
/* /*
Builtin function implementations. Builtin function implementations.
*/ */
int lsh_cd(char **args) int lsh_cd(char **args)
{ {
if (args[1] == NULL) { if (args[1] == NULL) {
fprintf(stderr, "lsh: expected argument to \"cd\"\n"); fprintf(stderr, "lsh: expected argument to \"cd\"\n");
} else { } else {
if (chdir(args[1]) != 0) { if (chdir(args[1]) != 0) {
perror("lsh"); perror("lsh");
}
} }
} return 1;
return 1;
} }
int lsh_help(char **args) int lsh_help(char **args)
{ {
int i; int i;
printf("Stephen Brennan's LSH\n"); printf("Stephen Brennan's LSH\n");
printf("Type program names and arguments, and hit enter.\n"); printf("Type program names and arguments, and hit enter.\n");
printf("The following are built in:\n"); printf("The following are built in:\n");
for (i = 0; i < lsh_num_builtins(); i++) { for (i = 0; i < lsh_num_builtins(); i++) {
printf(" %s\n", builtin_str[i]); printf(" %s\n", builtin_str[i]);
} }
printf("Use the man command for information on other programs.\n"); printf("Use the man command for information on other programs.\n");
return 1; return 1;
} }
int lsh_exit(char **args) int lsh_exit(char **args)
{ {
return 0; return 0;
}
int export(char **args){
char *token;
int position = 0;
char *key = NULL;
char *value = NULL;
token = strtok(args[1], "=\r\n");
while (token != NULL) {
if(position == 0){
key=token;
} else if(position == 1){
value=token;
}
position++;
token = strtok(NULL, "=\r\n");
}
setenv(key,value,1);
return 1;
} }
int lsh_execute(char **args) int lsh_execute(char **args)
{ {
int i; int i;
if (args[0] == NULL) { if (args[0] == NULL) {
// An empty command was entered. // An empty command was entered.
return 1; 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); 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 #endif

View File

@@ -10,97 +10,107 @@
#define LSH_RL_BUFSIZE 1024 #define LSH_RL_BUFSIZE 1024
char *lsh_read_line(void) char *lsh_read_line(void)
{ {
int bufsize = LSH_RL_BUFSIZE; int bufsize = LSH_RL_BUFSIZE;
int position = 0; int position = 0;
char *buffer = malloc(sizeof(char) * bufsize); char *buffer = malloc(sizeof(char) * bufsize);
int c; int c;
if (!buffer) { if (!buffer) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += LSH_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "lsh: allocation error\n"); fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
} }
}
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += LSH_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
char *handle_vars(char *token){
if (token[0] == '$'){
token=&token[1];
token=getenv(token);
}
return token;
} }
#define LSH_TOK_BUFSIZE 64 #define LSH_TOK_BUFSIZE 64
#define LSH_TOK_DELIM " \t\r\n\a" #define LSH_TOK_DELIM " \t\r\n\a"
char **lsh_split_line(char *line) char **lsh_split_line(char *line)
{ {
int bufsize = LSH_TOK_BUFSIZE, position = 0; int bufsize = LSH_TOK_BUFSIZE, position = 0;
char **tokens = malloc(bufsize * sizeof(char*)); char **tokens = malloc(bufsize * sizeof(char*));
char *token; char *token;
if (!tokens) { if (!tokens) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, LSH_TOK_DELIM);
while (token != NULL) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += LSH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
fprintf(stderr, "lsh: allocation error\n"); fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
} }
token = strtok(NULL, LSH_TOK_DELIM); token = strtok(line, LSH_TOK_DELIM);
} while (token != NULL) {
tokens[position] = NULL; token = handle_vars(token);
return tokens; tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += LSH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
if (!tokens) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, LSH_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
} }
int lsh_launch(char **args) int lsh_launch(char **args)
{ {
pid_t pid, wpid; pid_t pid, wpid;
int status; int status;
pid = fork(); pid = fork();
if (pid == 0) { if (pid == 0) {
// Child process // Child process
if (execvp(args[0], args) == -1) { if (execvp(args[0], args) == -1) {
perror("lsh"); perror("lsh");
}
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("lsh");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
} }
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("lsh");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1; return 1;
} }
#endif #endif

View File

@@ -1,7 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include "lsh_main_func.c" #include "lsh_main_func.c"
@@ -30,15 +28,17 @@ void lsh_loop(SHELL_OB *shell_obj)
int main(int argc, char **argv){ int main(int argc, char **argv){
SHELL_OB shell_obj; SHELL_OB shell_obj;
shell_obj.prompt = (char *)malloc(sizeof(char) * 1024);
//get the hostname //get the hostname
char *hostname = (char *)malloc(sizeof(char) * 1024); char *hostname = (char *)malloc(sizeof(char) * 1024);
gethostname(hostname,sizeof(char) * 1024); gethostname(hostname,sizeof(char) * 1024);
shell_obj.prompt=strcat(strcat(strcat(getenv("USER"), "@"), (char *)hostname), "> "); snprintf(shell_obj.prompt,sizeof(char) * 1024,"%s@%s> ",getenv("USER"),hostname);
lsh_loop(&shell_obj); lsh_loop(&shell_obj);
free(hostname); free(hostname);
free(shell_obj.prompt);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }