DCL 3.7.4
Loading...
Searching...
No Matches
_stdlib.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#include <errno.h>
4#if defined(_AIX) || defined(__sun__)
5 #include <alloca.h>
6#endif
7#include <stdlib.h> // alloca
8#include <string.h> // strchr, strlen
9#include <dcl/_stdlib.h>
10
11#include <dcl/Object.h> // __T(str)
12#include <dcl/String.h>
13
14#include "__strumbs.h"
15
16#if __DCL_HAVE_THIS_FILE__
17#undef __THIS_FILE__
18static const char_t __UNUSED__ __THIS_FILE__[] = __T("dcl/_stdlib.cpp");
19#endif
20
21__DCL_BEGIN_NAMESPACE
22
23#ifdef __WINNT__
24DCLCAPI int setenv(const char* _name, const char* _value, int _overwrite)
25{
26 if (_name == NULL || *_name == '\0' || strchr(_name, '=')
27 || _value == NULL) {
28 errno = EINVAL;
29 return -1;
30 }
31
32 if (!_overwrite && getenv(_name)) {
33 return 0;
34 }
35
36 char* sz = (char*) alloca(strlen(_name) + strlen(_value) + 2);
37 strcpy(sz, _name);
38 strcat(sz, "=");
39 strcat(sz, _value);
40 return _putenv(sz);
41}
42
43DCLCAPI int unsetenv(const char* _name)
44{
45 if (_name == NULL || *_name == '\0' || strchr(_name, '=')) {
46 errno = EINVAL;
47 return -1;
48 }
49
50 char* sz = (char*) alloca(strlen(_name) + 2);
51 strcpy(sz, _name);
52 strcat(sz, "=");
53 return _putenv(sz);
54}
55#endif
56
57int __getenv(const String& _name, String& _value)
58{
59 STRTOMBS_ER(_name, name);
60 char* s = getenv(name);
61 if (s) {
62 MBSTOSTR_ER(s, (size_t) -1, _value);
63 return 0;
64 }
65 errno = ENOENT;
66 return -1;
67}
68
69int __setenv(const String& _name, const String& _value, bool _overwrite)
70{
71 STRTOMBS_ER(_name, name);
72 STRTOMBS_ER(_value, value);
73 return setenv(name, value, _overwrite ? 1 : 0);
74}
75
76int __unsetenv(const String& _name)
77{
78 STRTOMBS_ER(_name, name);
79 return unsetenv(name);
80}
81
82#ifndef __WINNT__
83int __realpath(const String& _path, String& _resolved)
84{
85 char resolved[PATH_MAX + 1];
86 STRTOMBS_ER(_path, path);
87 char* s = ::realpath(path, resolved);
88 if (s) {
89 MBSTOSTR_ER(s, (size_t)-1, _resolved);
90 return 0;
91 }
92 return -1;
93}
94#endif // __WINNT__
95
96__DCL_END_NAMESPACE
#define STRTOMBS_ER(str, mbs)
Definition __strumbs.h:33
#define MBSTOSTR_ER(mbs, nmbs, str)
Definition __strumbs.h:19
int __realpath(const String &_path, String &_resolved)
Definition _stdlib.cpp:83
int __setenv(const String &_name, const String &_value, bool _overwrite)
Definition _stdlib.cpp:69
int __unsetenv(const String &_name)
Definition _stdlib.cpp:76
__DCL_BEGIN_NAMESPACE int __getenv(const String &_name, String &_value)
Definition _stdlib.cpp:57
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
#define __UNUSED__
Definition Config.h:341
#define DCLCAPI
Definition Config.h:95
wchar_t char_t
Definition Config.h:247
#define __T(str)
Definition Object.h:60