/* <string> getenv false */ static int zgetenv(i_ctx_t *i_ctx_p) { os_ptr op = osp; char *str; byte *value; int len = 0; check_read_type(*op, t_string); str = ref_to_string(op, imemory, "getenv key"); if (str == 0) return_error(e_VMerror); if (gp_getenv(str, (char *)0, &len) > 0) { /* key missing */ ifree_string((byte *) str, r_size(op) + 1, "getenv key"); make_false(op); return 0; } value = ialloc_string(len, "getenv value"); if (value == 0) { ifree_string((byte *) str, r_size(op) + 1, "getenv key"); return_error(e_VMerror); } DISCARD(gp_getenv(str, (char *)value, &len)); /* can't fail */ ifree_string((byte *) str, r_size(op) + 1, "getenv key"); /* Delete the stupid C string terminator. */ value = iresize_string(value, len, len - 1, "getenv value"); /* can't fail */ push(1); make_string(op - 1, a_all | icurrent_space, len - 1, value); make_true(op); return 0; }
/* get the cache directory's path */ static char *gp_cache_prefix(void) { char *prefix = NULL; int plen = 0; /* get the cache directory path */ if (gp_getenv("GS_CACHE_DIR", (char *)NULL, &plen) < 0) { prefix = malloc(plen); gp_getenv("GS_CACHE_DIR", prefix, &plen); plen--; } else { #ifdef GS_CACHE_DIR prefix = strdup(GS_CACHE_DIR); #else prefix = strdup(".cache"); #endif plen = strlen(prefix); } /* substitute $HOME for '~' */ if (plen >= 1 && prefix[0] == '~') { char *home, *path; int hlen = 0; unsigned int pathlen = 0; gp_file_name_combine_result result; if (gp_getenv("HOME", (char *)NULL, &hlen) < 0) { home = malloc(hlen); if (home == NULL) return prefix; gp_getenv("HOME", home, &hlen); hlen--; if (plen == 1) { /* only "~" */ free(prefix); return home; } /* substitue for the initial '~' */ pathlen = hlen + plen + 1; path = malloc(pathlen); if (path == NULL) { free(home); return prefix; } result = gp_file_name_combine(home, hlen, prefix+2, plen-2, false, path, &pathlen); if (result == gp_combine_success) { free(prefix); prefix = path; } else { dlprintf1("file_name_combine failed with code %d\n", result); } free(home); } } #ifdef DEBUG_CACHE dlprintf1("cache dir read as '%s'\n", prefix); #endif return prefix; }
/* * Get the name of the directory for temporary files, if any. Currently * this checks the TMPDIR and TEMP environment variables, in that order. * The return value and the setting of *ptr and *plen are as for gp_getenv. */ int gp_gettmpdir(char *ptr, int *plen) { int max_len = *plen; int code = gp_getenv("TMPDIR", ptr, plen); if (code != 1) return code; *plen = max_len; return gp_getenv("TEMP", ptr, plen); }
static int get_pipe_name(char *name) { const char *socket; int ret; socket = gp_getenv("GSSPROXY_SOCKET"); if (!socket) { socket = GP_SOCKET_NAME; } ret = snprintf(name, PATH_MAX, "%s", socket); if (ret < 0 || ret >= PATH_MAX) { return ENAMETOOLONG; } return 0; }
int load_dll(GSDLL *gsdll, char *last_error, int len) { char fullname[1024]; char *p; int length; gsapi_revision_t rv; /* Don't load if already loaded */ if (gsdll->hmodule) return 0; /* First try to load DLL from the same directory as EXE */ GetModuleFileName(GetModuleHandle(NULL), fullname, sizeof(fullname)); if ((p = strrchr(fullname,'\\')) != (char *)NULL) p++; else p = fullname; *p = '\0'; strcat(fullname, name); gsdll->hmodule = LoadLibrary(fullname); /* Next try to load DLL with name in registry or environment variable */ if (gsdll->hmodule < (HINSTANCE)HINSTANCE_ERROR) { length = sizeof(fullname); if (gp_getenv("GS_DLL", fullname, &length) == 0) gsdll->hmodule = LoadLibrary(fullname); } /* Finally try the system search path */ if (gsdll->hmodule < (HINSTANCE)HINSTANCE_ERROR) gsdll->hmodule = LoadLibrary(name); if (gsdll->hmodule < (HINSTANCE)HINSTANCE_ERROR) { /* Failed */ DWORD err = GetLastError(); sprintf(fullname, "Can't load DLL, LoadLibrary error code %ld", err); strncpy(last_error, fullname, len-1); gsdll->hmodule = (HINSTANCE)0; return 1; } /* DLL is now loaded */ /* Get pointers to functions */ gsdll->revision = (PFN_gsapi_revision) GetProcAddress(gsdll->hmodule, "gsapi_revision"); if (gsdll->revision == NULL) { strncpy(last_error, "Can't find gsapi_revision\n", len-1); unload_dll(gsdll); return 1; } /* check DLL version */ if (gsdll->revision(&rv, sizeof(rv)) != 0) { sprintf(fullname, "Unable to identify Ghostscript DLL revision - it must be newer than needed.\n"); strncpy(last_error, fullname, len-1); unload_dll(gsdll); return 1; } if (rv.revision != GSREVISION) { sprintf(fullname, "Wrong version of DLL found.\n Found version %ld\n Need version %ld\n", rv.revision, GSREVISION); strncpy(last_error, fullname, len-1); unload_dll(gsdll); return 1; } /* continue loading other functions */ gsdll->new_instance = (PFN_gsapi_new_instance) GetProcAddress(gsdll->hmodule, "gsapi_new_instance"); if (gsdll->new_instance == NULL) { strncpy(last_error, "Can't find gsapi_new_instance\n", len-1); unload_dll(gsdll); return 1; } gsdll->delete_instance = (PFN_gsapi_delete_instance) GetProcAddress(gsdll->hmodule, "gsapi_delete_instance"); if (gsdll->delete_instance == NULL) { strncpy(last_error, "Can't find gsapi_delete_instance\n", len-1); unload_dll(gsdll); return 1; } gsdll->set_stdio = (PFN_gsapi_set_stdio) GetProcAddress(gsdll->hmodule, "gsapi_set_stdio"); if (gsdll->set_stdio == NULL) { strncpy(last_error, "Can't find gsapi_set_stdio\n", len-1); unload_dll(gsdll); return 1; } gsdll->set_poll = (PFN_gsapi_set_poll) GetProcAddress(gsdll->hmodule, "gsapi_set_poll"); if (gsdll->set_poll == NULL) { strncpy(last_error, "Can't find gsapi_set_poll\n", len-1); unload_dll(gsdll); return 1; } gsdll->set_display_callback = (PFN_gsapi_set_display_callback) GetProcAddress(gsdll->hmodule, "gsapi_set_display_callback"); if (gsdll->set_display_callback == NULL) { strncpy(last_error, "Can't find gsapi_set_display_callback\n", len-1); unload_dll(gsdll); return 1; } gsdll->get_default_device_list = (PFN_gsapi_get_default_device_list) GetProcAddress(gsdll->hmodule, "gsapi_get_default_device_list"); if (gsdll->get_default_device_list == NULL) { strncpy(last_error, "Can't find gsapi_get_default_device_list\n", len-1); unload_dll(gsdll); return 1; } gsdll->set_default_device_list = (PFN_gsapi_set_default_device_list) GetProcAddress(gsdll->hmodule, "gsapi_set_default_device_list"); if (gsdll->set_default_device_list == NULL) { strncpy(last_error, "Can't find gsapi_set_default_device_list\n", len-1); unload_dll(gsdll); return 1; } gsdll->init_with_args = (PFN_gsapi_init_with_args) GetProcAddress(gsdll->hmodule, "gsapi_init_with_args"); if (gsdll->init_with_args == NULL) { strncpy(last_error, "Can't find gsapi_init_with_args\n", len-1); unload_dll(gsdll); return 1; } gsdll->set_arg_encoding = (PFN_gsapi_set_arg_encoding) GetProcAddress(gsdll->hmodule, "gsapi_set_arg_encoding"); if (gsdll->set_arg_encoding == NULL) { strncpy(last_error, "Can't find gsapi_set_arg_encoding\n", len-1); unload_dll(gsdll); return 1; } gsdll->run_string = (PFN_gsapi_run_string) GetProcAddress(gsdll->hmodule, "gsapi_run_string"); if (gsdll->run_string == NULL) { strncpy(last_error, "Can't find gsapi_run_string\n", len-1); unload_dll(gsdll); return 1; } gsdll->exit = (PFN_gsapi_exit) GetProcAddress(gsdll->hmodule, "gsapi_exit"); if (gsdll->exit == NULL) { strncpy(last_error, "Can't find gsapi_exit\n", len-1); unload_dll(gsdll); return 1; } gsdll->set_visual_tracer = (PFN_gsapi_set_visual_tracer) GetProcAddress(gsdll->hmodule, "gsapi_set_visual_tracer"); if (gsdll->set_visual_tracer == NULL) { strncpy(last_error, "Can't find gsapi_set_visual_tracer\n", len-1); unload_dll(gsdll); return 1; } return 0; }