environment vairables. set and get
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
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.
|
||||||
@@ -20,13 +21,15 @@ int lsh_exit(char **args);
|
|||||||
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() {
|
||||||
@@ -68,6 +71,28 @@ 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)
|
||||||
@@ -87,5 +112,4 @@ int lsh_execute(char **args)
|
|||||||
|
|
||||||
return lsh_launch(args);
|
return lsh_launch(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -45,6 +45,14 @@ char *lsh_read_line(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
@@ -60,9 +68,11 @@ char **lsh_split_line(char *line)
|
|||||||
|
|
||||||
token = strtok(line, LSH_TOK_DELIM);
|
token = strtok(line, LSH_TOK_DELIM);
|
||||||
while (token != NULL) {
|
while (token != NULL) {
|
||||||
|
token = handle_vars(token);
|
||||||
tokens[position] = token;
|
tokens[position] = token;
|
||||||
position++;
|
position++;
|
||||||
|
|
||||||
|
|
||||||
if (position >= bufsize) {
|
if (position >= bufsize) {
|
||||||
bufsize += LSH_TOK_BUFSIZE;
|
bufsize += LSH_TOK_BUFSIZE;
|
||||||
tokens = realloc(tokens, bufsize * sizeof(char*));
|
tokens = realloc(tokens, bufsize * sizeof(char*));
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user