2015-06-21 17:33:46 +02:00
/*
Simple DirectMedia Layer
2025-01-01 07:45:41 -08:00
Copyright ( C ) 1997 - 2025 Sam Lantinga < slouken @ libsdl . org >
2015-06-21 17:33:46 +02:00
This software is provided ' as - is ' , without any express or implied
warranty . In no event will the authors be held liable for any damages
arising from the use of this software .
Permission is granted to anyone to use this software for any purpose ,
including commercial applications , and to alter it and redistribute it
freely , subject to the following restrictions :
1. The origin of this software must not be misrepresented ; you must not
claim that you wrote the original software . If you use this software
in a product , an acknowledgment in the product documentation would be
appreciated but is not required .
2. Altered source versions must be plainly marked as such , and must not be
misrepresented as being the original software .
3. This notice may not be removed or altered from any source distribution .
*/
2022-11-29 18:34:15 -08:00
# include "SDL_internal.h"
2015-06-21 17:33:46 +02:00
2024-09-18 14:34:14 -07:00
# include "SDL_getenv_c.h"
2024-06-04 16:38:11 -07:00
2024-09-27 11:33:00 +02:00
# if defined(SDL_PLATFORM_WINDOWS)
2015-06-21 17:33:46 +02:00
# include "../core/windows/SDL_windows.h"
# endif
2024-01-24 02:40:51 +01:00
# ifdef SDL_PLATFORM_ANDROID
2017-10-24 00:17:07 -07:00
# include "../core/android/SDL_android.h"
# endif
2024-09-27 11:33:00 +02:00
# if defined(SDL_PLATFORM_WINDOWS)
2024-07-22 12:09:03 -07:00
# define HAVE_WIN32_ENVIRONMENT
2024-07-26 22:57:53 -07:00
# elif defined(HAVE_GETENV) && \
( defined ( HAVE_SETENV ) | | defined ( HAVE_PUTENV ) ) & & \
( defined ( HAVE_UNSETENV ) | | defined ( HAVE_PUTENV ) )
# define HAVE_LIBC_ENVIRONMENT
2024-09-13 17:00:15 -07:00
# if defined(SDL_PLATFORM_MACOS)
# include <crt_externs.h>
# define environ (*_NSGetEnviron())
# elif defined(SDL_PLATFORM_FREEBSD)
# include <dlfcn.h>
2025-07-30 13:10:11 +02:00
static char * * get_environ_rtld ( void )
{
char * * * environ_rtld = ( char * * * ) dlsym ( RTLD_DEFAULT , " environ " ) ;
return environ_rtld ? * environ_rtld : NULL ;
}
# define environ (get_environ_rtld())
2024-09-13 17:00:15 -07:00
# else
extern char * * environ ;
# endif
2024-09-15 09:16:30 -07:00
# else
# define HAVE_LOCAL_ENVIRONMENT
static char * * environ ;
# endif
struct SDL_Environment
{
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
SDL_Mutex * lock ; // !!! FIXME: reuse SDL_HashTable's lock.
2024-09-15 09:16:30 -07:00
SDL_HashTable * strings ;
} ;
static SDL_Environment * SDL_environment ;
SDL_Environment * SDL_GetEnvironment ( void )
{
if ( ! SDL_environment ) {
SDL_environment = SDL_CreateEnvironment ( true ) ;
}
return SDL_environment ;
}
2024-09-18 07:52:28 -07:00
bool SDL_InitEnvironment ( void )
2024-09-15 09:55:38 -07:00
{
return ( SDL_GetEnvironment ( ) ! = NULL ) ;
}
void SDL_QuitEnvironment ( void )
2024-09-15 09:16:30 -07:00
{
SDL_Environment * env = SDL_environment ;
if ( env ) {
SDL_environment = NULL ;
SDL_DestroyEnvironment ( env ) ;
}
}
2024-09-18 07:52:28 -07:00
SDL_Environment * SDL_CreateEnvironment ( bool populated )
2024-09-15 09:16:30 -07:00
{
SDL_Environment * env = SDL_calloc ( 1 , sizeof ( * env ) ) ;
if ( ! env ) {
return NULL ;
}
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
env - > strings = SDL_CreateHashTable ( 0 , false , SDL_HashString , SDL_KeyMatchString , SDL_DestroyHashKey , NULL ) ;
2024-09-15 09:16:30 -07:00
if ( ! env - > strings ) {
SDL_free ( env ) ;
return NULL ;
}
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
// Don't fail if we can't create a mutex (e.g. on a single-thread environment) // !!! FIXME: single-threaded environments should still return a non-NULL, do-nothing object here. Check for failure!
2024-12-12 13:15:05 -08:00
env - > lock = SDL_CreateMutex ( ) ;
2024-09-15 09:16:30 -07:00
if ( populated ) {
# ifdef SDL_PLATFORM_WINDOWS
LPWCH strings = GetEnvironmentStringsW ( ) ;
if ( strings ) {
for ( LPWCH string = strings ; * string ; string + = SDL_wcslen ( string ) + 1 ) {
char * variable = WIN_StringToUTF8W ( string ) ;
if ( ! variable ) {
continue ;
}
char * value = SDL_strchr ( variable , ' = ' ) ;
if ( ! value | | value = = variable ) {
SDL_free ( variable ) ;
continue ;
}
* value + + = ' \0 ' ;
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
SDL_InsertIntoHashTable ( env - > strings , variable , value , true ) ;
2024-09-15 09:16:30 -07:00
}
FreeEnvironmentStringsW ( strings ) ;
}
# else
# ifdef SDL_PLATFORM_ANDROID
// Make sure variables from the application manifest are available
2024-09-20 11:09:48 -07:00
Android_JNI_GetManifestEnvironmentVariables ( ) ;
2024-09-15 09:16:30 -07:00
# endif
char * * strings = environ ;
if ( strings ) {
for ( int i = 0 ; strings [ i ] ; + + i ) {
char * variable = SDL_strdup ( strings [ i ] ) ;
if ( ! variable ) {
continue ;
}
char * value = SDL_strchr ( variable , ' = ' ) ;
if ( ! value | | value = = variable ) {
SDL_free ( variable ) ;
continue ;
}
* value + + = ' \0 ' ;
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
SDL_InsertIntoHashTable ( env - > strings , variable , value , true ) ;
2024-09-15 09:16:30 -07:00
}
}
# endif // SDL_PLATFORM_WINDOWS
}
return env ;
}
const char * SDL_GetEnvironmentVariable ( SDL_Environment * env , const char * name )
{
const char * result = NULL ;
if ( ! env ) {
return NULL ;
} else if ( ! name | | * name = = ' \0 ' ) {
return NULL ;
}
2024-12-12 13:15:05 -08:00
SDL_LockMutex ( env - > lock ) ;
{
const char * value ;
if ( SDL_FindInHashTable ( env - > strings , name , ( const void * * ) & value ) ) {
result = SDL_GetPersistentString ( value ) ;
}
2024-09-15 09:16:30 -07:00
}
2024-12-12 13:15:05 -08:00
SDL_UnlockMutex ( env - > lock ) ;
2024-09-15 09:16:30 -07:00
return result ;
}
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
typedef struct CountEnvStringsData
{
size_t count ;
size_t length ;
} CountEnvStringsData ;
static bool SDLCALL CountEnvStrings ( void * userdata , const SDL_HashTable * table , const void * key , const void * value )
{
CountEnvStringsData * data = ( CountEnvStringsData * ) userdata ;
data - > length + = SDL_strlen ( ( const char * ) key ) + 1 + SDL_strlen ( ( const char * ) value ) + 1 ;
data - > count + + ;
return true ; // keep iterating.
}
typedef struct CopyEnvStringsData
{
char * * result ;
char * string ;
size_t count ;
} CopyEnvStringsData ;
static bool SDLCALL CopyEnvStrings ( void * userdata , const SDL_HashTable * table , const void * vkey , const void * vvalue )
{
CopyEnvStringsData * data = ( CopyEnvStringsData * ) userdata ;
const char * key = ( const char * ) vkey ;
const char * value = ( const char * ) vvalue ;
size_t len ;
len = SDL_strlen ( key ) ;
data - > result [ data - > count ] = data - > string ;
SDL_memcpy ( data - > string , key , len ) ;
data - > string + = len ;
* ( data - > string + + ) = ' = ' ;
len = SDL_strlen ( value ) ;
SDL_memcpy ( data - > string , value , len ) ;
data - > string + = len ;
* ( data - > string + + ) = ' \0 ' ;
data - > count + + ;
return true ; // keep iterating.
}
2024-09-15 09:16:30 -07:00
char * * SDL_GetEnvironmentVariables ( SDL_Environment * env )
{
char * * result = NULL ;
2025-09-16 21:51:03 -07:00
CHECK_PARAM ( ! env ) {
2024-09-15 09:16:30 -07:00
SDL_InvalidParamError ( " env " ) ;
return NULL ;
}
2024-12-12 13:15:05 -08:00
SDL_LockMutex ( env - > lock ) ;
2024-09-15 09:16:30 -07:00
{
// First pass, get the size we need for all the strings
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
CountEnvStringsData countdata = { 0 , 0 } ;
SDL_IterateHashTable ( env - > strings , CountEnvStrings , & countdata ) ;
2024-09-15 09:16:30 -07:00
// Allocate memory for the strings
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
result = ( char * * ) SDL_malloc ( ( countdata . count + 1 ) * sizeof ( * result ) + countdata . length ) ;
if ( result ) {
// Second pass, copy the strings
char * string = ( char * ) ( result + countdata . count + 1 ) ;
CopyEnvStringsData cpydata = { result , string , 0 } ;
SDL_IterateHashTable ( env - > strings , CopyEnvStrings , & cpydata ) ;
SDL_assert ( countdata . count = = cpydata . count ) ;
result [ cpydata . count ] = NULL ;
2024-09-15 09:16:30 -07:00
}
}
2024-12-12 13:15:05 -08:00
SDL_UnlockMutex ( env - > lock ) ;
2024-09-15 09:16:30 -07:00
return result ;
}
2024-09-18 07:52:28 -07:00
bool SDL_SetEnvironmentVariable ( SDL_Environment * env , const char * name , const char * value , bool overwrite )
2024-09-15 09:16:30 -07:00
{
bool result = false ;
2025-09-16 21:51:03 -07:00
CHECK_PARAM ( ! env ) {
2024-09-15 09:16:30 -07:00
return SDL_InvalidParamError ( " env " ) ;
2025-09-16 21:51:03 -07:00
}
CHECK_PARAM ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL ) {
2024-09-15 09:16:30 -07:00
return SDL_InvalidParamError ( " name " ) ;
2025-09-16 21:51:03 -07:00
}
CHECK_PARAM ( ! value ) {
2024-09-15 09:16:30 -07:00
return SDL_InvalidParamError ( " value " ) ;
}
2024-12-12 13:15:05 -08:00
SDL_LockMutex ( env - > lock ) ;
2024-09-15 09:16:30 -07:00
{
hashtable: Redesign the hashtable API.
This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.
This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.
- The ability to make "stackable" tables is removed. Apparently this still
worked with the current implementation, but I could see a future
implementation struggle mightily to support this. It'll be better for
something external to build on top of the table if it needs it, inserting a
linked list of stacked items as the hash values and managing them separately.
There was only one place in SDL using this, unnecessarily, and that has also
been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
estimated number of items the table is meant to hold. The bucket count was
crucial to our classic hashtable implementation, but meant less once we
moved to an Open Addressing implementation anyhow, since the bucket count
isn't static (and they aren't really "buckets" anymore either). Now you
can just report how many items you think the hash will hold and SDL will
allocate a reasonable default for you...or 0 to not guess, and SDL will
start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
and also lets us hold the read-lock for the entire iteration and get rid of
the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.
I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
2025-02-13 16:13:43 -05:00
char * string = NULL ;
if ( SDL_asprintf ( & string , " %s=%s " , name , value ) > 0 ) {
const size_t len = SDL_strlen ( name ) ;
string [ len ] = ' \0 ' ;
const char * origname = name ;
name = string ;
value = string + len + 1 ;
result = SDL_InsertIntoHashTable ( env - > strings , name , value , overwrite ) ;
if ( ! result ) {
SDL_free ( string ) ;
if ( ! overwrite ) {
const void * existing_value = NULL ;
// !!! FIXME: InsertIntoHashTable does this lookup too, maybe we should have a means to report that, to avoid duplicate work?
if ( SDL_FindInHashTable ( env - > strings , origname , & existing_value ) ) {
result = true ; // it already existed, and we refused to overwrite it. Call it success.
}
}
2024-09-15 09:16:30 -07:00
}
}
}
2024-12-12 13:15:05 -08:00
SDL_UnlockMutex ( env - > lock ) ;
2024-09-15 09:16:30 -07:00
return result ;
}
2024-09-18 07:52:28 -07:00
bool SDL_UnsetEnvironmentVariable ( SDL_Environment * env , const char * name )
2024-09-15 09:16:30 -07:00
{
2024-12-12 13:15:05 -08:00
bool result = false ;
2025-09-16 21:51:03 -07:00
CHECK_PARAM ( ! env ) {
2024-09-15 09:16:30 -07:00
return SDL_InvalidParamError ( " env " ) ;
2025-09-16 21:51:03 -07:00
}
CHECK_PARAM ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL ) {
2024-09-15 09:16:30 -07:00
return SDL_InvalidParamError ( " name " ) ;
}
2024-12-12 13:15:05 -08:00
SDL_LockMutex ( env - > lock ) ;
{
const void * value ;
if ( SDL_FindInHashTable ( env - > strings , name , & value ) ) {
result = SDL_RemoveFromHashTable ( env - > strings , name ) ;
} else {
result = true ;
}
}
SDL_UnlockMutex ( env - > lock ) ;
return result ;
2024-09-15 09:16:30 -07:00
}
void SDL_DestroyEnvironment ( SDL_Environment * env )
{
if ( ! env | | env = = SDL_environment ) {
return ;
}
2024-12-12 13:15:05 -08:00
SDL_DestroyMutex ( env - > lock ) ;
2024-09-15 09:16:30 -07:00
SDL_DestroyHashTable ( env - > strings ) ;
SDL_free ( env ) ;
}
2024-09-13 17:00:15 -07:00
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Put a variable into the environment
// Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/)
2024-07-22 12:09:03 -07:00
# ifdef HAVE_LIBC_ENVIRONMENT
# if defined(HAVE_SETENV)
2024-09-13 17:00:15 -07:00
int SDL_setenv_unsafe ( const char * name , const char * value , int overwrite )
2015-06-21 17:33:46 +02:00
{
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2023-11-09 22:29:15 +01:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL | | ! value ) {
2022-11-27 17:38:43 +01:00
return - 1 ;
2015-06-21 17:33:46 +02:00
}
2022-11-30 12:51:59 -08:00
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_SetEnvironmentVariable ( SDL_environment , name , value , ( overwrite ! = 0 ) ) ;
}
2024-09-15 09:16:30 -07:00
2015-06-21 17:33:46 +02:00
return setenv ( name , value , overwrite ) ;
}
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// We have a real environment table, but no real setenv? Fake it w/ putenv.
2024-07-22 12:09:03 -07:00
# else
2024-09-13 17:00:15 -07:00
int SDL_setenv_unsafe ( const char * name , const char * value , int overwrite )
2015-06-21 17:33:46 +02:00
{
char * new_variable ;
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2023-11-09 22:29:15 +01:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL | | ! value ) {
2022-11-27 17:38:43 +01:00
return - 1 ;
2015-06-21 17:33:46 +02:00
}
2022-11-30 12:51:59 -08:00
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_SetEnvironmentVariable ( SDL_environment , name , value , ( overwrite ! = 0 ) ) ;
}
2024-09-15 09:16:30 -07:00
2015-06-21 17:33:46 +02:00
if ( getenv ( name ) ! = NULL ) {
2024-07-26 22:57:53 -07:00
if ( ! overwrite ) {
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
return 0 ; // leave the existing one there.
2015-06-21 17:33:46 +02:00
}
}
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// This leaks. Sorry. Get a better OS so we don't have to do this.
2024-07-26 22:57:53 -07:00
SDL_asprintf ( & new_variable , " %s=%s " , name , value ) ;
2023-11-09 22:29:15 +01:00
if ( ! new_variable ) {
2022-11-27 17:38:43 +01:00
return - 1 ;
2015-06-21 17:33:46 +02:00
}
return putenv ( new_variable ) ;
}
2024-07-22 12:09:03 -07:00
# endif
# elif defined(HAVE_WIN32_ENVIRONMENT)
2024-09-13 17:00:15 -07:00
int SDL_setenv_unsafe ( const char * name , const char * value , int overwrite )
2024-07-22 12:09:03 -07:00
{
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2024-07-22 12:09:03 -07:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL | | ! value ) {
return - 1 ;
}
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_SetEnvironmentVariable ( SDL_environment , name , value , ( overwrite ! = 0 ) ) ;
}
2024-09-15 09:16:30 -07:00
2024-07-22 12:09:03 -07:00
if ( ! overwrite ) {
if ( GetEnvironmentVariableA ( name , NULL , 0 ) > 0 ) {
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
return 0 ; // asked not to overwrite existing value.
2024-07-22 12:09:03 -07:00
}
}
2024-07-26 22:57:53 -07:00
if ( ! SetEnvironmentVariableA ( name , value ) ) {
2024-07-22 12:09:03 -07:00
return - 1 ;
}
return 0 ;
}
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
# else // roll our own
2024-07-22 12:09:03 -07:00
2024-09-13 17:00:15 -07:00
int SDL_setenv_unsafe ( const char * name , const char * value , int overwrite )
2015-06-21 17:33:46 +02:00
{
int added ;
2020-05-05 12:48:55 -04:00
size_t len , i ;
2015-06-21 17:33:46 +02:00
char * * new_env ;
char * new_variable ;
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2023-11-09 22:29:15 +01:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL | | ! value ) {
2022-11-27 17:38:43 +01:00
return - 1 ;
2015-06-21 17:33:46 +02:00
}
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// See if it already exists
2024-09-13 17:00:15 -07:00
if ( ! overwrite & & SDL_getenv_unsafe ( name ) ) {
2015-06-21 17:33:46 +02:00
return 0 ;
}
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_SetEnvironmentVariable ( SDL_environment , name , value , ( overwrite ! = 0 ) ) ;
}
2024-09-15 09:16:30 -07:00
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Allocate memory for the variable
2015-06-21 17:33:46 +02:00
len = SDL_strlen ( name ) + SDL_strlen ( value ) + 2 ;
2022-11-30 12:51:59 -08:00
new_variable = ( char * ) SDL_malloc ( len ) ;
2023-11-09 22:29:15 +01:00
if ( ! new_variable ) {
2022-11-27 17:38:43 +01:00
return - 1 ;
2015-06-21 17:33:46 +02:00
}
SDL_snprintf ( new_variable , len , " %s=%s " , name , value ) ;
value = new_variable + SDL_strlen ( name ) + 1 ;
name = new_variable ;
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Actually put it into the environment
2015-06-21 17:33:46 +02:00
added = 0 ;
i = 0 ;
2024-09-15 09:16:30 -07:00
if ( environ ) {
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Check to see if it's already there...
2015-06-21 17:33:46 +02:00
len = ( value - name ) ;
2024-09-15 09:16:30 -07:00
for ( ; environ [ i ] ; + + i ) {
if ( SDL_strncmp ( environ [ i ] , name , len ) = = 0 ) {
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// If we found it, just replace the entry
2024-09-15 09:16:30 -07:00
SDL_free ( environ [ i ] ) ;
environ [ i ] = new_variable ;
2024-07-26 22:57:53 -07:00
added = 1 ;
2015-06-21 17:33:46 +02:00
break ;
}
}
}
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Didn't find it in the environment, expand and add
2015-06-21 17:33:46 +02:00
if ( ! added ) {
2024-09-15 09:16:30 -07:00
new_env = SDL_realloc ( environ , ( i + 2 ) * sizeof ( char * ) ) ;
2015-06-21 17:33:46 +02:00
if ( new_env ) {
2024-09-15 09:16:30 -07:00
environ = new_env ;
environ [ i + + ] = new_variable ;
environ [ i + + ] = ( char * ) 0 ;
2015-06-21 17:33:46 +02:00
added = 1 ;
} else {
SDL_free ( new_variable ) ;
}
}
2022-11-27 17:38:43 +01:00
return added ? 0 : - 1 ;
2015-06-21 17:33:46 +02:00
}
2024-07-22 12:09:03 -07:00
# endif // HAVE_LIBC_ENVIRONMENT
2015-06-21 17:33:46 +02:00
2024-07-26 22:57:53 -07:00
# ifdef HAVE_LIBC_ENVIRONMENT
# if defined(HAVE_UNSETENV)
2024-09-13 17:00:15 -07:00
int SDL_unsetenv_unsafe ( const char * name )
2024-07-26 22:57:53 -07:00
{
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2024-07-26 22:57:53 -07:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL ) {
return - 1 ;
}
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_UnsetEnvironmentVariable ( SDL_environment , name ) ;
}
2024-09-15 09:16:30 -07:00
2024-07-26 22:57:53 -07:00
return unsetenv ( name ) ;
}
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// We have a real environment table, but no unsetenv? Fake it w/ putenv.
2024-07-26 22:57:53 -07:00
# else
2024-09-13 17:00:15 -07:00
int SDL_unsetenv_unsafe ( const char * name )
2024-07-26 22:57:53 -07:00
{
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2024-07-26 22:57:53 -07:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL ) {
return - 1 ;
}
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_UnsetEnvironmentVariable ( SDL_environment , name ) ;
}
2024-09-15 09:16:30 -07:00
2024-07-26 22:57:53 -07:00
// Hope this environment uses the non-standard extension of removing the environment variable if it has no '='
return putenv ( name ) ;
}
# endif
# elif defined(HAVE_WIN32_ENVIRONMENT)
2024-09-13 17:00:15 -07:00
int SDL_unsetenv_unsafe ( const char * name )
2024-07-26 22:57:53 -07:00
{
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2024-07-26 22:57:53 -07:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL ) {
return - 1 ;
}
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_UnsetEnvironmentVariable ( SDL_environment , name ) ;
}
2024-09-15 09:16:30 -07:00
2024-07-26 22:57:53 -07:00
if ( ! SetEnvironmentVariableA ( name , NULL ) ) {
return - 1 ;
}
return 0 ;
}
# else
2024-09-13 17:00:15 -07:00
int SDL_unsetenv_unsafe ( const char * name )
2024-07-26 22:57:53 -07:00
{
size_t len , i ;
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2024-07-26 22:57:53 -07:00
if ( ! name | | * name = = ' \0 ' | | SDL_strchr ( name , ' = ' ) ! = NULL ) {
return - 1 ;
}
2025-12-13 21:25:43 -08:00
if ( SDL_environment ) {
SDL_UnsetEnvironmentVariable ( SDL_environment , name ) ;
}
2024-09-15 09:16:30 -07:00
if ( environ ) {
2024-07-26 22:57:53 -07:00
len = SDL_strlen ( name ) ;
2024-09-15 09:16:30 -07:00
for ( i = 0 ; environ [ i ] ; + + i ) {
if ( ( SDL_strncmp ( environ [ i ] , name , len ) = = 0 ) & &
( environ [ i ] [ len ] = = ' = ' ) ) {
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Just clear out this entry for now
2024-09-15 09:16:30 -07:00
* environ [ i ] = ' \0 ' ;
2024-07-26 22:57:53 -07:00
break ;
}
}
}
return 0 ;
}
# endif // HAVE_LIBC_ENVIRONMENT
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Retrieve a variable named "name" from the environment
2024-07-22 12:09:03 -07:00
# ifdef HAVE_LIBC_ENVIRONMENT
2024-09-13 17:00:15 -07:00
const char * SDL_getenv_unsafe ( const char * name )
2017-10-24 00:17:07 -07:00
{
2024-01-24 02:40:51 +01:00
# ifdef SDL_PLATFORM_ANDROID
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Make sure variables from the application manifest are available
2017-11-04 09:37:29 -07:00
Android_JNI_GetManifestEnvironmentVariables ( ) ;
# endif
2017-10-24 00:17:07 -07:00
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2023-11-09 22:29:15 +01:00
if ( ! name | | * name = = ' \0 ' ) {
2015-06-21 17:33:46 +02:00
return NULL ;
}
return getenv ( name ) ;
}
2024-07-22 12:09:03 -07:00
# elif defined(HAVE_WIN32_ENVIRONMENT)
2024-09-13 17:00:15 -07:00
const char * SDL_getenv_unsafe ( const char * name )
2015-06-21 17:33:46 +02:00
{
2024-07-22 12:09:03 -07:00
DWORD length , maxlen = 0 ;
2024-07-26 18:57:18 -07:00
char * string = NULL ;
2024-08-22 17:33:49 -07:00
const char * result = NULL ;
2015-06-21 17:33:46 +02:00
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2023-11-09 22:29:15 +01:00
if ( ! name | | * name = = ' \0 ' ) {
2015-06-21 17:33:46 +02:00
return NULL ;
}
2022-11-30 12:51:59 -08:00
2024-07-22 12:09:03 -07:00
for ( ; ; ) {
2024-07-26 22:57:53 -07:00
SetLastError ( ERROR_SUCCESS ) ;
2024-07-26 18:57:18 -07:00
length = GetEnvironmentVariableA ( name , string , maxlen ) ;
2024-07-22 12:09:03 -07:00
if ( length > maxlen ) {
2024-07-26 18:57:18 -07:00
char * temp = ( char * ) SDL_realloc ( string , length ) ;
if ( ! temp ) {
2024-07-22 12:09:03 -07:00
return NULL ;
}
2024-07-26 18:57:18 -07:00
string = temp ;
2024-07-22 12:09:03 -07:00
maxlen = length ;
} else {
2024-07-26 22:57:53 -07:00
if ( GetLastError ( ) ! = ERROR_SUCCESS ) {
2025-10-19 11:07:48 +02:00
SDL_free ( string ) ;
2024-07-26 22:57:53 -07:00
return NULL ;
}
2024-07-22 12:09:03 -07:00
break ;
2015-06-21 17:33:46 +02:00
}
}
2024-07-26 18:57:18 -07:00
if ( string ) {
2024-08-22 17:33:49 -07:00
result = SDL_GetPersistentString ( string ) ;
2024-07-26 18:57:18 -07:00
SDL_free ( string ) ;
}
2024-08-22 17:33:49 -07:00
return result ;
2015-06-21 17:33:46 +02:00
}
# else
2024-09-13 17:00:15 -07:00
const char * SDL_getenv_unsafe ( const char * name )
2015-06-21 17:33:46 +02:00
{
2020-05-05 12:48:55 -04:00
size_t len , i ;
2024-09-15 09:16:30 -07:00
const char * value = NULL ;
2015-06-21 17:33:46 +02:00
Use C++ style comments consistently in SDL source code
Implemented using this script:
find . -type f -exec sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' {} \;
git checkout \
core/linux/SDL_evdev_kbd_default_keymap.h \
events/imKStoUCS.* \
hidapi \
joystick/controller_type.c \
joystick/controller_type.h \
joystick/hidapi/steam/controller_constants.h \
joystick/hidapi/steam/controller_structs.h \
joystick/SDL_gamepad_db.h \
libm \
render/*/*Shader*.h \
render/vitagxm/SDL_render_vita_gxm_shaders.h \
render/metal/SDL_shaders_metal_*.h \
stdlib/SDL_malloc.c \
stdlib/SDL_qsort.c \
stdlib/SDL_strtokr.c \
test/ \
video/directx/SDL_d3d12_xbox_cmacros.h \
video/directx/d3d12.h \
video/directx/d3d12sdklayers.h \
video/khronos \
video/x11/edid-parse.c \
video/x11/xsettings-client.* \
video/yuv2rgb
sed -i'' -e 's,/\* *\([^*]*\)\*/ *$,// \1,' -e 's, \+$,,' hidapi/SDL_hidapi.c
2024-08-22 10:30:45 -07:00
// Input validation
2023-11-09 22:29:15 +01:00
if ( ! name | | * name = = ' \0 ' ) {
2015-06-21 17:33:46 +02:00
return NULL ;
}
2022-11-30 12:51:59 -08:00
2024-09-15 09:16:30 -07:00
if ( environ ) {
2015-06-21 17:33:46 +02:00
len = SDL_strlen ( name ) ;
2024-09-15 09:16:30 -07:00
for ( i = 0 ; environ [ i ] ; + + i ) {
if ( ( SDL_strncmp ( environ [ i ] , name , len ) = = 0 ) & &
( environ [ i ] [ len ] = = ' = ' ) ) {
value = & environ [ i ] [ len + 1 ] ;
2024-07-26 22:57:53 -07:00
break ;
2015-06-21 17:33:46 +02:00
}
}
}
return value ;
}
2024-07-22 12:09:03 -07:00
# endif // HAVE_LIBC_ENVIRONMENT
2024-09-13 17:00:15 -07:00
2024-09-15 09:16:30 -07:00
const char * SDL_getenv ( const char * name )
2024-09-13 17:00:15 -07:00
{
2024-09-15 09:16:30 -07:00
return SDL_GetEnvironmentVariable ( SDL_GetEnvironment ( ) , name ) ;
2024-09-13 17:00:15 -07:00
}