2021-06-19 20:25:27 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Christopher Cromer
|
|
|
|
* Copyright 2021 Raúl Hernandez
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-06-19 22:58:57 -04:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <limits.h>
|
2021-06-19 20:05:20 -04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
2021-06-19 22:58:57 -04:00
|
|
|
#include <stdlib.h>
|
2021-06-26 23:05:27 -04:00
|
|
|
#include <string.h>
|
2021-06-19 22:58:57 -04:00
|
|
|
#include <sys/types.h>
|
2021-06-19 20:05:20 -04:00
|
|
|
#include <unistd.h>
|
2021-06-20 12:02:38 -04:00
|
|
|
#include "color.h"
|
2021-06-26 22:38:22 -04:00
|
|
|
#include "console_line.h"
|
2021-06-19 20:05:20 -04:00
|
|
|
|
2021-06-26 23:05:27 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2021-06-19 23:10:20 -04:00
|
|
|
/**
|
|
|
|
* Get the logged in user's username.
|
|
|
|
* @return Returns the logged in user's username.
|
|
|
|
*/
|
2021-06-26 23:05:27 -04:00
|
|
|
char *get_username() {
|
2021-06-19 20:05:20 -04:00
|
|
|
struct passwd *pass;
|
|
|
|
pass = getpwuid(getuid());
|
2021-06-27 00:17:03 -04:00
|
|
|
if (pass == NULL) {
|
|
|
|
perror("getpwuid");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-06-19 20:05:20 -04:00
|
|
|
return pass->pw_name;
|
|
|
|
}
|
2021-06-19 22:58:57 -04:00
|
|
|
|
2021-06-26 23:05:27 -04:00
|
|
|
/**
|
|
|
|
* 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));
|
2021-06-26 23:13:51 -04:00
|
|
|
if (result == NULL) {
|
2021-06-27 00:17:03 -04:00
|
|
|
perror("malloc");
|
2021-06-26 23:13:51 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-06-26 23:05:27 -04:00
|
|
|
strcpy(result, hostname);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-19 23:10:20 -04:00
|
|
|
/**
|
|
|
|
* Get the current working directory of the shell.
|
|
|
|
* @return Returns the current working directory.
|
|
|
|
*/
|
2021-06-19 22:58:57 -04:00
|
|
|
char *get_working_directory() {
|
|
|
|
char *cwd = NULL;
|
|
|
|
cwd = getcwd(NULL, PATH_MAX);
|
2021-06-27 00:17:03 -04:00
|
|
|
if (cwd == NULL) {
|
|
|
|
perror("getcwd");
|
2021-06-19 22:58:57 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-06-27 00:17:03 -04:00
|
|
|
return cwd;
|
2021-06-19 22:58:57 -04:00
|
|
|
}
|
|
|
|
|
2021-06-19 23:10:20 -04:00
|
|
|
/**
|
|
|
|
* Print the console line before the user input.
|
|
|
|
*/
|
2021-06-19 22:58:57 -04:00
|
|
|
void print_input_line() {
|
2021-06-26 23:05:27 -04:00
|
|
|
char *username = get_username();
|
|
|
|
char *hostname = get_hostname();
|
2021-06-19 22:58:57 -04:00
|
|
|
char *cwd = get_working_directory();
|
2021-06-27 15:44:45 -04:00
|
|
|
printf(BRIGHT_GREEN "%s" RESET BRIGHT_MAGENTA "@" RESET BRIGHT_RED "%s" RESET BRIGHT_MAGENTA ":" RESET BRIGHT_BLUE "%s" RESET BRIGHT_MAGENTA "$ " RESET, username, hostname, cwd);
|
2021-07-06 15:00:57 -04:00
|
|
|
if (hostname != NULL) {
|
|
|
|
free(hostname);
|
|
|
|
hostname = NULL;
|
|
|
|
}
|
|
|
|
if (cwd != NULL) {
|
|
|
|
free(cwd);
|
|
|
|
cwd = NULL;
|
|
|
|
}
|
2021-06-19 22:58:57 -04:00
|
|
|
}
|
2021-06-26 22:38:22 -04:00
|
|
|
|
2021-06-26 22:48:19 -04:00
|
|
|
/**
|
|
|
|
* Get input from the console.
|
|
|
|
* @return Returns a string input by the user.
|
|
|
|
*/
|
2021-06-26 22:38:22 -04:00
|
|
|
char *get_console_input() {
|
|
|
|
size_t buffer_size = 0;
|
|
|
|
char *line = NULL;
|
|
|
|
|
|
|
|
if (getline(&line, &buffer_size, stdin) == -1) {
|
|
|
|
if (feof(stdin)) {
|
|
|
|
// the stdin was closed, this usually happens for CTRL-D
|
|
|
|
printf("\n");
|
|
|
|
if (line != NULL) {
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
else {
|
2021-06-27 00:17:03 -04:00
|
|
|
perror("getline");
|
2021-06-26 22:38:22 -04:00
|
|
|
if (line != NULL) {
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 23:05:27 -04:00
|
|
|
remove_new_line(line);
|
2021-06-26 22:38:22 -04:00
|
|
|
return line;
|
|
|
|
}
|