From 51c92027e398942c143adb51ceefc066c3dd6e16 Mon Sep 17 00:00:00 2001 From: savashn Date: Sun, 8 Jun 2025 22:53:50 +0300 Subject: [PATCH 1/2] fix mingw64 compatibility issue --- src/dotenv.c | 74 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/src/dotenv.c b/src/dotenv.c index fea0b81..2fdbb0b 100644 --- a/src/dotenv.c +++ b/src/dotenv.c @@ -1,19 +1,25 @@ #include "dotenv.h" #include -#include +#include +#include #include #include -#if (defined(_WIN32) || defined(_MSC_VER)) && !defined(__MINGW32__) +#if defined(_WIN32) + +#ifdef _MSC_VER +#define strdup _strdup +#endif -#include #define strtok_r strtok_s -int setenv(const char *name, const char *value, int overwrite) { +int setenv(const char *name, const char *value, int overwrite) +{ int errcode = 0; - if (!overwrite) { + if (!overwrite) + { size_t envsize = 0; - errcode = getenv_s(&envsize, NULL, 0, name); + errcode = getenv_s(&envsize, NULL, 0, name); if (errcode || envsize) return errcode; } @@ -21,12 +27,14 @@ int setenv(const char *name, const char *value, int overwrite) { } // https://dev.w3.org/libwww/Library/src/vms/getline.c -int getline(char **lineptr, size_t *n, FILE *stream) { - static char line[256]; - char *ptr; +int getline(char **lineptr, size_t *n, FILE *stream) +{ + static char line[256]; + char *ptr; unsigned int len; - if (lineptr == NULL || n == NULL) { + if (lineptr == NULL || n == NULL) + { errno = EINVAL; return -1; } @@ -45,12 +53,13 @@ int getline(char **lineptr, size_t *n, FILE *stream) { len = strlen(line); - if ((len + 1) < 256) { + if ((len + 1) < 256) + { ptr = realloc(*lineptr, 256); if (ptr == NULL) return (-1); *lineptr = ptr; - *n = 256; + *n = 256; } strcpy(*lineptr, line); @@ -64,13 +73,14 @@ int getline(char **lineptr, size_t *n, FILE *stream) { #define remove_space(value) value + 1 - static char *concat(char *buffer, char *string) { - if (!buffer) { + if (!buffer) + { return strdup(string); } - if (string) { + if (string) + { size_t length = strlen(buffer) + strlen(string) + 1; char *new = realloc(buffer, length); @@ -104,12 +114,15 @@ static char *parse_value(char *value) char *search = value, *parsed = NULL, *tok_ptr; char *name; - if (value && is_nested(value)) { - while (1) { + if (value && is_nested(value)) + { + while (1) + { parsed = concat(parsed, strtok_r(search, "${", &tok_ptr)); name = strtok_r(NULL, "}", &tok_ptr); - if (!name) { + if (!name) + { break; } parsed = concat(parsed, getenv(remove_bracket(name))); @@ -124,13 +137,16 @@ static char *parse_value(char *value) static bool is_commented(char *line) { - if ('#' == line[0]) { + if ('#' == line[0]) + { return true; } int i = 0; - while (' ' == line[i]) { - if ('#' == line[++i]) { + while (' ' == line[i]) + { + if ('#' == line[++i]) + { return true; } } @@ -142,7 +158,8 @@ static void set_variable(char *name, char *original, bool overwrite) { char *parsed; - if (original) { + if (original) + { parsed = parse_value(original); setenv(name, remove_space(parsed), overwrite); @@ -155,8 +172,10 @@ static void parse(FILE *file, bool overwrite) char *name, *original, *line = NULL, *tok_ptr; size_t len = 0; - while (-1 != getline(&line, &len, file)) { - if (!is_commented(line)) { + while (-1 != getline(&line, &len, file)) + { + if (!is_commented(line)) + { name = strtok_r(line, "=", &tok_ptr); original = strtok_r(NULL, "\n", &tok_ptr); @@ -178,10 +197,12 @@ int env_load(const char *path, bool overwrite) { FILE *file = open_default(path); - if (!file) { + if (!file) + { file = fopen(path, "rb"); - if (!file) { + if (!file) + { return -1; } } @@ -190,4 +211,3 @@ int env_load(const char *path, bool overwrite) return 0; } - From 4bd790dc217a3383922cab7bc14e46f4dd26ae6b Mon Sep 17 00:00:00 2001 From: savashn Date: Thu, 12 Jun 2025 18:28:57 +0300 Subject: [PATCH 2/2] =?UTF-8?q?update=20code=20format=20and=20improve=20wi?= =?UTF-8?q?ndows=C2=A0portability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dotenv.c | 68 +++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/src/dotenv.c b/src/dotenv.c index 2fdbb0b..4fd3eee 100644 --- a/src/dotenv.c +++ b/src/dotenv.c @@ -7,19 +7,16 @@ #if defined(_WIN32) -#ifdef _MSC_VER +#if defined(_MSC_VER) +#define strtok_r strtok_s #define strdup _strdup #endif -#define strtok_r strtok_s - -int setenv(const char *name, const char *value, int overwrite) -{ +int setenv(const char *name, const char *value, int overwrite) { int errcode = 0; - if (!overwrite) - { + if (!overwrite) { size_t envsize = 0; - errcode = getenv_s(&envsize, NULL, 0, name); + errcode = getenv_s(&envsize, NULL, 0, name); if (errcode || envsize) return errcode; } @@ -27,14 +24,12 @@ int setenv(const char *name, const char *value, int overwrite) } // https://dev.w3.org/libwww/Library/src/vms/getline.c -int getline(char **lineptr, size_t *n, FILE *stream) -{ - static char line[256]; - char *ptr; +int getline(char **lineptr, size_t *n, FILE *stream) { + static char line[256]; + char *ptr; unsigned int len; - if (lineptr == NULL || n == NULL) - { + if (lineptr == NULL || n == NULL) { errno = EINVAL; return -1; } @@ -53,13 +48,12 @@ int getline(char **lineptr, size_t *n, FILE *stream) len = strlen(line); - if ((len + 1) < 256) - { + if ((len + 1) < 256) { ptr = realloc(*lineptr, 256); if (ptr == NULL) return (-1); *lineptr = ptr; - *n = 256; + *n = 256; } strcpy(*lineptr, line); @@ -73,14 +67,13 @@ int getline(char **lineptr, size_t *n, FILE *stream) #define remove_space(value) value + 1 + static char *concat(char *buffer, char *string) { - if (!buffer) - { + if (!buffer) { return strdup(string); } - if (string) - { + if (string) { size_t length = strlen(buffer) + strlen(string) + 1; char *new = realloc(buffer, length); @@ -114,15 +107,12 @@ static char *parse_value(char *value) char *search = value, *parsed = NULL, *tok_ptr; char *name; - if (value && is_nested(value)) - { - while (1) - { + if (value && is_nested(value)) { + while (1) { parsed = concat(parsed, strtok_r(search, "${", &tok_ptr)); name = strtok_r(NULL, "}", &tok_ptr); - if (!name) - { + if (!name) { break; } parsed = concat(parsed, getenv(remove_bracket(name))); @@ -137,16 +127,13 @@ static char *parse_value(char *value) static bool is_commented(char *line) { - if ('#' == line[0]) - { + if ('#' == line[0]) { return true; } int i = 0; - while (' ' == line[i]) - { - if ('#' == line[++i]) - { + while (' ' == line[i]) { + if ('#' == line[++i]) { return true; } } @@ -158,8 +145,7 @@ static void set_variable(char *name, char *original, bool overwrite) { char *parsed; - if (original) - { + if (original) { parsed = parse_value(original); setenv(name, remove_space(parsed), overwrite); @@ -172,10 +158,8 @@ static void parse(FILE *file, bool overwrite) char *name, *original, *line = NULL, *tok_ptr; size_t len = 0; - while (-1 != getline(&line, &len, file)) - { - if (!is_commented(line)) - { + while (-1 != getline(&line, &len, file)) { + if (!is_commented(line)) { name = strtok_r(line, "=", &tok_ptr); original = strtok_r(NULL, "\n", &tok_ptr); @@ -197,12 +181,10 @@ int env_load(const char *path, bool overwrite) { FILE *file = open_default(path); - if (!file) - { + if (!file) { file = fopen(path, "rb"); - if (!file) - { + if (!file) { return -1; } }