Pedro,

Attached is a proposed implementation for getenv, setenv, unsetenv. I
would put this in src/mingw/mingwex/getenv.c . A test program is in the
other attachment.

I would propose using this to get libgcov to work on mingw32ce.

Comments, or can I commit and change libgcov to use this ?

        Danny
-- 
Danny Backx ; danny.backx - at - scarlet.be ; http://danny.backx.info
Index: Makefile.in
===================================================================
--- Makefile.in	(revision 856)
+++ Makefile.in	(working copy)
@@ -85,7 +85,8 @@
 
 WINCE_DISTFILES = \
 	asctime.c freopen.c gmtime.c localtime.c mktime.c strftime.c time.c \
-	tempnam.c unlink.c wcsftime.c fdopen.c read.c write.c open.c lseek.c close.c
+	tempnam.c unlink.c wcsftime.c fdopen.c read.c write.c open.c lseek.c close.c \
+	getenv.c
 
 CC = @CC@
 # FIXME: Which is it, CC or CC_FOR_TARGET?
@@ -186,7 +187,7 @@
 WINCE_OBJS = \
 	asctime.o freopen.o gmtime.o localtime.o mktime.o strftime.o time.o \
 	tempnam.o unlink.o wcsftime.o fdopen.o read.o write.o open.o lseek.o \
-	close.o
+	close.o getenv.o
 
 ifneq (,$(findstring wince,$(target_alias)))
 LIB_OBJS = $(WINCE_OBJS)
Index: getenv.c
===================================================================
--- getenv.c	(revision 0)
+++ getenv.c	(revision 0)
@@ -0,0 +1,126 @@
+/*
+ * Functions to mimic getenv/setenv on Windows.
+ * These functions are written by Danny Backx (c) 2006 for the CeGCC project
+ * and are in the public domain.
+ *
+ * We store stuff in HKEY_CURRENT_USER under the key "environment".
+ *
+ * Current limitations :
+ * - Only keys which are simple strings can be read. See the switch statement in getenv.
+ * - As in UNIX implementations, getenv returns a pointer to a static structure,
+ *   whose value is only guaranteed until the next call to getenv.
+ * - The buffer size of that structure is predefined to 256 bytes.
+ */
+#include <windows.h>
+#include <winreg.h>
+#include <stdlib.h>
+
+#define	BUF_SIZE	256
+
+static char	buffer[BUF_SIZE];
+static wchar_t	wbuffer[BUF_SIZE];
+static DWORD	bufsize;
+
+const static wchar_t	*env = L"environment";
+
+char *getenv(const char *name)
+{
+	LONG	r;
+	wchar_t	*wname;
+	size_t	l;
+	DWORD	tp;
+	HKEY	k;
+
+	r = RegOpenKeyEx(HKEY_CURRENT_USER, env, 0, 0, &k);
+	if (r != ERROR_SUCCESS)
+		return -1;
+
+	l = mbstowcs(NULL, name, 0);
+	wname = (wchar_t *)calloc(l+1, sizeof(wchar_t));
+	(void)mbstowcs(wname, name, l+1);
+	wname[l] = 0;
+
+	bufsize = BUF_SIZE;
+	r = RegQueryValueEx(k, wname, NULL, &tp, (LPBYTE)&wbuffer[0], &bufsize);
+	RegCloseKey(k);
+
+	if (r != ERROR_SUCCESS)
+		return NULL;
+
+	switch (tp) {
+	case REG_SZ:
+		wcstombs(buffer, wbuffer, bufsize);
+		return &buffer[0];
+	case REG_NONE:
+	case REG_BINARY:
+	case REG_DWORD:
+	case REG_DWORD_BIG_ENDIAN:
+	case REG_EXPAND_SZ:
+	case REG_LINK:
+	case REG_MULTI_SZ:
+	case REG_RESOURCE_LIST:
+	default:
+		return NULL;
+	}
+	return NULL;
+}
+
+int setenv(const char *name, const char *value)
+{
+	LONG	r;
+	wchar_t	*wname, *wvalue;
+	size_t	l;
+	HKEY	k;
+	DWORD	disp;
+
+	/* class ? */
+	r = RegCreateKeyEx(HKEY_CURRENT_USER, env, 0, L"class", 0, 0, NULL, &k, &disp);
+	/* Now disp == REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY */
+
+	if (r != ERROR_SUCCESS)
+		return -1;
+
+	l = mbstowcs(NULL, name, 0);
+	wname = (wchar_t *)calloc(l+1, sizeof(wchar_t));
+	(void)mbstowcs(wname, name, l+1);
+	wname[l] = 0;
+
+	l = mbstowcs(NULL, value, 0);
+	wvalue = (wchar_t *)calloc(l+1, sizeof(wchar_t));
+	l = mbstowcs(wvalue, value, l+1);
+	wvalue[l] = 0;
+
+	r = RegSetValueEx(k, wname, 0, REG_SZ, wvalue, (l+1) * sizeof(wchar_t));
+	RegCloseKey(k);
+	free(wname);
+	free(wvalue);
+	if (r != ERROR_SUCCESS)
+		return -1;
+	return 0;
+}
+
+int unsetenv(const char *name)
+{
+	LONG	r;
+	wchar_t	*wname;
+	size_t	l;
+	DWORD	tp;
+	HKEY	k;
+
+	r = RegOpenKeyEx(HKEY_CURRENT_USER, env, 0, 0, &k);
+	if (r != ERROR_SUCCESS)
+		return -1;
+
+	l = mbstowcs(NULL, name, 0);
+	wname = (wchar_t *)calloc(l+1, sizeof(wchar_t));
+	(void)mbstowcs(wname, name, l+1);
+	wname[l] = 0;
+
+	r = RegDeleteValue(k, wname);
+	RegCloseKey(k);
+
+	if (r != ERROR_SUCCESS)
+		return -1;
+
+	return 0;
+}

Property changes on: getenv.c
___________________________________________________________________
Name: svn:eol-style
   + native

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int APIENTRY WinMain(HINSTANCE p1,HINSTANCE p2,LPWSTR p3,int p4)
{
	FILE	*fp;
	int	r;
	char	*s;

	char	*a = "HOME",
		*b = "abc def ghi jkl mno pqr stu vwx yz";

	fp = fopen("/storage card/devel/out.txt", "w");
	r = setenv(a, b);
	fprintf(fp, "setenv(%s,%s) -> %d\n", a, b, r);

	s = getenv(a);
	fprintf(fp, "getenv(%s) => %s\n", a, s);

	unsetenv(a);

	s = getenv(a);
	fprintf(fp, "getenv(%s) => %s\n", a, s);

	fclose(fp);

	exit(0);
}
setenv(HOME,abc def ghi jkl mno pqr stu vwx yz) -> 0
getenv(HOME) => abc def ghi jkl mno pqr stu vwx yz
getenv(HOME) => (null)

Attachment: signature.asc
Description: This is a digitally signed message part

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Cegcc-devel mailing list
Cegcc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cegcc-devel

Reply via email to