From 533934fb16779336cb73e861e613206ef78a55fc Mon Sep 17 00:00:00 2001 From: Matthew Kilgore Date: Tue, 13 Feb 2024 01:13:50 -0500 Subject: [PATCH] Move environ functions to separate file --- internal/c/libqb.cpp | 97 -------------------------- internal/c/libqb/build.mk | 1 + internal/c/libqb/include/environ.h | 9 +++ internal/c/libqb/src/environ.cpp | 106 +++++++++++++++++++++++++++++ internal/c/qbx.cpp | 5 +- 5 files changed, 117 insertions(+), 101 deletions(-) create mode 100644 internal/c/libqb/include/environ.h create mode 100644 internal/c/libqb/src/environ.cpp diff --git a/internal/c/libqb.cpp b/internal/c/libqb.cpp index 13e26afc0..14cab5a7f 100644 --- a/internal/c/libqb.cpp +++ b/internal/c/libqb.cpp @@ -24829,103 +24829,6 @@ udlr: goto nextchar; } -#ifdef QB64_WINDOWS -# define envp _environ -#else -extern char **environ; -# define envp environ -#endif - -int32 func__environcount() { - // count array bound - char **p = envp; - while (*++p) - ; - return p - envp; -} - -qbs *func_environ(qbs *name) { - char *query, *result; - qbs *tqbs; - query = (char *)malloc(name->len + 1); - query[name->len] = '\0'; // add NULL terminator - memcpy(query, name->chr, name->len); - result = getenv(query); - if (result) { - int result_length = strlen(result); - tqbs = qbs_new(result_length, 1); - memcpy(tqbs->chr, result, result_length); - } else { - tqbs = qbs_new(0, 1); - } - return tqbs; -} - -qbs *func_environ(int32 number) { - char *result; - qbs *tqbs; - int result_length; - if (number <= 0) { - tqbs = qbs_new(0, 1); - error(5); - return tqbs; - } - // Check we do not go beyond array bound - char **p = envp; - while (*++p) - ; - if (number > p - envp) { - tqbs = qbs_new(0, 1); - return tqbs; - } - result = envp[number - 1]; - result_length = strlen(result); - tqbs = qbs_new(result_length, 1); - memcpy(tqbs->chr, result, result_length); - return tqbs; -} - -void sub_environ(qbs *str) { - char *buf; - char *separator; - buf = (char *)malloc(str->len + 1); - buf[str->len] = '\0'; - memcpy(buf, str->chr, str->len); - // Name and value may be separated by = or space - separator = strchr(buf, ' '); - if (!separator) { - separator = strchr(buf, '='); - } - if (!separator) { - // It is an error is there is no separator - error(5); - return; - } - // Split into two separate strings - *separator = '\0'; - if (separator == &buf[str->len] - 1) { - // Separator is at end of string, so remove the variable -#ifdef QB64_WINDOWS - *separator = '='; - _putenv(buf); -#else - unsetenv(buf); -#endif - } else { -#ifdef QB64_WINDOWS -# if WINVER >= 0x0600 - _putenv_s(buf, separator + 1); -# else - *separator = '='; - _putenv(buf); -# endif -#else - setenv(buf, separator + 1, 1); -#endif - } - free(buf); -} - #ifdef QB64_WINDOWS void showvalue(__int64 v) { static qbs *s = NULL; diff --git a/internal/c/libqb/build.mk b/internal/c/libqb/build.mk index 9d4100c24..1e4f62f59 100644 --- a/internal/c/libqb/build.mk +++ b/internal/c/libqb/build.mk @@ -3,6 +3,7 @@ libqb-objs-y += $(PATH_LIBQB)/src/threading.o libqb-objs-y += $(PATH_LIBQB)/src/buffer.o libqb-objs-y += $(PATH_LIBQB)/src/bitops.o libqb-objs-y += $(PATH_LIBQB)/src/command.o +libqb-objs-y += $(PATH_LIBQB)/src/environ.o libqb-objs-y += $(PATH_LIBQB)/src/filepath.o libqb-objs-y += $(PATH_LIBQB)/src/filesystem.o libqb-objs-y += $(PATH_LIBQB)/src/datetime.o diff --git a/internal/c/libqb/include/environ.h b/internal/c/libqb/include/environ.h new file mode 100644 index 000000000..f398015a1 --- /dev/null +++ b/internal/c/libqb/include/environ.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include "qbs.h" + +int32_t func__environcount(); +qbs *func_environ(qbs *name); +qbs *func_environ(int32_t number); +void sub_environ(qbs *str); diff --git a/internal/c/libqb/src/environ.cpp b/internal/c/libqb/src/environ.cpp new file mode 100644 index 000000000..8bcfa42fd --- /dev/null +++ b/internal/c/libqb/src/environ.cpp @@ -0,0 +1,106 @@ + +#include "libqb-common.h" + +#include +#include + +#include "error_handle.h" +#include "qbs.h" +#include "environ.h" + +#ifdef QB64_WINDOWS +# define envp _environ +#else +extern char **environ; +# define envp environ +#endif + +int32_t func__environcount() { + // count array bound + char **p = envp; + while (*++p) + ; + return p - envp; +} + +qbs *func_environ(qbs *name) { + char *query, *result; + qbs *tqbs; + query = (char *)malloc(name->len + 1); + query[name->len] = '\0'; // add NULL terminator + memcpy(query, name->chr, name->len); + result = getenv(query); + if (result) { + int result_length = strlen(result); + tqbs = qbs_new(result_length, 1); + memcpy(tqbs->chr, result, result_length); + } else { + tqbs = qbs_new(0, 1); + } + return tqbs; +} + +qbs *func_environ(int32_t number) { + char *result; + qbs *tqbs; + int result_length; + if (number <= 0) { + tqbs = qbs_new(0, 1); + error(5); + return tqbs; + } + // Check we do not go beyond array bound + char **p = envp; + while (*++p) + ; + if (number > p - envp) { + tqbs = qbs_new(0, 1); + return tqbs; + } + result = envp[number - 1]; + result_length = strlen(result); + tqbs = qbs_new(result_length, 1); + memcpy(tqbs->chr, result, result_length); + return tqbs; +} + +void sub_environ(qbs *str) { + char *buf; + char *separator; + buf = (char *)malloc(str->len + 1); + buf[str->len] = '\0'; + memcpy(buf, str->chr, str->len); + // Name and value may be separated by = or space + separator = strchr(buf, ' '); + if (!separator) { + separator = strchr(buf, '='); + } + if (!separator) { + // It is an error is there is no separator + error(5); + return; + } + // Split into two separate strings + *separator = '\0'; + if (separator == &buf[str->len] - 1) { + // Separator is at end of string, so remove the variable +#ifdef QB64_WINDOWS + *separator = '='; + _putenv(buf); +#else + unsetenv(buf); +#endif + } else { +#ifdef QB64_WINDOWS +# if WINVER >= 0x0600 + _putenv_s(buf, separator + 1); +# else + *separator = '='; + _putenv(buf); +# endif +#else + setenv(buf, separator + 1, 1); +#endif + } + free(buf); +} diff --git a/internal/c/qbx.cpp b/internal/c/qbx.cpp index 7512ec0dc..eebb58deb 100755 --- a/internal/c/qbx.cpp +++ b/internal/c/qbx.cpp @@ -4,6 +4,7 @@ #include "compression.h" #include "command.h" #include "datetime.h" +#include "environ.h" #include "event.h" #include "extended_math.h" #include "filepath.h" @@ -210,10 +211,6 @@ extern int32 func__openconnection(int32); extern int32 func__openclient(qbs *); extern int32 func__connected(int32); extern qbs *func__connectionaddress(int32); -extern int32 func__environcount(); -extern qbs *func_environ(qbs *); -extern qbs *func_environ(int32); -extern void sub_environ(qbs *); extern void sub_draw(qbs *); extern void qbs_maketmp(qbs *); extern void sub_run(qbs *);