refactor utils

This commit is contained in:
Chris Cromer 2021-07-17 17:36:38 -04:00
parent 71d74c3324
commit f867a46565
7 changed files with 69 additions and 28 deletions

View File

@ -2,7 +2,7 @@ CC=clang
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c11
LDFLAGS=
FILENAME=myshellin
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c src/utils.c
OBJ=$(SRC:.c=.o)
all: myshellin

View File

@ -20,7 +20,7 @@
#include <unistd.h>
#include "array.h"
#include "builtins.h"
#include "console_line.h"
#include "utils.h"
bool is_builtin(char *command) {
if (strcmp(command, "exit") == 0) {

View File

@ -24,10 +24,7 @@
#include "array.h"
#include "color.h"
#include "console_line.h"
void remove_new_line(char* line) {
line[strcspn(line, "\n")] = 0;
}
#include "utils.h"
char *get_username() {
struct passwd *pass = NULL;
@ -51,16 +48,6 @@ char *get_hostname() {
return result;
}
char *get_working_directory() {
char *cwd = malloc(PATH_MAX * sizeof(char *));
getcwd(cwd, PATH_MAX);
if (cwd == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
}
return cwd;
}
void print_input_line() {
char *username = get_username();
char *hostname = get_hostname();

View File

@ -16,12 +16,6 @@
#ifndef _MYSHELLIN_CONSOLE_LINE
#define _MYSHELLIN_CONSOLE_LINE
/**
* 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);
/**
* Get the logged in user's username.
* @return Returns the logged in user's username.
@ -34,12 +28,6 @@ char *get_username();
*/
char *get_hostname();
/**
* Get the current working directory of the shell.
* @return Returns the current working directory.
*/
char *get_working_directory();
/**
* Print the console line before the user input.
*/

30
src/include/utils.h Normal file
View File

@ -0,0 +1,30 @@
/*
* 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.
*/
#ifndef _MYSHELLIN_UTILS
#define _MYSHELLIN_UTILS
/**
* 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);
/**
* Get the current working directory of the shell.
* @return Returns the current working directory.
*/
char *get_working_directory();
#endif

View File

@ -21,6 +21,7 @@
#include "builtins.h"
#include "console_line.h"
#include "launch.h"
#include "utils.h"
void add_to_cleanup(void *data) {
clean.array = realloc(clean.array, (clean.size + 1) * sizeof(void *));

35
src/utils.c Normal file
View File

@ -0,0 +1,35 @@
/*
* 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.
*/
#define _GNU_SOURCE
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void remove_new_line(char* line) {
line[strcspn(line, "\n")] = 0;
}
char *get_working_directory() {
char *cwd = malloc(PATH_MAX * sizeof(char *));
getcwd(cwd, PATH_MAX);
if (cwd == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
}
return cwd;
}