console_line #7
@ -18,21 +18,42 @@
|
|||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "console_line.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.
|
* Get the logged in user's username.
|
||||||
* @return Returns the logged in user's username.
|
* @return Returns the logged in user's username.
|
||||||
*/
|
*/
|
||||||
char *get_user() {
|
char *get_username() {
|
||||||
struct passwd *pass;
|
struct passwd *pass;
|
||||||
pass = getpwuid(getuid());
|
pass = getpwuid(getuid());
|
||||||
return pass->pw_name;
|
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.
|
* Get the current working directory of the shell.
|
||||||
* @return Returns the current working directory.
|
* @return Returns the current working directory.
|
||||||
@ -53,9 +74,10 @@ char *get_working_directory() {
|
|||||||
* Print the console line before the user input.
|
* Print the console line before the user input.
|
||||||
*/
|
*/
|
||||||
void print_input_line() {
|
void print_input_line() {
|
||||||
char *name = get_user();
|
char *username = get_username();
|
||||||
|
char *hostname = get_hostname();
|
||||||
char *cwd = get_working_directory();
|
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);
|
free(cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,5 +108,6 @@ char *get_console_input() {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
remove_new_line(line);
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,11 @@
|
|||||||
#define _MYSHELLIN_CONSOLE_LINE
|
#define _MYSHELLIN_CONSOLE_LINE
|
||||||
#define CONSOLE_BUFFER_SIZE 1024
|
#define CONSOLE_BUFFER_SIZE 1024
|
||||||
|
|
||||||
char *get_user();
|
void remove_new_line(char *line);
|
||||||
|
|
||||||
|
char *get_username();
|
||||||
|
|
||||||
|
char *get_hostname();
|
||||||
|
|
||||||
char *get_working_directory();
|
char *get_working_directory();
|
||||||
|
|
||||||
|
@ -15,7 +15,5 @@
|
|||||||
|
|
||||||
#ifndef _MYSHELLIN_LOOP
|
#ifndef _MYSHELLIN_LOOP
|
||||||
#define _MYSHELLIN_LOOP
|
#define _MYSHELLIN_LOOP
|
||||||
void remove_new_line(char *line);
|
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
#endif
|
#endif
|
||||||
|
10
src/loop.c
10
src/loop.c
@ -21,14 +21,6 @@
|
|||||||
#include "builtins.h"
|
#include "builtins.h"
|
||||||
#include "console_line.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.
|
* This is the loop that checks for user input and acts on it.
|
||||||
*/
|
*/
|
||||||
@ -38,8 +30,6 @@ void loop() {
|
|||||||
|
|
||||||
char *line = get_console_input();
|
char *line = get_console_input();
|
||||||
|
|
||||||
remove_new_line(line);
|
|
||||||
|
|
||||||
StringArray args;
|
StringArray args;
|
||||||
create_string_array(&args);
|
create_string_array(&args);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user