This commit was manufactured by cvs2svn to create tag
[kbuild-mirror.git] / src / gmake / w32 / subproc / misc.c
blob7e32652c2a2873b3ce9bb65d8a17ff07c142de53
1 #include <stddef.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <windows.h>
5 #include "proc.h"
8 /*
9 * Description: Convert a NULL string terminated UNIX environment block to
10 * an environment block suitable for a windows32 system call
12 * Returns: TRUE= success, FALSE=fail
14 * Notes/Dependencies: the environment block is sorted in case-insensitive
15 * order, is double-null terminated, and is a char *, not a char **
17 int _cdecl compare(const void *a1, const void *a2)
19 return _stricoll(*((char**)a1),*((char**)a2));
21 bool_t
22 arr2envblk(char **arr, char **envblk_out)
24 char **tmp;
25 int size_needed;
26 int arrcnt;
27 char *ptr;
29 arrcnt = 0;
30 while (arr[arrcnt]) {
31 arrcnt++;
34 tmp = (char**) calloc(arrcnt + 1, sizeof(char *));
35 if (!tmp) {
36 return FALSE;
39 arrcnt = 0;
40 size_needed = 0;
41 while (arr[arrcnt]) {
42 tmp[arrcnt] = arr[arrcnt];
43 size_needed += strlen(arr[arrcnt]) + 1;
44 arrcnt++;
46 size_needed++;
48 qsort((void *) tmp, (size_t) arrcnt, sizeof (char*), compare);
50 ptr = *envblk_out = calloc(size_needed, 1);
51 if (!ptr) {
52 free(tmp);
53 return FALSE;
56 arrcnt = 0;
57 while (tmp[arrcnt]) {
58 strcpy(ptr, tmp[arrcnt]);
59 ptr += strlen(tmp[arrcnt]) + 1;
60 arrcnt++;
63 free(tmp);
64 return TRUE;