source: trunk/dll/srchpath.c@ 1761

Last change on this file since 1761 was 1761, checked in by Steven Levine, 11 years ago

Implement IsAbsolutePath and modify SearchPathForFile to use

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1
2/***********************************************************************
3
4 $Id: srchpath.c 1761 2014-03-21 23:29:52Z stevenhl $
5
6 Path search functions
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2003, 2010 Steven H. Levine
10
11 22 Apr 07 GKY Add RunFM2Util to find and run apps from the FM2Utilities
12 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
13 23 Aug 07 SHL Comments
14 04 Oct 08 JBS Make searchapath non-static
15 17 JAN 10 GKY Changes to get working with Watcom 1.9 Beta (1/16/10). Mostly cast CHAR CONSTANT * as CHAR *.
16 01 Mar 14 JBS Ticket #524: Made "searchapath" thread-safe. Function names and signatures were changed.
17 21 Mar 14 SHL SearchPathForFile: use IsAbsolutePath
18
19***********************************************************************/
20
21#include <string.h>
22
23#define INCL_WIN
24#define INCL_DOS
25#define INCL_DOSERRORS
26#define INCL_LONGLONG // dircnrs.h
27
28#include "fm3dll.h"
29#include "fm3dlg.h"
30#include "fm3str.h"
31#include "srchpath.h"
32#include "pathutil.h" // BldQuotedFileName
33#include "errutil.h" // Dos_Error...
34#include "systemf.h" // runemf2
35#include "notebook.h" // Data declaration(s)
36#include "init.h" // Data declaration(s)
37
38static PSZ pszSrcFile = __FILE__;
39
40// static CHAR *first_path(CHAR * path, CHAR * ret);
41
42#pragma data_seg(DATA1)
43
44//== RunFM2Util() Find and run an app from the FM2utilities ==
45//== Search PATH plus 2 default install dirs ==
46
47INT RunFM2Util(PCSZ appname, CHAR *filename)
48{
49 CHAR fbuf[CCHMAXPATH];
50 CHAR szQuotedFileName[CCHMAXPATH];
51 APIRET rc, ret = -1;
52
53 rc = DosSearchPath(SEARCH_IGNORENETERRS |SEARCH_ENVIRONMENT |
54 SEARCH_CUR_DIRECTORY, (CHAR *) PCSZ_PATH,
55 (CHAR *) appname, (PBYTE)fbuf, CCHMAXPATH - 1);
56 if (rc != 0) {
57 if (rc != 2){
58 Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
59 PCSZ_DOSSEARCHPATH, appname);
60 return ret;
61 }
62 else {
63 rc = DosSearchPath(0, "UTILS;..\\FM2Utils",
64 (CHAR *) appname, (PBYTE)fbuf, CCHMAXPATH - 1);
65 if (rc != 0 && rc != 2){
66 Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
67 PCSZ_DOSSEARCHPATH, appname);
68 return ret;
69 }
70 }
71 }
72 ret = runemf2(SEPARATE | WINDOWED,
73 HWND_DESKTOP, pszSrcFile, __LINE__,
74 NULL,
75 NULL,
76 "%s %s",
77 fbuf, BldQuotedFileName(szQuotedFileName, filename));
78 return ret;
79}
80
81#if 0 // JBS 11 Sep 08
82CHAR *first_path(CHAR * path, CHAR * ret)
83{
84
85 CHAR *p, *pp;
86
87 if (!path || !ret)
88 return ret;
89 strcpy(ret, path);
90 p = strchr(ret, ';');
91 if (p) {
92 *p = 0;
93 p++;
94 if (*ret == '.') { // skip initial "cur dir"
95 pp = strchr(p, ';');
96 if (pp)
97 *pp = 0;
98 if (*p)
99 memmove(ret, p, strlen(p) + 1);
100 }
101 }
102 return ret;
103}
104#endif
105
106/**
107 * SearchPathForFile: Search for a file along a path
108 *
109 * @param pszEnvVarName: the name of a path environment variable (input)
110 * Used only if pFilename has not an absoluate path
111 *
112 * @param pFilename: the name of a file to search for (input)
113 * If the file name is an absolute path then
114 * DosQueryPathInfo is used for the search
115 * else
116 * DosSearchPath is used, along with the pszEnvVarName parameter
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 *
123 */
124APIRET SearchPathForFile(PCSZ pszEnvVarName,
125 PCSZ pFilename,
126 PCHAR pFullFilename)
127{
128 APIRET rc;
129 CHAR szFullFilename[CCHMAXPATH];
130
131 if (!pszEnvVarName || !*pszEnvVarName || !pFilename || !*pFilename)
132 return ERROR_INVALID_PARAMETER;
133
134 if (IsAbsolutePath(pFilename)) {
135 rc = DosQueryPathInfo(pFilename, FIL_QUERYFULLNAME,
136 (PVOID)szFullFilename, (ULONG)CCHMAXPATH-1);
137 }
138 else {
139 rc = DosSearchPath(SEARCH_IGNORENETERRS | SEARCH_ENVIRONMENT |
140 SEARCH_CUR_DIRECTORY,
141 (CHAR *) pszEnvVarName,
142 (CHAR *) pFilename,
143 (PBYTE) szFullFilename,
144 CCHMAXPATH - 1);
145 }
146 if (!rc && pFullFilename)
147 strcpy(pFullFilename, szFullFilename);
148
149 return rc;
150}
151
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 */
167APIRET SearchMultiplePathsForFile(PCSZ pFilename,
168 PSZ pFullFilename)
169{
170 APIRET rc;
171
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);
183 }
184 return rc;
185}
186
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)
Note: See TracBrowser for help on using the repository browser.