xref: /plan9/sys/src/cmd/gs/src/gp_wgetv.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gp_wgetv.c,v 1.6 2002/02/21 22:24:52 giles Exp $ */
18 /* MS Windows implementation of gp_getenv */
19 
20 #include <windows.h>
21 #include <stdio.h>
22 #include <stdlib.h>		/* for getenv */
23 #include <string.h>
24 #include "gscdefs.h"		/* for gs_productfamily and gs_revision */
25 
26 /* prototypes */
27 int gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name,
28     char *ptr, int *plen);
29 
30 /* ------ Environment variables ------ */
31 
32 /* Get the value of an environment variable.  See gp.h for details. */
33 int
gp_getenv(const char * name,char * ptr,int * plen)34 gp_getenv(const char *name, char *ptr, int *plen)
35 {
36     const char *str = getenv(name);
37 
38     if (str) {
39 	int len = strlen(str);
40 
41 	if (len < *plen) {
42 	    /* string fits */
43 	    strcpy(ptr, str);
44 	    *plen = len + 1;
45 	    return 0;
46 	}
47 	/* string doesn't fit */
48 	*plen = len + 1;
49 	return -1;
50     }
51     /* environment variable was not found */
52 
53 #ifdef __WIN32__
54     {
55 	/* If using Win32, look in the registry for a value with
56 	 * the given name.  The registry value will be under the key
57 	 * HKEY_CURRENT_USER\Software\AFPL Ghostscript\N.NN
58 	 * or if that fails under the key
59 	 * HKEY_LOCAL_MACHINE\Software\AFPL Ghostscript\N.NN
60 	 * where "AFPL Ghostscript" is actually gs_productfamily
61 	 * and N.NN is obtained from gs_revision.
62 	 */
63 	DWORD version = GetVersion();
64 
65 	if (!(((HIWORD(version) & 0x8000) != 0)
66 	      && ((HIWORD(version) & 0x4000) == 0))) {
67 	    /* not Win32s */
68 	    int code;
69 	    char key[256];
70 	    char dotversion[16];
71 
72 	    sprintf(dotversion, "%d.%02d", (int)(gs_revision / 100),
73 		    (int)(gs_revision % 100));
74 	    sprintf(key, "Software\\%s\\%s", gs_productfamily, dotversion);
75 
76 	    code = gp_getenv_registry(HKEY_CURRENT_USER, key, name, ptr, plen);
77 	    if ( code <= 0 )
78 		return code;	/* found it */
79 
80 	    code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, name, ptr, plen);
81 	    if ( code <= 0 )
82 		return code;	/* found it */
83 	}
84     }
85 #endif
86 
87     /* nothing found at all */
88 
89     if (*plen > 0)
90 	*ptr = 0;
91     *plen = 1;
92     return 1;
93 }
94 
95 
96 /*
97  * Get a named registry value.
98  * Key = hkeyroot\\key, named value = name.
99  * name, ptr, plen and return values are the same as in gp_getenv();
100  */
101 
102 int
gp_getenv_registry(HKEY hkeyroot,const char * key,const char * name,char * ptr,int * plen)103 gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name,
104     char *ptr, int *plen)
105 {
106     HKEY hkey;
107     DWORD cbData, keytype;
108     BYTE b;
109     LONG rc;
110     BYTE *bptr = (BYTE *)ptr;
111 
112     if (RegOpenKeyEx(hkeyroot, key, 0, KEY_READ, &hkey)
113 	== ERROR_SUCCESS) {
114 	keytype = REG_SZ;
115 	cbData = *plen;
116 	if (bptr == (char *)NULL)
117 	    bptr = &b;	/* Registry API won't return ERROR_MORE_DATA */
118 			/* if ptr is NULL */
119 	rc = RegQueryValueEx(hkey, (char *)name, 0, &keytype, bptr, &cbData);
120 	RegCloseKey(hkey);
121 	if (rc == ERROR_SUCCESS) {
122 	    *plen = cbData;
123 	    return 0;	/* found environment variable and copied it */
124 	} else if (rc == ERROR_MORE_DATA) {
125 	    /* buffer wasn't large enough */
126 	    *plen = cbData;
127 	    return -1;
128 	}
129     }
130     return 1;	/* not found */
131 }
132