From 31ee578f5fdcef5112d49fdc50d3aa4d7116cfd9 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 8 Nov 2023 18:10:27 +0100 Subject: [PATCH] add: program to generate the README.md from README_COMPLET.md --- .gitignore | 1 + create_short.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ create_short.sh | 2 ++ 3 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 create_short.c create mode 100755 create_short.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8305e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/create_short.c b/create_short.c new file mode 100644 index 0000000..0833cf1 --- /dev/null +++ b/create_short.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + +#define BUTTON "\n### [More](https://urlz.fr/mqBB)" +#define TOTAL_SIZE 2272 +#define MAX_SIZE TOTAL_SIZE - strlen(BUTTON) + +char* read_file(const char* path) +{ + char* content; + int fd; + + fd = open(path, O_RDONLY); + if (fd == -1) + goto open_fail; + + content = malloc((TOTAL_SIZE + 1) * sizeof(char)); + if (content == NULL) + goto alloc_fail; + + int written = read(fd, content, MAX_SIZE); + if (written == -1) + goto read_fail; + + content[written] = '\0'; + + close(fd); + return content; + + read_fail: + alloc_fail: + free(content); + + open_fail: + close(fd); + return NULL; +} + +int write_file(const char* path, const char* content) +{ + int fd = open(path, O_WRONLY | O_TRUNC); + + if (fd == -1) + return 1; + + return write(fd, content, strlen(content)); +} + +int main(int ac, char **av) +{ + char* path; + + if (ac > 2) + path = av[1]; + else + path = "README_COMPLET.md"; + + char* content = read_file(path); + if (content == NULL) + return 1; + + strcat(content, BUTTON); + + int ret = write_file("README.md", content); + free(content); + return ret; +} \ No newline at end of file diff --git a/create_short.sh b/create_short.sh new file mode 100755 index 0000000..b047cfb --- /dev/null +++ b/create_short.sh @@ -0,0 +1,2 @@ +gcc -g -Wall -Werror -Wextra create_short.c && +./a.out \ No newline at end of file