1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gp_wgetv.c 8022 2007-06-05 22:23:38Z giles $ */
15
/* MS Windows implementation of gp_getenv */
19
#include <stdlib.h> /* for getenv */
21
#include "gscdefs.h" /* for gs_productfamily and gs_revision */
24
int gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name,
25
char *ptr, int *plen);
27
/* ------ Environment variables ------ */
29
/* Get the value of an environment variable. See gp.h for details. */
31
gp_getenv(const char *name, char *ptr, int *plen)
33
const char *str = getenv(name);
36
int len = strlen(str);
44
/* string doesn't fit */
48
/* environment variable was not found */
52
/* If using Win32, look in the registry for a value with
53
* the given name. The registry value will be under the key
54
* HKEY_CURRENT_USER\Software\GPL Ghostscript\N.NN
55
* or if that fails under the key
56
* HKEY_LOCAL_MACHINE\Software\GPL Ghostscript\N.NN
57
* where "GPL Ghostscript" is actually gs_productfamily
58
* and N.NN is obtained from gs_revision.
60
DWORD version = GetVersion();
62
if (!(((HIWORD(version) & 0x8000) != 0)
63
&& ((HIWORD(version) & 0x4000) == 0))) {
69
sprintf(dotversion, "%d.%02d", (int)(gs_revision / 100),
70
(int)(gs_revision % 100));
71
sprintf(key, "Software\\%s\\%s", gs_productfamily, dotversion);
73
code = gp_getenv_registry(HKEY_CURRENT_USER, key, name, ptr, plen);
75
return code; /* found it */
77
code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, name, ptr, plen);
79
return code; /* found it */
84
/* nothing found at all */
94
* Get a named registry value.
95
* Key = hkeyroot\\key, named value = name.
96
* name, ptr, plen and return values are the same as in gp_getenv();
100
gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name,
101
char *ptr, int *plen)
104
DWORD cbData, keytype;
107
BYTE *bptr = (BYTE *)ptr;
109
if (RegOpenKeyEx(hkeyroot, key, 0, KEY_READ, &hkey)
113
if (bptr == (char *)NULL)
114
bptr = &b; /* Registry API won't return ERROR_MORE_DATA */
116
rc = RegQueryValueEx(hkey, (char *)name, 0, &keytype, bptr, &cbData);
118
if (rc == ERROR_SUCCESS) {
120
return 0; /* found environment variable and copied it */
121
} else if (rc == ERROR_MORE_DATA) {
122
/* buffer wasn't large enough */
127
return 1; /* not found */