Skip to content

Commit

Permalink
Merge pull request #26 from Livl-Corporation/fg/feat/display-pwd
Browse files Browse the repository at this point in the history
feat: display current wd
  • Loading branch information
jvondermarck authored Dec 30, 2023
2 parents 946c171 + b86dc03 commit 121a4c6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 31 deletions.
64 changes: 44 additions & 20 deletions src/builtin_commands.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,85 @@
#include "builtin_commands.h"

int is_builtin_command(const Command* command, const char* expected) {
int is_builtin_command(const Command *command, const char *expected)
{
return strcmp(command->command, expected) == 0;
}

void cd(const char* path) {
if (chdir(path) != 0) {
void cd(const char *path)
{
if (chdir(path) != 0)
{
perror("cd");
}
}

void pwd() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
void pwd()
{
char cwd[MAX_INPUT_LENGTH];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
perror("pwd");
} else {
}
else
{
printf("%s\n", cwd);
}
}

void exit_shell() {
void exit_shell()
{
exit(EXIT_SUCCESS);
}

void echo(const Command* command) {
for (int i = 0; i < command->num_arguments; i++) {
void echo(const Command *command)
{
for (int i = 0; i < command->num_arguments; i++)
{
printf("%s ", command->arguments[i]);
}
printf("\n");
}

int execute_builtin_command(const Command* command) {
if (is_builtin_command(command, "cd")) {
if (command->num_arguments == 1) {
int execute_builtin_command(const Command *command)
{
if (is_builtin_command(command, "cd"))
{
if (command->num_arguments == 1)
{
cd(command->arguments[0]);
return IS_BUILTIN_COMMAND;
}
perror("Usage: cd <directory>");
} else if (is_builtin_command(command, "pwd")) {
if (command->num_arguments == 0) {
}
else if (is_builtin_command(command, "pwd"))
{
if (command->num_arguments == 0)
{
pwd();
return IS_BUILTIN_COMMAND;
}
perror("Usage: pwd");
} else if (is_builtin_command(command, "exit")) {
if (command->num_arguments == 0) {
}
else if (is_builtin_command(command, "exit"))
{
if (command->num_arguments == 0)
{
exit_shell();
return IS_BUILTIN_COMMAND;
}
perror("Usage: exit");
} else if (is_builtin_command(command, "echo")) {
if (command->num_arguments >= 1) {
}
else if (is_builtin_command(command, "echo"))
{
if (command->num_arguments >= 1)
{
echo(command);
return IS_BUILTIN_COMMAND;
}
perror("Usage: echo <message>");
} else {
}
else
{
return IS_NOT_BUILTIN_COMMAND;
}

Expand Down
29 changes: 18 additions & 11 deletions src/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

void print_title()
{
printf("\n" YELB" __ _____ ____ _____ __ __________ __ " RESET);
printf("\n" YELB" / / / _/ | / / / / ___// / / / ____/ / / / " RESET);
printf("\n" YELB" / / / / | | / / / \\__ \\/ /_/ / __/ / / / / " RESET);
printf("\n" YELB" / /____/ / | |/ / /___ ___/ / __ / /___/ /___/ /___ " RESET);
printf("\n" YELB"/_____/___/ |___/_____/ /____/_/ /_/_____/_____/_____/ " RESET);
printf("\n" YELB" "RESET "\n\n");
printf("\n" YELB " __ _____ ____ _____ __ __________ __ " RESET);
printf("\n" YELB " / / / _/ | / / / / ___// / / / ____/ / / / " RESET);
printf("\n" YELB " / / / / | | / / / \\__ \\/ /_/ / __/ / / / / " RESET);
printf("\n" YELB " / /____/ / | |/ / /___ ___/ / __ / /___/ /___/ /___ " RESET);
printf("\n" YELB "/_____/___/ |___/_____/ /____/_/ /_/_____/_____/_____/ " RESET);
printf("\n" YELB " " RESET "\n\n");
}

void print_perror(const char *format, ...) {
void print_perror(const char *format, ...)
{
va_list args;
va_start(args, format);

Expand All @@ -32,17 +33,23 @@ void print_info(const char *text, ...)

void print_prompt()
{
char* username = getenv("USER");
if(username == NULL)
username = "username";
char *username = getenv("USER");
if (username == NULL)
username = "";

char cwd[MAX_INPUT_LENGTH];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
cwd[0] = '\0';
}

// Use this if you want to print the hostname
// char hostname[1024];
// gethostname(hostname, 1024);
// if(username == NULL)
// username = "hostname";

printf(YELLOW "%s" RESET "@" BLUE "livl-shell" RESET ":$ ", username);
printf(YELLOW "%s" RESET "@" BLUE "livl-shell" GREEN " %s" RESET " $ ", username, cwd);
}

void print_exit_info()
Expand Down

0 comments on commit 121a4c6

Please sign in to comment.