diff --git a/libc/inc/io/ansi_colors.h b/libc/inc/io/ansi_colors.h index 193eada..ec518ee 100644 --- a/libc/inc/io/ansi_colors.h +++ b/libc/inc/io/ansi_colors.h @@ -1,5 +1,4 @@ /// @file ansi_colors.h -/// @author Enrico Fraccaroli (enry.frak@gmail.com) /// @brief List of ANSI colors. /// @copyright (c) 2014-2023 This file is distributed under the MIT License. /// See LICENSE.md for details. diff --git a/libc/src/string.c b/libc/src/string.c index 8c70eb9..a8a8f16 100644 --- a/libc/src/string.c +++ b/libc/src/string.c @@ -384,18 +384,15 @@ char *strtok_r(char *str, const char *delim, char **saveptr) * #pragma function(strset) */ -void *memset(void *ptr, int value, size_t num) +void *memset(void *ptr, int c, size_t n) { - // Truncate c to 8 bits. - value = (value & 0xFF); - - char *dst = (char *)ptr; - - // Initialize the rest of the size. - while (num--) { - *dst++ = (char)value; - } - + // Turn the pointer into a char * pointer. Here, we use the volatile keyword + // to prevent the compiler from optimizing away the operations involving the + // pointer. + unsigned char volatile *dst = (unsigned char volatile *)ptr; + // Initialize the content of the memory. + while (n--) *dst++ = (unsigned char)c; + // Return the pointer. return ptr; } diff --git a/programs/ansi_colors.h b/programs/ansi_colors.h deleted file mode 100644 index 00e9d54..0000000 --- a/programs/ansi_colors.h +++ /dev/null @@ -1,62 +0,0 @@ -/// @file ansi_colors.h -/// @brief Macros for ANSI colors. -/// @copyright (c) 2014-2023 This file is distributed under the MIT License. -/// See LICENSE.md for details. - -#pragma once - -#define FG_RESET "\033[0m" ///< ANSI code for resetting. - -#define FG_BLACK "\033[30m" ///< ANSI code for setting a BLACK foreground. -#define FG_RED "\033[31m" ///< ANSI code for setting a RED foreground. -#define FG_GREEN "\033[32m" ///< ANSI code for setting a GREEN foreground. -#define FG_YELLOW "\033[33m" ///< ANSI code for setting a YELLOW foreground. -#define FG_BLUE "\033[34m" ///< ANSI code for setting a BLUE foreground. -#define FG_MAGENTA "\033[35m" ///< ANSI code for setting a MAGENTA foreground. -#define FG_CYAN "\033[36m" ///< ANSI code for setting a CYAN foreground. -#define FG_WHITE "\033[37m" ///< ANSI code for setting a WHITE foreground. - -#define FG_BLACK_BOLD "\033[1;30m" ///< ANSI code for setting a BLACK foreground. -#define FG_RED_BOLD "\033[1;31m" ///< ANSI code for setting a RED foreground. -#define FG_GREEN_BOLD "\033[1;32m" ///< ANSI code for setting a GREEN foreground. -#define FG_YELLOW_BOLD "\033[1;33m" ///< ANSI code for setting a YELLOW foreground. -#define FG_BLUE_BOLD "\033[1;34m" ///< ANSI code for setting a BLUE foreground. -#define FG_MAGENTA_BOLD "\033[1;35m" ///< ANSI code for setting a MAGENTA foreground. -#define FG_CYAN_BOLD "\033[1;36m" ///< ANSI code for setting a CYAN foreground. -#define FG_WHITE_BOLD "\033[1;37m" ///< ANSI code for setting a WHITE foreground. - -#define FG_BLACK_BRIGHT "\033[90m" ///< ANSI code for setting a BRIGHT_BLACK foreground. -#define FG_RED_BRIGHT "\033[91m" ///< ANSI code for setting a BRIGHT_RED foreground. -#define FG_GREEN_BRIGHT "\033[92m" ///< ANSI code for setting a BRIGHT_GREEN foreground. -#define FG_YELLOW_BRIGHT "\033[93m" ///< ANSI code for setting a BRIGHT_YELLOW foreground. -#define FG_BLUE_BRIGHT "\033[94m" ///< ANSI code for setting a BRIGHT_BLUE foreground. -#define FG_MAGENTA_BRIGHT "\033[95m" ///< ANSI code for setting a BRIGHT_MAGENTA foreground. -#define FG_CYAN_BRIGHT "\033[96m" ///< ANSI code for setting a BRIGHT_CYAN foreground. -#define FG_WHITE_BRIGHT "\033[97m" ///< ANSI code for setting a BRIGHT_WHITE foreground. - -#define FG_BLACK_BRIGHT_BOLD "\033[1;90m" ///< ANSI code for setting a BRIGHT_BLACK foreground. -#define FG_RED_BRIGHT_BOLD "\033[1;91m" ///< ANSI code for setting a BRIGHT_RED foreground. -#define FG_GREEN_BRIGHT_BOLD "\033[1;92m" ///< ANSI code for setting a BRIGHT_GREEN foreground. -#define FG_YELLOW_BRIGHT_BOLD "\033[1;93m" ///< ANSI code for setting a BRIGHT_YELLOW foreground. -#define FG_BLUE_BRIGHT_BOLD "\033[1;94m" ///< ANSI code for setting a BRIGHT_BLUE foreground. -#define FG_MAGENTA_BRIGHT_BOLD "\033[1;95m" ///< ANSI code for setting a BRIGHT_MAGENTA foreground. -#define FG_CYAN_BRIGHT_BOLD "\033[1;96m" ///< ANSI code for setting a BRIGHT_CYAN foreground. -#define FG_WHITE_BRIGHT_BOLD "\033[1;97m" ///< ANSI code for setting a BRIGHT_WHITE foreground. - -#define BG_BLACK "\033[40m" ///< ANSI code for setting a BLACK background. -#define BG_RED "\033[41m" ///< ANSI code for setting a RED background. -#define BG_GREEN "\033[42m" ///< ANSI code for setting a GREEN background. -#define BG_YELLOW "\033[43m" ///< ANSI code for setting a YELLOW background. -#define BG_BLUE "\033[44m" ///< ANSI code for setting a BLUE background. -#define BG_MAGENTA "\033[45m" ///< ANSI code for setting a MAGENTA background. -#define BG_CYAN "\033[46m" ///< ANSI code for setting a CYAN background. -#define BG_WHITE "\033[47m" ///< ANSI code for setting a WHITE background. - -#define BG_BRIGHT_BLACK "\033[100m" ///< ANSI code for setting a BRIGHT_BLACK background. -#define BG_BRIGHT_RED "\033[101m" ///< ANSI code for setting a BRIGHT_RED background. -#define BG_BRIGHT_GREEN "\033[102m" ///< ANSI code for setting a BRIGHT_GREEN background. -#define BG_BRIGHT_YELLOW "\033[103m" ///< ANSI code for setting a BRIGHT_YELLOW background. -#define BG_BRIGHT_BLUE "\033[104m" ///< ANSI code for setting a BRIGHT_BLUE background. -#define BG_BRIGHT_MAGENTA "\033[105m" ///< ANSI code for setting a BRIGHT_MAGENTA background. -#define BG_BRIGHT_CYAN "\033[106m" ///< ANSI code for setting a BRIGHT_CYAN background. -#define BG_BRIGHT_WHITE "\033[107m" ///< ANSI code for setting a BRIGHT_WHITE background. diff --git a/programs/login.c b/programs/login.c index c954434..150f7fb 100644 --- a/programs/login.c +++ b/programs/login.c @@ -15,8 +15,7 @@ #include #include #include - -#include "ansi_colors.h" +#include /// Maximum length of credentials. #define CREDENTIALS_LENGTH 50 @@ -52,7 +51,7 @@ static inline void __set_io_flags(unsigned flag, bool_t active) tcsetattr(STDIN_FILENO, 0, &_termios); } -static inline void __print_message_file(const char * file) +static inline void __print_message_file(const char *file) { char buffer[256]; ssize_t nbytes, total = 0; @@ -159,12 +158,12 @@ int main(int argc, char **argv) // Get the username. do { printf("Username :"); - } while (!__get_input(username, CREDENTIALS_LENGTH, false)); + } while (!__get_input(username, sizeof(username), false)); // Get the password. do { printf("Password :"); - } while (!__get_input(password, CREDENTIALS_LENGTH, true)); + } while (!__get_input(password, sizeof(password), true)); // Check if we can find the user. if ((pwd = getpwnam(username)) == NULL) { diff --git a/programs/shell.c b/programs/shell.c index 740e5ca..0a78d91 100644 --- a/programs/shell.c +++ b/programs/shell.c @@ -10,18 +10,18 @@ #include #include #include +#include #include -#include "stdbool.h" -#include "stddef.h" -#include "string.h" -#include "stdio.h" -#include "stdlib.h" -#include "strerror.h" -#include "termios.h" -#include "limits.h" -#include "sys/utsname.h" -#include "ctype.h" -#include "ansi_colors.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /// Maximum length of commands. #define CMD_LEN 32