summaryrefslogtreecommitdiff
path: root/legacy/evil/src/lib/evil_stdlib.c
blob: 492bc63ab66af3b3eb7b64795da11b5e6c07c186 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
# include <io.h>

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif /* HAVE_ERRNO_H */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include "Evil.h"
#include "evil_private.h"

/*
 * Environment variable related functions
 *
 * char *getenv (const char *name);
 * int putenv (const char *string);
 * int setenv (const char *name, const char *value, int overwrite);
 * void unsetenv (const char *name);
 *
 */

#ifdef _WIN32_WCE

static char _evil_stdlib_getenv_buffer[PATH_MAX];

char *
getenv(const char *name)
{
   HKEY     key;
   wchar_t *wname;
   LONG     res;
   DWORD    type;
   DWORD    disposition;
   DWORD    size = PATH_MAX;

   if (!name || !*name)
     return NULL;

   if ((res = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                             TEXT("Software\\Efl\\Environment"),
                             0, NULL,
                             REG_OPTION_VOLATILE,
                             0, NULL,
                             &key, &disposition)) != ERROR_SUCCESS)
     {
        _evil_error_display(__FUNCTION__, res);
        return NULL;
     }

   wname = evil_char_to_wchar(name);
   if (!wname)
     {
        if ((res = RegCloseKey (key)) != ERROR_SUCCESS)
          _evil_error_display(__FUNCTION__, res);
        return NULL;
     }

   if ((res = RegQueryValueEx(key, wname,
                              NULL, &type,
                              (LPBYTE)&_evil_stdlib_getenv_buffer,
                              &size)) != ERROR_SUCCESS)
     {
        if ((res = RegCloseKey (key)) != ERROR_SUCCESS)
          _evil_error_display(__FUNCTION__, res);
        free(wname);
        return NULL;
     }

   free(wname);

   if ((res = RegCloseKey (key)) != ERROR_SUCCESS)
     {
        _evil_error_display(__FUNCTION__, res);
        return NULL;
     }

   if (_evil_stdlib_getenv_buffer[0] == '\0')
     return NULL;
   else
     {
        return _evil_stdlib_getenv_buffer;
     }
}

#endif /* _WIN32_WCE */

#ifdef __MINGW32CE__

int
putenv(const char *string)
{
   char *str;
   char *egal;
   char *name;
   char *value;

   str = strdup(string);
   if (!str)
     return -1;
   egal = strchr(str, '=');
   if (!egal)
     return -1;

   value = egal + 1;
   *egal = '\0';
   name = str;
   setenv(name, value, 1);
   free(str);

   return 0;
}

#endif /* __MINGW32CE__ */



int
setenv(const char *name,
       const char *value,
       int         overwrite)
{
#ifndef __MINGW32CE__

   char  *old_name;
   char  *str;
   size_t length;
   int    res;

   if (!name || !*name)
     return -1;

   /* if '=' is found, return EINVAL */
   if (strchr (name, '='))
     {
#ifdef HAVE_ERRNO_H
        errno = EINVAL;
#endif /* HAVE_ERRNO_H */
        return -1;
     }

   /* if name is already set and overwrite is 0, we exit with success */
   old_name = getenv(name);
   if (!overwrite && old_name)
     return 0;

   length = value ? strlen(value) : 0;
   length += strlen(name) + 2;
   str = (char *)malloc(length);
   if (!str)
     {
#ifdef HAVE_ERRNO_H
        errno = ENOMEM;
#endif /* HAVE_ERRNO_H */
        return -1;
     }
   if (!value)
     sprintf(str, "%s=", name);
   else
     sprintf(str, "%s=%s", name, value);
   res = _putenv(str);
   free(str);

   return res;

#else /* __MINGW32CE__ */

   HKEY     key;
   LONG     res;
   DWORD    disposition;
   wchar_t *wname;
   char    *data;
   DWORD    size;

   if (!name || !*name)
     return -1;

   /* if '=' is found, return an error */
   if (strchr (name, '='))
     return -1;

   if ((res = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                             TEXT("Software\\Efl\\Environment"),
                             0, NULL,
                             REG_OPTION_VOLATILE,
                             0, NULL,
                             &key,
                             &disposition)) != ERROR_SUCCESS)
     {
        _evil_error_display(__FUNCTION__, res);
        return -1;
     }

   /* if name is already set and overwrite is 0, we exit with success */
   if (!overwrite && (disposition == REG_OPENED_EXISTING_KEY))
     return 0;

   wname = evil_char_to_wchar(name);
   if (!wname)
     {
        if ((res = RegCloseKey (key)) != ERROR_SUCCESS)
          _evil_error_display(__FUNCTION__, res);
        return -1;
     }

   if (value)
     {
        size = strlen(value);
        data = malloc(sizeof(char) * (size + 1));
        if (!data)
          return -1;
        memcpy((void *)data, value, size);
        data[size] = '\0';
     }
   else
     {
        size = 0;
        data = malloc(sizeof(char));
        if (!data)
          return -1;
        data[0] = '\0';
     }
   if (!data)
     return -1;

   if ((res = RegSetValueEx(key,
                            (LPCWSTR)wname,
                            0, REG_SZ,
                            (const BYTE *)data,
                            size + 1)) != ERROR_SUCCESS)
     {
        free(wname);
        _evil_error_display(__FUNCTION__, res);
        if ((res = RegCloseKey (key)) != ERROR_SUCCESS)
          _evil_error_display(__FUNCTION__, res);
        return -1;
     }

   free(data);
   free(wname);

   if ((res = RegCloseKey (key)) != ERROR_SUCCESS)
     {
        _evil_error_display(__FUNCTION__, res);
        return -1;
     }

   return 0;

