2023-11-21 13:51:05 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#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));
|
2023-11-23 10:51:53 +01:00
|
|
|
// Avoiding undefined behaviour with strlen if no null byte is
|
|
|
|
// present at the end of line
|
|
|
|
*line = 0;
|
2023-11-21 13:51:05 +01:00
|
|
|
|
|
|
|
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 = "";
|
|
|
|
}
|
|
|
|
|
2023-11-23 10:54:08 +01:00
|
|
|
if (argc >= 3 && *argv[2] != 0) {
|
2023-11-21 13:51:05 +01:00
|
|
|
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;
|
|
|
|
}
|