From e178f4d504e97d31f4c9b210717cb81e0cb195e2 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Tue, 21 Nov 2023 13:51:05 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20:=20impl=C3=A9mentation=20C=20de=20mon?= =?UTF-8?q?=20programme=20separator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/separator.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/separator.c diff --git a/src/separator.c b/src/separator.c new file mode 100644 index 0000000..575011c --- /dev/null +++ b/src/separator.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +#define println() printf("\n") +#define printsep(sep) printf("%s\n", sep) + +static char *separation_character_line(const char *separation_character, const unsigned short terminal_width) { + const unsigned short line_length = terminal_width / 3; + const size_t character_length = strlen(separation_character); + + char *line = malloc(line_length * sizeof(char)); + + while (strlen(line) + character_length <= line_length) { + strncat(line, separation_character, character_length); + } + + return line; +} + +static unsigned short terminal_width() { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + return w.ws_col; +} + +int main(int argc, char *argv[]) { + // Parsing arguments + char *name; + char *separation_character; + + if (argc >= 2) { + name = argv[1]; + } else { + name = ""; + } + + if (argc >= 3) { + separation_character = argv[2]; + } else { + separation_character = "="; + } + + // Creating a /3 terminal size character line + char *separation_line = separation_character_line(separation_character, terminal_width()); + + // Printing the separator + println(); + + printsep(separation_line); + + if (name != NULL) { + printf("\t%s\n", name); + } else { + println(); + } + + printsep(separation_line); + + // Freeing the created character line + free(separation_line); + + // Returning with success + return EXIT_SUCCESS; +}