1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/* Copyright (C) 2001-2003, Ghostgum Software Pty Ltd. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* This file is part of GSview. The above notice applies only
* to this file. It does NOT apply to any other file in GSview
* unless that file includes the above copyright notice.
*/
/* $Id: wgsver.c,v 1.1.2.4 2003/07/14 08:03:09 ghostgum Exp $ */
/* Obtain details of copies of Ghostscript installed under Windows */
/* To compile as a demo program, define DUMP_GSVER */
/* #define DUMP_GSVER */
#include <windows.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include "wgsver.h"
/* Ghostscript may be known in the Windows Registry by
* the following names.
*/
#define GS_PRODUCT_AFPL "AFPL Ghostscript"
#define GS_PRODUCT_ALADDIN "Aladdin Ghostscript"
#define GS_PRODUCT_GPL "GPL Ghostscript"
#define GS_PRODUCT_GNU "GNU Ghostscript"
/* Get Ghostscript versions for given product.
* Store results starting at pver + 1 + offset.
* Returns total number of versions in pver.
*/
static int get_gs_versions_product(int *pver, int offset,
const char *gs_productfamily)
{
HKEY hkey;
DWORD cbData;
HKEY hkeyroot;
char key[256];
int ver;
char *p;
int n = 0;
sprintf(key, "Software\\%s", gs_productfamily);
hkeyroot = HKEY_LOCAL_MACHINE;
if (RegOpenKeyExA(hkeyroot, key, 0, KEY_READ, &hkey) == ERROR_SUCCESS) {
/* Now enumerate the keys */
cbData = sizeof(key) / sizeof(char);
while (RegEnumKeyA(hkey, n, key, cbData) == ERROR_SUCCESS) {
n++;
ver = 0;
p = key;
while (*p && (*p!='.')) {
ver = (ver * 10) + (*p - '0')*100;
p++;
}
if (*p == '.')
p++;
if (*p) {
ver += (*p - '0') * 10;
p++;
}
if (*p)
ver += (*p - '0');
if (n + offset < pver[0])
pver[n+offset] = ver;
}
}
return n+offset;
}
/* Query registry to find which versions of Ghostscript are installed.
* Return version numbers in an integer array.
* On entry, the first element in the array must be the array size
* in elements.
* If all is well, TRUE is returned.
* On exit, the first element is set to the number of Ghostscript
* versions installed, and subsequent elements to the version
* numbers of Ghostscript.
* e.g. on entry {5, 0, 0, 0, 0}, on exit {3, 550, 600, 596, 0}
* Returned version numbers may not be sorted.
*
* If Ghostscript is not installed at all, return FALSE
* and set pver[0] to 0.
* If the array is not large enough, return FALSE
* and set pver[0] to the number of Ghostscript versions installed.
*/
BOOL get_gs_versions(int *pver)
{
int n;
if (pver == (int *)NULL)
return FALSE;
n = get_gs_versions_product(pver, 0, GS_PRODUCT_AFPL);
n = get_gs_versions_product(pver, n, GS_PRODUCT_ALADDIN);
n = get_gs_versions_product(pver, n, GS_PRODUCT_GPL);
n = get_gs_versions_product(pver, n, GS_PRODUCT_GNU);
if (n >= pver[0]) {
pver[0] = n;
return FALSE; /* too small */
}
if (n == 0) {
pver[0] = 0;
return FALSE; /* not installed */
}
pver[0] = n;
return TRUE;
}
/*
* Get a named registry value.
* Key = hkeyroot\\key, named value = name.
* name, ptr, plen and return values are the same as in gp_getenv();
*/
int
gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name,
char *ptr, int *plen)
{
HKEY hkey;
DWORD cbData, keytype;
BYTE b;
LONG rc;
BYTE *bptr = (BYTE *)ptr;
if (RegOpenKeyExA(hkeyroot, key, 0, KEY_READ, &hkey)
== ERROR_SUCCESS) {
keytype = REG_SZ;
cbData = *plen;
if (bptr == (BYTE *)NULL)
bptr = &b; /* Registry API won't return ERROR_MORE_DATA */
/* if ptr is NULL */
rc = RegQueryValueExA(hkey, (char *)name, 0, &keytype, bptr, &cbData);
RegCloseKey(hkey);
if (rc == ERROR_SUCCESS) {
*plen = cbData;
return 0; /* found environment variable and copied it */
} else if (rc == ERROR_MORE_DATA) {
/* buffer wasn't large enough */
*plen = cbData;
return -1;
}
}
return 1; /* not found */
}
static BOOL get_gs_string_product(int gs_revision, const char *name,
char *ptr, int len, const char *gs_productfamily)
{
/* If using Win32, look in the registry for a value with
* the given name. The registry value will be under the key
* HKEY_CURRENT_USER\Software\AFPL Ghostscript\N.NN
* or if that fails under the key
* HKEY_LOCAL_MACHINE\Software\AFPL Ghostscript\N.NN
* where "AFPL Ghostscript" is actually gs_productfamily
* and N.NN is obtained from gs_revision.
*/
int code;
char key[256];
char dotversion[16];
int length;
DWORD version = GetVersion();
if (((HIWORD(version) & 0x8000) != 0)
&& ((HIWORD(version) & 0x4000) == 0)) {
/* Win32s */
return FALSE;
}
sprintf(dotversion, "%d.%02d",
(int)(gs_revision / 100), (int)(gs_revision % 100));
sprintf(key, "Software\\%s\\%s", gs_productfamily, dotversion);
length = len;
code = gp_getenv_registry(HKEY_CURRENT_USER, key, name, ptr, &length);
if ( code == 0 )
return TRUE; /* found it */
length = len;
code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, name, ptr, &length);
if ( code == 0 )
return TRUE; /* found it */
return FALSE;
}
BOOL get_gs_string(int gs_revision, const char *name, char *ptr, int len)
{
if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_AFPL))
return TRUE;
if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_ALADDIN))
return TRUE;
if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_GPL))
return TRUE;
if (get_gs_string_product(gs_revision, name, ptr, len, GS_PRODUCT_GNU))
return TRUE;
return FALSE;
}
/* Set the latest Ghostscript EXE or DLL from the registry */
BOOL
find_gs(char *gspath, int len, int minver, BOOL bDLL)
{
int count;
int *ver;
int gsver;
char buf[256];
char *p;
int i;
DWORD version = GetVersion();
if ( ((HIWORD(version) & 0x8000)!=0) && ((HIWORD(version) & 0x4000)==0) )
return FALSE; // win32s
count = 1;
get_gs_versions(&count);
if (count < 1)
return FALSE;
ver = (int *)malloc((count+1)*sizeof(int));
if (ver == (int *)NULL)
return FALSE;
ver[0] = count+1;
if (!get_gs_versions(ver)) {
free(ver);
return FALSE;
}
gsver = 0;
for (i=1; i<=ver[0]; i++) {
if (ver[i] > gsver)
gsver = ver[i];
}
free(ver);
if (gsver < minver) // minimum version (e.g. for gsprint)
return FALSE;
if (!get_gs_string(gsver, "GS_DLL", buf, sizeof(buf)))
return FALSE;
if (bDLL) {
strncpy(gspath, buf, len-1);
return TRUE;
}
p = strrchr(buf, '\\');
if (p) {
p++;
*p = 0;
strncpy(p, "gswin32c.exe", sizeof(buf)-1-strlen(buf));
strncpy(gspath, buf, len-1);
return TRUE;
}
return FALSE;
}
#ifdef DUMP_GSVER
/* This is an example of how you can use the above functions */
int main(int argc, char *argv[])
{
BOOL flag;
int n;
int ver[10];
int i;
char buf[256];
if (find_gs(buf, sizeof(buf), 550, TRUE))
printf("Latest GS DLL is %s\n", buf);
if (find_gs(buf, sizeof(buf), 550, FALSE))
printf("Latest GS EXE is %s\n", buf);
ver[0] = sizeof(ver) / sizeof(int);
flag = get_gs_versions(ver);
printf("Versions: %d\n", ver[0]);
if (flag == FALSE) {
printf("get_gs_versions failed, need %d\n", ver[0]);
return 1;
}
for (i=1; i <= ver[0]; i++) {
printf(" %d\n", ver[i]);
if (get_gs_string(ver[i], "GS_DLL", buf, sizeof(buf)))
printf(" GS_DLL=%s\n", buf);
if (get_gs_string(ver[i], "GS_LIB", buf, sizeof(buf)))
printf(" GS_LIB=%s\n", buf);
}
return 0;
}
#endif
|