Imported Upstream version 3.82
[platform/upstream/make.git] / w32 / subproc / misc.c
1 /* Process handling for Windows
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GNU Make.
5
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <windows.h>
23 #include "proc.h"
24
25
26 /*
27  * Description:  Convert a NULL string terminated UNIX environment block to
28  *              an environment block suitable for a windows32 system call
29  *
30  * Returns:  TRUE= success, FALSE=fail
31  *
32  * Notes/Dependencies:  the environment block is sorted in case-insensitive
33  *      order, is double-null terminated, and is a char *, not a char **
34  */
35 int _cdecl compare(const void *a1, const void *a2)
36 {
37         return _stricoll(*((char**)a1),*((char**)a2));
38 }
39 bool_t
40 arr2envblk(char **arr, char **envblk_out)
41 {
42         char **tmp;
43         int size_needed;
44         int arrcnt;
45         char *ptr;
46
47         arrcnt = 0;
48         while (arr[arrcnt]) {
49                 arrcnt++;
50         }
51
52         tmp = (char**) calloc(arrcnt + 1, sizeof(char *));
53         if (!tmp) {
54                 return FALSE;
55         }
56
57         arrcnt = 0;
58         size_needed = 0;
59         while (arr[arrcnt]) {
60                 tmp[arrcnt] = arr[arrcnt];
61                 size_needed += strlen(arr[arrcnt]) + 1;
62                 arrcnt++;
63         }
64         size_needed++;
65
66         qsort((void *) tmp, (size_t) arrcnt, sizeof (char*), compare);
67
68         ptr = *envblk_out = calloc(size_needed, 1);
69         if (!ptr) {
70                 free(tmp);
71                 return FALSE;
72         }
73
74         arrcnt = 0;
75         while (tmp[arrcnt]) {
76                 strcpy(ptr, tmp[arrcnt]);
77                 ptr += strlen(tmp[arrcnt]) + 1;
78                 arrcnt++;
79         }
80
81         free(tmp);
82         return TRUE;
83 }