Add init function and better prompt
This commit is contained in:
parent
e088bfbac0
commit
51e83010e0
23
src/main.c
23
src/main.c
|
@ -3,9 +3,15 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <pwd.h>
|
||||
#include <libgen.h>
|
||||
|
||||
int position = 0;
|
||||
char pwd[128]; // FIXME: change to PATH_MAX
|
||||
char hostname[1024]; // Hostname of machine
|
||||
struct passwd *pw; // Struct of passwd for uid
|
||||
char *username; // Current username
|
||||
char *homedir; // HOME directory of current user
|
||||
|
||||
char **split_line(char *line, char *delim) {
|
||||
int bufsize = 64;
|
||||
|
@ -106,16 +112,29 @@ int parse_command(char **command) {
|
|||
return status;
|
||||
}
|
||||
|
||||
void init() { // Some initial tasks
|
||||
getcwd(pwd, sizeof(pwd)); // Get directory where shell was started
|
||||
gethostname(hostname, sizeof(hostname)); // Get hostname
|
||||
pw = getpwuid(getuid()); // Get passwd struct for current uid
|
||||
username = pw->pw_name;
|
||||
homedir = pw->pw_dir;
|
||||
}
|
||||
|
||||
void print_ps() {
|
||||
// TODO: add parsing config file
|
||||
printf("%s@%s %s> ", username, hostname, basename(pwd));
|
||||
}
|
||||
|
||||
int main() {//int argc, char *argv[]) {
|
||||
char *line = NULL;
|
||||
size_t len = 0;
|
||||
init();
|
||||
char** commands;
|
||||
char** args;
|
||||
int status = 0; // FIXME
|
||||
int command_status = 0; // FIXME
|
||||
getcwd(pwd, sizeof(pwd));
|
||||
while(1) {
|
||||
printf("orsh> "); // FIXME or TODO: add PS1 var
|
||||
print_ps(); // Prints PS1 (TODO: also should print PS2 and so on if it is required)
|
||||
status = getline(&line, &len, stdin);
|
||||
if (status == EOF) {
|
||||
printf("\nexit\n");
|
||||
|
|
Loading…
Reference in New Issue
Block a user