naev 0.10.4
env.c
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
5#include <assert.h>
6#include <limits.h>
7#include <stdlib.h>
10#include "env.h"
11
12#include "log.h"
13#include "nstring.h"
14
15env_t env;
16
17void env_detect( int argc, char **argv )
18{
19 (void) argc;
20 static short once = 0;
21 assert( once == 0 );
22 once = 1;
23
24 env.appimage = getenv( "APPIMAGE" );
25 if (env.appimage != NULL) {
26 env.isAppImage = 1;
27 env.argv0 = getenv( "ARGV0" );
28 env.appdir = getenv( "APPDIR" );
29 }
30 else {
31 env.isAppImage = 0;
32 env.argv0 = argv[0];
33 }
34}
35
42int nsetenv( const char *name, const char *value, int overwrite )
43{
44#if HAVE_DECL_SETENV
45 return setenv( name, value, overwrite );
46#else /* HAVE_DECL_SETENV */
47 if (!overwrite) {
48#if HAVE_DECL_GETENV_S
49 size_t envsize = 0;
50 int errcode = getenv_s( &envsize, NULL, 0, name );
51 if (errcode || envsize)
52 return errcode;
53#else /* HAVE_DECL__PUTENV_S */
54 const char *envval = getenv( name );
55 if (envval != NULL)
56 return 0;
57#endif /* HAVE_DECL__PUTENV_S */
58 }
59#if HAVE_DECL__PUTENV_S
60 return _putenv_s(name, value);
61#else /* HAVE_DECL__PUTENV_S */
62 char *buf;
63 asprintf( &buf, "%s=%s", name, value );
64 /* Per the standard, the string pointed to by putenv's argument becomes part of the environment
65 * ("so altering the string alters the environment" and "it is an error to call putenv() with an
66 * automatic variable as the argument".)
67 * If we're stuck using this wildly dangerous function, just leak the memory.
68 * */
69 return putenv( buf );
70#endif /* HAVE_DECL__PUTENV_S */
71#endif /* HAVE_DECL_SETENV */
72}
static char buf[NEWS_MAX_LENGTH]
Definition: news.c:45
int asprintf(char **strp, const char *fmt,...)
Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument....
Definition: nstring.c:161
Definition: env.h:10