| [632] | 1 |  | 
|---|
|  | 2 | /*********************************************************************** | 
|---|
|  | 3 |  | 
|---|
|  | 4 | $Id: srchpath.c 1761 2014-03-21 23:29:52Z stevenhl $ | 
|---|
|  | 5 |  | 
|---|
| [809] | 6 | Path search functions | 
|---|
| [632] | 7 |  | 
|---|
|  | 8 | Copyright (c) 1993-98 M. Kimes | 
|---|
| [1498] | 9 | Copyright (c) 2003, 2010 Steven H. Levine | 
|---|
| [632] | 10 |  | 
|---|
|  | 11 | 22 Apr 07 GKY Add RunFM2Util to find and run apps from the FM2Utilities | 
|---|
| [793] | 12 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat | 
|---|
| [809] | 13 | 23 Aug 07 SHL Comments | 
|---|
| [1241] | 14 | 04 Oct 08 JBS Make searchapath non-static | 
|---|
| [1498] | 15 | 17 JAN 10 GKY Changes to get working with Watcom 1.9 Beta (1/16/10). Mostly cast CHAR CONSTANT * as CHAR *. | 
|---|
| [1750] | 16 | 01 Mar 14 JBS Ticket #524: Made "searchapath" thread-safe. Function names and signatures were changed. | 
|---|
| [1761] | 17 | 21 Mar 14 SHL SearchPathForFile: use IsAbsolutePath | 
|---|
| [632] | 18 |  | 
|---|
|  | 19 | ***********************************************************************/ | 
|---|
| [907] | 20 |  | 
|---|
|  | 21 | #include <string.h> | 
|---|
|  | 22 |  | 
|---|
| [632] | 23 | #define INCL_WIN | 
|---|
| [2] | 24 | #define INCL_DOS | 
|---|
| [1750] | 25 | #define INCL_DOSERRORS | 
|---|
| [907] | 26 | #define INCL_LONGLONG                   // dircnrs.h | 
|---|
| [2] | 27 |  | 
|---|
| [1212] | 28 | #include "fm3dll.h" | 
|---|
| [632] | 29 | #include "fm3dlg.h" | 
|---|
|  | 30 | #include "fm3str.h" | 
|---|
| [1241] | 31 | #include "srchpath.h" | 
|---|
| [907] | 32 | #include "pathutil.h"                   // BldQuotedFileName | 
|---|
|  | 33 | #include "errutil.h"                    // Dos_Error... | 
|---|
| [1184] | 34 | #include "systemf.h"                    // runemf2 | 
|---|
| [1212] | 35 | #include "notebook.h"                   // Data declaration(s) | 
|---|
| [1398] | 36 | #include "init.h"                       // Data declaration(s) | 
|---|
| [632] | 37 |  | 
|---|
|  | 38 | static PSZ pszSrcFile = __FILE__; | 
|---|
|  | 39 |  | 
|---|
| [1184] | 40 | // static CHAR *first_path(CHAR * path, CHAR * ret); | 
|---|
|  | 41 |  | 
|---|
| [2] | 42 | #pragma data_seg(DATA1) | 
|---|
|  | 43 |  | 
|---|
| [632] | 44 | //== RunFM2Util() Find and run an app from the FM2utilities == | 
|---|
|  | 45 | //== Search PATH plus 2 default install dirs == | 
|---|
|  | 46 |  | 
|---|
| [1398] | 47 | INT RunFM2Util(PCSZ appname, CHAR *filename) | 
|---|
| [632] | 48 | { | 
|---|
|  | 49 | CHAR fbuf[CCHMAXPATH]; | 
|---|
| [891] | 50 | CHAR szQuotedFileName[CCHMAXPATH]; | 
|---|
| [632] | 51 | APIRET rc, ret = -1; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | rc = DosSearchPath(SEARCH_IGNORENETERRS |SEARCH_ENVIRONMENT | | 
|---|
| [1498] | 54 | SEARCH_CUR_DIRECTORY, (CHAR *) PCSZ_PATH, | 
|---|
|  | 55 | (CHAR *) appname, (PBYTE)fbuf, CCHMAXPATH - 1); | 
|---|
| [632] | 56 | if (rc != 0) { | 
|---|
|  | 57 | if (rc != 2){ | 
|---|
|  | 58 | Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__, | 
|---|
| [1398] | 59 | PCSZ_DOSSEARCHPATH, appname); | 
|---|
| [632] | 60 | return ret; | 
|---|
|  | 61 | } | 
|---|
|  | 62 | else { | 
|---|
|  | 63 | rc = DosSearchPath(0, "UTILS;..\\FM2Utils", | 
|---|
| [1498] | 64 | (CHAR *) appname, (PBYTE)fbuf, CCHMAXPATH - 1); | 
|---|
| [632] | 65 | if (rc != 0 && rc != 2){ | 
|---|
|  | 66 | Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__, | 
|---|
| [1398] | 67 | PCSZ_DOSSEARCHPATH, appname); | 
|---|
| [632] | 68 | return ret; | 
|---|
|  | 69 | } | 
|---|
|  | 70 | } | 
|---|
|  | 71 | } | 
|---|
|  | 72 | ret = runemf2(SEPARATE | WINDOWED, | 
|---|
| [888] | 73 | HWND_DESKTOP, pszSrcFile, __LINE__, | 
|---|
| [632] | 74 | NULL, | 
|---|
|  | 75 | NULL, | 
|---|
| [891] | 76 | "%s %s", | 
|---|
|  | 77 | fbuf, BldQuotedFileName(szQuotedFileName, filename)); | 
|---|
| [632] | 78 | return ret; | 
|---|
|  | 79 | } | 
|---|
|  | 80 |  | 
|---|
| [1193] | 81 | #if 0   // JBS  11 Sep 08 | 
|---|
| [551] | 82 | CHAR *first_path(CHAR * path, CHAR * ret) | 
|---|
|  | 83 | { | 
|---|
| [2] | 84 |  | 
|---|
| [551] | 85 | CHAR *p, *pp; | 
|---|
| [2] | 86 |  | 
|---|
| [551] | 87 | if (!path || !ret) | 
|---|
| [2] | 88 | return ret; | 
|---|
| [551] | 89 | strcpy(ret, path); | 
|---|
|  | 90 | p = strchr(ret, ';'); | 
|---|
|  | 91 | if (p) { | 
|---|
| [2] | 92 | *p = 0; | 
|---|
|  | 93 | p++; | 
|---|
| [1673] | 94 | if (*ret == '.') {                  // skip initial "cur dir" | 
|---|
| [551] | 95 | pp = strchr(p, ';'); | 
|---|
|  | 96 | if (pp) | 
|---|
|  | 97 | *pp = 0; | 
|---|
|  | 98 | if (*p) | 
|---|
|  | 99 | memmove(ret, p, strlen(p) + 1); | 
|---|
| [2] | 100 | } | 
|---|
|  | 101 | } | 
|---|
|  | 102 | return ret; | 
|---|
|  | 103 | } | 
|---|
| [1184] | 104 | #endif | 
|---|
| [2] | 105 |  | 
|---|
| [809] | 106 | /** | 
|---|
| [1750] | 107 | * SearchPathForFile: Search for a file along a path | 
|---|
|  | 108 | * | 
|---|
| [1761] | 109 | * @param pszEnvVarName: the name of a path environment variable (input) | 
|---|
|  | 110 | *    Used only if pFilename has not an absoluate path | 
|---|
| [1750] | 111 | * | 
|---|
|  | 112 | * @param pFilename: the name of a file to search for (input) | 
|---|
| [1761] | 113 | *    If the file name is an absolute path then | 
|---|
| [1750] | 114 | *        DosQueryPathInfo is used for the search | 
|---|
|  | 115 | *    else | 
|---|
| [1761] | 116 | *        DosSearchPath is used, along with the pszEnvVarName parameter | 
|---|
| [1750] | 117 | * | 
|---|
|  | 118 | * @param pFullFilename: address of where to place fully-qulified name if search succeeds (output) | 
|---|
|  | 119 | *    This parameter may be NULL if the fully-qualified filename is not desired. | 
|---|
|  | 120 | * | 
|---|
|  | 121 | * @return Return code from call to DosQueryPathInfo/DosSearchPath | 
|---|
|  | 122 | * | 
|---|
| [809] | 123 | */ | 
|---|
| [1761] | 124 | APIRET SearchPathForFile(PCSZ pszEnvVarName, | 
|---|
| [1750] | 125 | PCSZ pFilename, | 
|---|
|  | 126 | PCHAR pFullFilename) | 
|---|
| [551] | 127 | { | 
|---|
| [1750] | 128 | APIRET rc; | 
|---|
|  | 129 | CHAR szFullFilename[CCHMAXPATH]; | 
|---|
| [2] | 130 |  | 
|---|
| [1761] | 131 | if (!pszEnvVarName || !*pszEnvVarName || !pFilename || !*pFilename) | 
|---|
| [1750] | 132 | return ERROR_INVALID_PARAMETER; | 
|---|
| [2] | 133 |  | 
|---|
| [1761] | 134 | if (IsAbsolutePath(pFilename)) { | 
|---|
| [1750] | 135 | rc = DosQueryPathInfo(pFilename, FIL_QUERYFULLNAME, | 
|---|
| [1761] | 136 | (PVOID)szFullFilename, (ULONG)CCHMAXPATH-1); | 
|---|
| [2] | 137 | } | 
|---|
| [1750] | 138 | else { | 
|---|
|  | 139 | rc = DosSearchPath(SEARCH_IGNORENETERRS | SEARCH_ENVIRONMENT | | 
|---|
|  | 140 | SEARCH_CUR_DIRECTORY, | 
|---|
| [1761] | 141 | (CHAR *) pszEnvVarName, | 
|---|
| [1750] | 142 | (CHAR *) pFilename, | 
|---|
|  | 143 | (PBYTE) szFullFilename, | 
|---|
|  | 144 | CCHMAXPATH - 1); | 
|---|
|  | 145 | } | 
|---|
| [1761] | 146 | if (!rc && pFullFilename) | 
|---|
| [1750] | 147 | strcpy(pFullFilename, szFullFilename); | 
|---|
| [1761] | 148 |  | 
|---|
| [1750] | 149 | return rc; | 
|---|
| [2] | 150 | } | 
|---|
|  | 151 |  | 
|---|
| [1750] | 152 | /** | 
|---|
|  | 153 | * SearchMultiplePathsForFile: Search for a file along multiple paths. | 
|---|
|  | 154 | *   Currently these paths are hard-coded to: PATH, DPATH and XPATH. | 
|---|
|  | 155 | * | 
|---|
|  | 156 | * @param pFilename: the name of a file to search for (input) | 
|---|
|  | 157 | * | 
|---|
|  | 158 | * @param pFullFilename: address of where to place fully-qulified name if search succeeds (output) | 
|---|
|  | 159 | *    This parameter may be NULL if the fully-qualified filename is not desired. | 
|---|
|  | 160 | * | 
|---|
|  | 161 | * @return Return code from call to SearchPathForFile (DosQueryPathInfo/DosSearchPath) | 
|---|
|  | 162 | * | 
|---|
|  | 163 | * @note: The code uses DosSearchPathForFile for all searches. First it searches PATH. | 
|---|
|  | 164 | *        If this fails it searches DPATH. If this fails it seaches XPATH. | 
|---|
|  | 165 | * | 
|---|
|  | 166 | */ | 
|---|
|  | 167 | APIRET SearchMultiplePathsForFile(PCSZ pFilename, | 
|---|
|  | 168 | PSZ pFullFilename) | 
|---|
| [551] | 169 | { | 
|---|
| [1750] | 170 | APIRET rc; | 
|---|
| [2] | 171 |  | 
|---|
| [1750] | 172 | rc = SearchPathForFile(PCSZ_PATH, | 
|---|
|  | 173 | pFilename, | 
|---|
|  | 174 | pFullFilename); | 
|---|
|  | 175 | if (rc && rc != ERROR_INVALID_PARAMETER) { | 
|---|
|  | 176 | rc = SearchPathForFile("DPATH", | 
|---|
|  | 177 | pFilename, | 
|---|
|  | 178 | pFullFilename); | 
|---|
|  | 179 | if (rc && rc != ERROR_INVALID_PARAMETER) | 
|---|
|  | 180 | rc = SearchPathForFile("XPATH", | 
|---|
|  | 181 | pFilename, | 
|---|
|  | 182 | pFullFilename); | 
|---|
| [2] | 183 | } | 
|---|
| [1750] | 184 | return rc; | 
|---|
| [2] | 185 | } | 
|---|
| [793] | 186 |  | 
|---|
| [1750] | 187 | //#pragma alloc_text(MISC9,first_path,searchapath,searchpath,RunFM2Util) | 
|---|
|  | 188 | // jbs: first_path seems to be unused | 
|---|
|  | 189 | #pragma alloc_text(MISC9,first_path,SearchPathForFile,SearchMultiplePathsForFile,RunFM2Util) | 
|---|