From ee1dff912b15d04879bc1f2d59ef5d4e89b19476 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Tue, 6 Aug 2024 22:49:43 +0300 Subject: [PATCH] downgrade POSIX C source version to 2001-12 Implement `strdup` to not require version 2008-09 --- Makefile | 2 +- include/common.h | 1 + include/strdup.h | 11 +++++++++++ src/main.c | 1 + src/strdup.c | 17 +++++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 include/strdup.h create mode 100644 src/strdup.c diff --git a/Makefile b/Makefile index d68f602..2ad028f 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC ?= gcc DEBUG ?= 0 CFLAGS = -Wall -Werror -Wextra -Wpedantic -std=c99 -Ofast \ --D_POSIX_C_SOURCE=200809L +-D_POSIX_C_SOURCE=200112L ifeq ($(DEBUG), 1) CFLAGS += -g -DDEBUG diff --git a/include/common.h b/include/common.h index 065ab4b..9660199 100644 --- a/include/common.h +++ b/include/common.h @@ -6,6 +6,7 @@ #include #include #include +#include /* enums */ enum configurations_bitshift { diff --git a/include/strdup.h b/include/strdup.h new file mode 100644 index 0000000..09ee248 --- /dev/null +++ b/include/strdup.h @@ -0,0 +1,11 @@ +#ifndef _STRDUP_H +#define _STRDUP_H + +#include +#include +#include + +/* functions definitions */ +char* strdup(const char* s); + +#endif /* _STRDUP_H */ diff --git a/src/main.c b/src/main.c index 1c98ab0..6d951f7 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ #include "atoumax_base10.h" #include "corrupter.h" #include "file_type.h" +#include "strdup.h" /* enums */ enum arg_destinations { diff --git a/src/strdup.c b/src/strdup.c new file mode 100644 index 0000000..2c7ee5f --- /dev/null +++ b/src/strdup.c @@ -0,0 +1,17 @@ +#include "strdup.h" + +/* function implementations */ +char* strdup(const char* s) { + if (s == NULL) { + errno = EINVAL; + return NULL; + } + + char* copy = malloc((strlen(s) + 1) * sizeof(char)); + if (copy == NULL) + return NULL; + + strcpy(copy, s); + + return copy; +}