summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/System/StdC/NptStdcEnvironment.cpp
blob: 62894ebe65e5df1b651854bafc59988db45a4159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*****************************************************************
|
|      Neptune - Environment variables: StdC Implementation
|
|      (c) 2002-2006 Gilles Boccon-Gibod
|      Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#include <stdlib.h>

#include "NptConfig.h"
#include "NptUtils.h"
#include "NptResults.h"

/*----------------------------------------------------------------------
|   NPT_Environment::Get
+---------------------------------------------------------------------*/
NPT_Result 
NPT_Environment::Get(const char* name, NPT_String& value)
{
    char* env = nullptr;

    /* default value */
    value.SetLength(0);

#if defined(NPT_CONFIG_HAVE_GETENV)
    env = getenv(name);
    if (env) {
        value = env;
        return NPT_SUCCESS;
    } else {
        return NPT_ERROR_NO_SUCH_ITEM;
    }
#elif defined(NPT_CONFIG_HAVE_DUPENV_S)
    if (dupenv_s(&env, NULL, name) != 0) {
        return NPT_FAILURE;
    } else if (env != NULL) {
        value = env;
        free(env);
        return NPT_SUCCESS;
    } else {
        return NPT_ERROR_NO_SUCH_ITEM;
    }
#else
    return NPT_ERROR_NOT_SUPPORTED;
#endif
}

/*----------------------------------------------------------------------
|   NPT_Environment::Set
+---------------------------------------------------------------------*/
NPT_Result 
NPT_Environment::Set(const char* name, const char* value)
{
    if (value) {
#if defined(NPT_CONFIG_HAVE_SETENV)
        // set the variable
        setenv(name, value, 1); // ignore return value (some platforms have this function as void)
        return NPT_SUCCESS;
#elif defined(NPT_CONFIG_HAVE_PUTENV_S)
        return putenv_s(name, value)==0?NPT_SUCCESS:NPT_FAILURE;
#else
        return NPT_ERROR_NOT_SUPPORTED;
#endif
    } else {
        // remove the variable
#if defined(NPT_CONFIG_HAVE_UNSETENV)
        unsetenv(name); // ignore return value (some platforms have this function as void)
        return NPT_SUCCESS;
#elif defined(NPT_CONFIG_HAVE_PUTENV_S)
        return putenv_s(name, "")==0?NPT_SUCCESS:NPT_FAILURE;
#else
        return NPT_ERROR_NOT_SUPPORTED;
#endif
    }
}