- readme. todo

- update pwd
- update SHLVL
- basename pwd in prompt
  - updating pwd
This commit is contained in:
2026-02-09 23:48:22 -06:00
parent f1f01cbbf9
commit 42abbc8f06
5 changed files with 112 additions and 15 deletions

18
README.md Normal file
View File

@@ -0,0 +1,18 @@
# todo
- [x] environment variables
- [x] custom prompts
- [x] minimal current directory
- [ ] quotes
- [-] handle ~
- [ ] dynamically allocate cwd in cd builtin command
- [-] all of posixs
- [ ] aliases
- [x] increase the SHLVL var
- [x] update pwd
- [ ] pipes |
- [ ] redirections > <
- [ ] append >>
- [ ] heredoc
- [ ] rcfile
- [ ] rcfile in homedir

View File

@@ -1,6 +1,8 @@
#ifndef shell_helper_functions #ifndef shell_helper_functions
#define shell_helper_functions #define shell_helper_functions
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@@ -51,4 +53,38 @@ char *str_replace(char *orig, char *rep, char *with) {
return result; return result;
} }
char *basename(char *path_in)
{
char *token = NULL;
char *lastToken = NULL;
char *basePath;
basePath = (char *)malloc(sizeof(char) * (strlen(path_in) + 1));
if (!basePath) {
fprintf(stderr, "lsh: allocatin error\n");
exit(EXIT_FAILURE);
}
strcpy(basePath, path_in);
token = strtok(basePath, "/");
while (token != NULL) {
lastToken = token;
token = strtok(NULL, "/");
}
if (lastToken == NULL){
free(basePath);
return NULL;
}
char *ret_str = (char*)malloc(sizeof(char)*strlen(lastToken)+1);
if (!ret_str) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
strcpy(ret_str,lastToken);
free(basePath);
return ret_str;
}
#endif #endif

View File

@@ -44,9 +44,18 @@ 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) { int err = chdir(args[1]);
if (err != 0) {
perror("lsh"); perror("lsh");
} }
char *cwd=(char*)malloc(sizeof(char)*4096);
if (!cwd) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
getcwd(cwd,sizeof(char*)*4096);
setenv("PWD",cwd,1);
free(cwd);
} }
return 1; return 1;
} }
@@ -54,7 +63,7 @@ int lsh_cd(char **args)
int lsh_help(char **args) int lsh_help(char **args)
{ {
int i; int i;
printf("Stephen Brennan's LSH\n"); printf("FOX SHELL\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");

View File

@@ -46,10 +46,12 @@ char *lsh_read_line(void)
} }
char *handle_vars(char *token){ char *handle_vars(char *token){
token=&token[0];
if (token[0] == '$'){ if (token[0] == '$'){
token=&token[1]; token=&token[1];
token=getenv(token); token=getenv(token);
} }
return token; return token;
} }

View File

@@ -2,11 +2,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "helper_functions.c"
#include "lsh_main_func.c" #include "lsh_main_func.c"
#include "lsh_builtins.c" #include "lsh_builtins.c"
typedef struct{ typedef struct{
char *prompt; char *prompt;
char *pre_prompt;
} SHELL_OB; } SHELL_OB;
void lsh_loop(SHELL_OB *shell_obj) void lsh_loop(SHELL_OB *shell_obj)
@@ -16,6 +18,11 @@ void lsh_loop(SHELL_OB *shell_obj)
int status; int status;
do { do {
//get pwd
char *cwd= getenv("PWD");
char *pwd=basename(cwd);
snprintf(shell_obj->prompt,sizeof(char) * 1024,"%s %s> ",shell_obj->pre_prompt,pwd);
free(pwd);
printf("%s",shell_obj->prompt); printf("%s",shell_obj->prompt);
line = lsh_read_line(); line = lsh_read_line();
args = lsh_split_line(line); args = lsh_split_line(line);
@@ -27,18 +34,43 @@ void lsh_loop(SHELL_OB *shell_obj)
} }
int main(int argc, char **argv){ int main(int argc, char **argv){
//set sh level
char *shlvl = getenv("SHLVL");
if(shlvl != NULL){
int shlvli = atoi(shlvl);
shlvli++;
sprintf(shlvl,"%d",shlvli);
setenv("SHLVL",shlvl,1);
}
//Set The prompt
SHELL_OB shell_obj; SHELL_OB shell_obj;
shell_obj.prompt = (char *)malloc(sizeof(char) * 1024); shell_obj.prompt = (char *)malloc(sizeof(char) * 1024);
if (!shell_obj.prompt){
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
shell_obj.pre_prompt = (char *)malloc(sizeof(char) * 1024);
if (!shell_obj.pre_prompt){
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
//get the hostname //get the hostname
char *hostname = (char *)malloc(sizeof(char) * 1024); char *hostname = (char *)malloc(sizeof(char) * 1024);
if (!hostname) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
gethostname(hostname,sizeof(char) * 1024); gethostname(hostname,sizeof(char) * 1024);
snprintf(shell_obj.prompt,sizeof(char) * 1024,"%s@%s> ",getenv("USER"),hostname); //set the pre_prompt
snprintf(shell_obj.pre_prompt,sizeof(char) * 1024,"%s@%s",getenv("USER"),hostname);
free(hostname);
lsh_loop(&shell_obj); lsh_loop(&shell_obj);
free(hostname);
free(shell_obj.prompt); free(shell_obj.prompt);
free(shell_obj.pre_prompt);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }