console_line #7

Merged
cromer merged 7 commits from getchar into master 2021-06-26 23:15:01 -04:00
4 changed files with 31 additions and 16 deletions
Showing only changes of commit 8776bbd6fa - Show all commits

View File

@ -18,21 +18,42 @@
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#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;
}

View File

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

View File

@ -15,7 +15,5 @@
#ifndef _MYSHELLIN_LOOP
#define _MYSHELLIN_LOOP
void remove_new_line(char *line);
void loop();
#endif

View File

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