1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-09-20 04:24:48 +00:00

Move environ functions to separate file

This commit is contained in:
Matthew Kilgore 2024-02-13 01:13:50 -05:00
parent def3ada041
commit 533934fb16
5 changed files with 117 additions and 101 deletions

View file

@ -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;

View file

@ -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

View file

@ -0,0 +1,9 @@
#pragma once
#include <stdint.h>
#include "qbs.h"
int32_t func__environcount();
qbs *func_environ(qbs *name);
qbs *func_environ(int32_t number);
void sub_environ(qbs *str);

View file

@ -0,0 +1,106 @@
#include "libqb-common.h"
#include <stdlib.h>
#include <string.h>
#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);
}

View file

@ -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 *);