-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for PATH and other environment variables
Also, the shell prompt now has color
- Loading branch information
1 parent
fe1282d
commit 58e1b01
Showing
5 changed files
with
171 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "envmgr.h" | ||
|
||
#include <string.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <linux/limits.h> | ||
|
||
static char* pwd; | ||
static char* old_pwd; | ||
|
||
static char prog_path[PATH_MAX]; | ||
|
||
//The compiler throws a hissyfit if setenv doesn't have a function declaration | ||
//here, despite the fact that it *IS* in stdlib.h which *IS* #include'd | ||
int setenv(const char*, const char*, int); | ||
|
||
void init_envmgr(){ | ||
pwd = malloc(sizeof(char) * PATH_MAX); | ||
if(getcwd(pwd, PATH_MAX) != pwd){ | ||
pwd[0] = '\0'; | ||
} | ||
old_pwd = malloc(sizeof(char) * PATH_MAX); | ||
} | ||
|
||
void deinit_envmgr(){ | ||
free(pwd); | ||
free(old_pwd); | ||
} | ||
|
||
const char* get_pwd(){ | ||
return pwd; | ||
} | ||
|
||
void update_pwd(){ | ||
strncpy(old_pwd, pwd, PATH_MAX); | ||
if(getcwd(pwd, PATH_MAX) != pwd){ | ||
pwd[0] = '\0'; | ||
} | ||
setenv("PWD", pwd, 1); | ||
setenv("OLDPWD", old_pwd, 1); | ||
} | ||
|
||
void execute_program(char* program, char** arguments){ | ||
//Try to execute the program in all the paths found in $PATH | ||
//But first, try to execute it in the current directory | ||
execv(program, arguments); | ||
//If we return from an execv call, it failed | ||
char* pathstr = getenv("PATH"); | ||
|
||
if(!pathstr) | ||
return; | ||
|
||
char* begincolon = pathstr; | ||
char* endcolon = NULL; | ||
do{ | ||
if(*pathstr == ':' || *pathstr == '\0'){ | ||
endcolon = pathstr; | ||
int size = endcolon - begincolon; | ||
strncpy(prog_path, begincolon, size); | ||
//Add a slash to the end of the path before concatenating | ||
//our command string | ||
prog_path[size] = '/'; | ||
|
||
//Now add our command to the end | ||
char* cat_begin = (prog_path + size + 1); | ||
//don't let anyone copy past the end of the array | ||
size = PATH_MAX - size - 1; | ||
strncpy(cat_begin, program, size); | ||
|
||
//Now try to run the program from that location | ||
execv(prog_path, arguments); | ||
begincolon = endcolon + 1; | ||
|
||
} | ||
}while(*(pathstr++) != '\0'); | ||
|
||
//If we get to here, then the command could not be found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef ENVIRONMENT_MANAGER_H | ||
#define ENVIRONMENT_MANAGER_H | ||
|
||
#include <stdlib.h> | ||
|
||
void init_envmgr(); | ||
void deinit_envmgr(); | ||
|
||
void update_pwd(); | ||
const char* get_pwd(); | ||
void execute_program(char* program, char** arguments); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters