From 8776bbd6fa701e5d15e6daec2856ab0949195738 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 26 Jun 2021 23:05:27 -0400 Subject: [PATCH] add hostname --- src/console_line.c | 29 ++++++++++++++++++++++++++--- src/include/console_line.h | 6 +++++- src/include/loop.h | 2 -- src/loop.c | 10 ---------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/console_line.c b/src/console_line.c index 58105e9..49301b2 100644 --- a/src/console_line.c +++ b/src/console_line.c @@ -18,21 +18,42 @@ #include #include #include +#include #include #include #include "color.h" #include "console_line.h" +/** + * Remove new line from the end of a string. + * @param line The string to remove the new line from. + */ +void remove_new_line(char* line) { + line[strcspn(line, "\n")] = 0; +} + /** * Get the logged in user's username. * @return Returns the logged in user's username. */ -char *get_user() { +char *get_username() { struct passwd *pass; pass = getpwuid(getuid()); return pass->pw_name; } +/** + * Get the hostname of the machine. + * @return Returns the hostname. + */ +char *get_hostname() { + char hostname[HOST_NAME_MAX + 1]; + gethostname(hostname, HOST_NAME_MAX + 1); + char *result = malloc((HOST_NAME_MAX + 1) * sizeof(char)); + strcpy(result, hostname); + return result; +} + /** * Get the current working directory of the shell. * @return Returns the current working directory. @@ -53,9 +74,10 @@ char *get_working_directory() { * Print the console line before the user input. */ void print_input_line() { - char *name = get_user(); + char *username = get_username(); + char *hostname = get_hostname(); char *cwd = get_working_directory(); - printf(BRIGHT_CYAN "%s" MAGENTA "@" RED "localhost" MAGENTA ":" BLUE "%s" MAGENTA "$ " RESET, name, cwd); + printf(BRIGHT_CYAN "%s" MAGENTA "@" RED "%s" MAGENTA ":" BLUE "%s" MAGENTA "$ " RESET, username, hostname, cwd); free(cwd); } @@ -86,5 +108,6 @@ char *get_console_input() { exit(EXIT_FAILURE); } } + remove_new_line(line); return line; } diff --git a/src/include/console_line.h b/src/include/console_line.h index 8071ce5..e1f85eb 100644 --- a/src/include/console_line.h +++ b/src/include/console_line.h @@ -17,7 +17,11 @@ #define _MYSHELLIN_CONSOLE_LINE #define CONSOLE_BUFFER_SIZE 1024 -char *get_user(); +void remove_new_line(char *line); + +char *get_username(); + +char *get_hostname(); char *get_working_directory(); diff --git a/src/include/loop.h b/src/include/loop.h index d60f051..0586ad7 100644 --- a/src/include/loop.h +++ b/src/include/loop.h @@ -15,7 +15,5 @@ #ifndef _MYSHELLIN_LOOP #define _MYSHELLIN_LOOP -void remove_new_line(char *line); - void loop(); #endif diff --git a/src/loop.c b/src/loop.c index 6721ec3..e7b6087 100644 --- a/src/loop.c +++ b/src/loop.c @@ -21,14 +21,6 @@ #include "builtins.h" #include "console_line.h" -/** - * Remove new line from the end of a string. - * @param line The string to remove the new line from. - */ -void remove_new_line(char* line) { - line[strcspn(line, "\n")] = 0; -} - /** * This is the loop that checks for user input and acts on it. */ @@ -38,8 +30,6 @@ void loop() { char *line = get_console_input(); - remove_new_line(line); - StringArray args; create_string_array(&args);