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