forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopts_win32.h
100 lines (79 loc) · 1.59 KB
/
gopts_win32.h
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
/**
* @file
*
* @brief Source for opts plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef ELEKTRA_GOPTS_SYSCTL_H
#define ELEKTRA_GOPTS_SYSCTL_H
#include <string.h>
#include <windows.h>
#include <kdbhelper.h>
static int loadArgs (char *** argvp)
{
int argc;
LPWSTR * args = CommandLineToArgvW (GetCommandLineW (), &argc);
if (args == NULL)
{
return 0;
}
char ** argv = elektraMalloc ((argc + 1) * sizeof (char *));
for (int i = 0; i < argc; ++i)
{
// TODO: error handling?
int size = WideCharToMultiByte (CP_UTF8, 0, args[i], -1, NULL, 0, NULL, NULL);
argv[i] = elektraMalloc (size * sizeof (char));
WideCharToMultiByte (CP_UTF8, 0, args[i], -1, argv[i], size, NULL, NULL);
}
LocalFree (args);
*argvp = argv;
return argc;
}
static char ** loadEnvp (void)
{
LPTCH env = GetEnvironmentStrings ();
if (env == NULL)
{
return NULL;
}
char * e = env;
size_t size = 0;
int count = 0;
while (e[0] != '\0' && (e = strchr (e, '\0')) != NULL)
{
++count;
++e;
}
char ** envp = elektraMalloc ((count + 1) * sizeof (char *));
e = env;
envp[0] = env;
int index = 0;
char * next = NULL;
while (e[0] != '\0' && (next = strchr (e, '\0')) != NULL)
{
envp[index] = e;
++index;
e = next + 1;
}
envp[index] = e;
envp[index + 1] = NULL;
return envp;
}
static void cleanupArgs (int argc, char ** argv)
{
for (int i = 0; i < argc; ++i)
{
elektraFree (argv[i]);
}
elektraFree (argv);
}
static void cleanupEnvp (char ** envp)
{
if (envp != NULL)
{
FreeEnvironmentStrings (envp[0]);
}
}
#endif // ELEKTRA_GOPTS_SYSCTL_H