#endif /* ! __MINGW32CE__ */
}

int
unsetenv(const char *name)
{
   return setenv(name, NULL, 1);
}


/*
 * Files related functions
 *
 */

int
mkstemp(char *__template)
{
   const char lookup[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   char      *suffix;
   DWORD      val;
   size_t     length;
   int        i;

   if (!__template)
     return 0;

   length = strlen(__template);
   if ((length < 6) ||
       (strncmp (__template + length - 6, "XXXXXX", 6)))
     {
#ifdef HAVE_ERRNO_H
        errno = EINVAL;
#endif /* HAVE_ERRNO_H */
        return -1;
     }

   suffix = __template + length - 6;

   val = GetTickCount();
   val += GetCurrentProcessId();

   for (i = 0; i < 32768; i++)
     {
        DWORD v;
        int fd;

        v = val;

        suffix[0] = lookup[v % 62];
        v /= 62;
        suffix[1] = lookup[v % 62];
        v /= 62;
        suffix[2] = lookup[v % 62];
        v /= 62;
        suffix[3] = lookup[v % 62];
        v /= 62;
        suffix[4] = lookup[v % 62];
        v /= 62;
        suffix[5] = lookup[v % 62];
        v /= 62;

#ifndef __MINGW32CE__
        fd = _open(__template, _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE);
#else /* ! __MINGW32CE__ */
        {
           FILE    *f;
           wchar_t *wtemplate;

           wtemplate = evil_char_to_wchar(__template);
           if (!wtemplate)
             return -1;
           f = _wfopen(wtemplate, L"rwb");
           free(wtemplate);
           if (!f)
             {
# ifdef HAVE_ERRNO_H
                errno = EEXIST;
# endif /* HAVE_ERRNO_H */
                return -1;
             }
           fd = (int)_fileno(f);
        }
#endif /* __MINGW32CE__ */
        if (fd >= 0)
          return fd;

        val += 7777;
     }

#ifdef HAVE_ERRNO_H
   errno = EEXIST;
#endif /* HAVE_ERRNO_H */
   return -1;
}


char *
realpath(const char *file_name, char *resolved_name)
{
#ifndef __MINGW32CE__
   return _fullpath(resolved_name, file_name, PATH_MAX);
#else
   char   cwd[PATH_MAX];
   size_t l1;
   size_t l2;
   size_t l;

   if (!file_name || !resolved_name)
     return NULL;

   if (!getcwd(cwd, PATH_MAX))
     return NULL;

   l1 = strlen(cwd);
   l2 = strlen(file_name);
   l = l1 + l2 + 2;

   if (l > PATH_MAX)
     l = PATH_MAX - 1;
   memcpy(resolved_name, cwd, l1);
   resolved_name[l1] = '\\';
   memcpy(resolved_name + l1 + 1, file_name, l2);
   resolved_name[l] = '\0';

   return resolved_name;
#endif /* __MINGW32CE__ */
}