Add init function and better prompt

This commit is contained in:
g1n 2021-09-18 18:36:37 +03:00
parent e088bfbac0
commit 51e83010e0

View File

@ -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");