[907] | 1 |
|
---|
| 2 | /***********************************************************************
|
---|
| 3 |
|
---|
[1323] | 4 | $Id: pathutil.c 1398 2009-02-21 17:43:00Z gyoung $
|
---|
[907] | 5 |
|
---|
| 6 | Path handling utility functions
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 1993-98 M. Kimes
|
---|
[1394] | 9 | Copyright (c) 2001, 2009 Steven H. Levine
|
---|
[907] | 10 |
|
---|
| 11 | 05 Jan 08 SHL Move from arccnrs.c and comp.c to here
|
---|
[920] | 12 | 06 Jan 08 GKY Add NormalizeCmdLine to check program strings on entry
|
---|
[985] | 13 | 29 Feb 08 GKY Changes to enable user settable command line length
|
---|
[1247] | 14 | 15 Oct 08 GKY Fix NormalizeCmdLine to check all 5 executible extensions when no extension provided;
|
---|
[1394] | 15 | use searchapath to check for existance of file types not checked by DosQAppType;
|
---|
| 16 | close DosFind.
|
---|
[907] | 17 |
|
---|
| 18 | ***********************************************************************/
|
---|
| 19 |
|
---|
[1039] | 20 | #include <stdlib.h>
|
---|
[907] | 21 | #include <string.h>
|
---|
| 22 |
|
---|
| 23 | #define INCL_WIN
|
---|
[920] | 24 | #define INCL_DOS
|
---|
[907] | 25 | #define INCL_LONGLONG
|
---|
| 26 |
|
---|
[1185] | 27 | #include "fm3dll.h" // needs_quoting
|
---|
[1211] | 28 | #include "notebook.h" // Data declaration(s)
|
---|
| 29 | #include "init.h" // Data declaration(s)
|
---|
[1185] | 30 | #include "fm3str.h"
|
---|
[1247] | 31 | #include "srchpath.h" // searchapath
|
---|
[907] | 32 | #include "pathutil.h"
|
---|
[1161] | 33 | #include "strips.h" // remove_first_occurence_of_character
|
---|
[1185] | 34 | #include "valid.h" // needs_quoting
|
---|
[920] | 35 | #include "errutil.h" // Dos_Error...
|
---|
| 36 | #include "strutil.h" // GetPString
|
---|
[1185] | 37 | #include "wrappers.h" // xmalloc
|
---|
[1039] | 38 | #include "fortify.h"
|
---|
[1398] | 39 | #include "stristr.h" //stristr
|
---|
[907] | 40 |
|
---|
[985] | 41 | static PSZ pszSrcFile = __FILE__;
|
---|
| 42 |
|
---|
[907] | 43 | // #pragma data_seg(DATA1)
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Build full path name in callers buffer given directory
|
---|
| 47 | * name and filename
|
---|
| 48 | * @param pszPathName points to drive/directory if not NULL
|
---|
| 49 | * @returns pointer to full path name in caller's buffer
|
---|
| 50 | * @note OK for pszFullPathName and pszPathName to point to same buffer
|
---|
| 51 | *
|
---|
| 52 | */
|
---|
| 53 |
|
---|
[1398] | 54 | PSZ BldFullPathName(PSZ pszFullPathName, PCSZ pszPathName, PCSZ pszFileName)
|
---|
[907] | 55 | {
|
---|
| 56 | UINT c = pszPathName ? strlen(pszPathName) : 0;
|
---|
| 57 | if (c > 0) {
|
---|
| 58 | memcpy(pszFullPathName, pszPathName, c);
|
---|
| 59 | if (pszFullPathName[c - 1] != '\\')
|
---|
| 60 | pszFullPathName[c++] = '\\';
|
---|
| 61 | }
|
---|
| 62 | strcpy(pszFullPathName + c, pszFileName);
|
---|
| 63 | return pszFullPathName;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Build quoted full path name in callers buffer given
|
---|
| 68 | * directory name and filename
|
---|
| 69 | * @param pszPathName points to drive/directory if not NULL
|
---|
| 70 | * @returns pointer to quoted path name in caller's buffer
|
---|
| 71 | */
|
---|
| 72 |
|
---|
[1398] | 73 | PSZ BldQuotedFullPathName(PSZ pszFullPathName, PCSZ pszPathName, PCSZ pszFileName)
|
---|
[907] | 74 | {
|
---|
| 75 | UINT c = pszPathName ? strlen(pszPathName) : 0;
|
---|
[1161] | 76 | BOOL q = needs_quoting(pszPathName) || needs_quoting(pszFileName);
|
---|
[907] | 77 | PSZ psz = pszFullPathName;
|
---|
| 78 |
|
---|
| 79 | if (q)
|
---|
| 80 | *psz++ = '"';
|
---|
| 81 | if (c > 0) {
|
---|
| 82 | memcpy(psz, pszPathName, c);
|
---|
| 83 | psz += c;
|
---|
| 84 | if (*(psz - 1) != '\\')
|
---|
| 85 | *psz++ = '\\';
|
---|
| 86 | }
|
---|
| 87 | strcpy(psz, pszFileName);
|
---|
| 88 | if (q) {
|
---|
| 89 | psz += strlen(psz);
|
---|
| 90 | *psz++ = '"';
|
---|
| 91 | *psz = 0;
|
---|
| 92 | }
|
---|
| 93 | return pszFullPathName;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Build quoted full path name in callers buffer given a filename
|
---|
| 98 | * @returns pointer to quoted file name in caller's buffer
|
---|
| 99 | */
|
---|
| 100 |
|
---|
[1394] | 101 | PSZ BldQuotedFileName(PSZ pszQuotedFileName, PCSZ pszFileName)
|
---|
[907] | 102 | {
|
---|
| 103 | BOOL q = needs_quoting(pszFileName);
|
---|
| 104 | PSZ psz = pszQuotedFileName;
|
---|
| 105 |
|
---|
| 106 | if (q)
|
---|
| 107 | *psz++ = '"';
|
---|
| 108 | strcpy(psz, pszFileName);
|
---|
| 109 | if (q) {
|
---|
| 110 | psz += strlen(psz);
|
---|
| 111 | *psz++ = '"';
|
---|
| 112 | *psz = 0;
|
---|
| 113 | }
|
---|
| 114 | return pszQuotedFileName;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[920] | 117 | /** NormalizeCmdLine
|
---|
| 118 | * Checks a command line for common errors (missing quotes, missing extension,
|
---|
[1248] | 119 | * no space between exe and args etc) Also check for the existance of the file
|
---|
| 120 | * and checks .com and .exe file headers.
|
---|
[920] | 121 | * Command line passed as pszCmdLine_
|
---|
[985] | 122 | * A pointer to a buffer of the size MaxComLineStrg should be supplied in
|
---|
[920] | 123 | * pszWorkBuf. This is where the quoted etc as necessary command
|
---|
| 124 | * line string will be returned.
|
---|
| 125 | */
|
---|
| 126 |
|
---|
| 127 | PCSZ NormalizeCmdLine(PSZ pszWorkBuf, PSZ pszCmdLine_)
|
---|
| 128 | {
|
---|
[985] | 129 | char *szCmdLine, *szArgs;
|
---|
[920] | 130 | char *offset = '\0', *offsetexe, *offsetcom, *offsetcmd, *offsetbtm, *offsetbat;
|
---|
| 131 | APIRET ret;
|
---|
| 132 | ULONG ulAppType;
|
---|
| 133 | char *pszChar;
|
---|
[1247] | 134 | char *FullPath;
|
---|
[920] | 135 | PSZ pszNewCmdLine = pszWorkBuf;
|
---|
| 136 |
|
---|
[985] | 137 | szCmdLine = xmalloc(MaxComLineStrg, pszSrcFile, __LINE__);
|
---|
| 138 | if (!szCmdLine)
|
---|
| 139 | return pszCmdLine_; //already complained
|
---|
| 140 | szArgs = xmalloc(MaxComLineStrg, pszSrcFile, __LINE__);
|
---|
| 141 | if (!szArgs) {
|
---|
[1039] | 142 | free(szCmdLine);
|
---|
[985] | 143 | return pszCmdLine_; //already complained
|
---|
| 144 | }
|
---|
[920] | 145 | bstrip(pszCmdLine_);
|
---|
[985] | 146 | memset(pszWorkBuf, 0, MaxComLineStrg);
|
---|
[920] | 147 | strcpy(szCmdLine, pszCmdLine_);
|
---|
| 148 | if (szCmdLine[0] != '\0') {
|
---|
[1398] | 149 | offsetexe = stristr(pszCmdLine_, PCSZ_DOTEXE);
|
---|
| 150 | offsetcmd = stristr(pszCmdLine_, PCSZ_DOTCMD);
|
---|
| 151 | offsetcom = stristr(pszCmdLine_, PCSZ_DOTCOM);
|
---|
| 152 | offsetbtm = stristr(pszCmdLine_, PCSZ_DOTBTM);
|
---|
| 153 | offsetbat = stristr(pszCmdLine_, PCSZ_DOTBAT);
|
---|
[920] | 154 | if (offsetexe)
|
---|
| 155 | offset = offsetexe;
|
---|
| 156 | else {
|
---|
| 157 | if (offsetcom)
|
---|
| 158 | offset = offsetcom;
|
---|
| 159 | else {
|
---|
| 160 | if (offsetcmd)
|
---|
| 161 | offset = offsetcmd;
|
---|
| 162 | else {
|
---|
| 163 | if (offsetbtm)
|
---|
| 164 | offset = offsetbtm;
|
---|
| 165 | else {
|
---|
| 166 | if (offsetbat)
|
---|
| 167 | offset = offsetexe;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | if (offset) {
|
---|
| 173 | szCmdLine[offset + 4 - pszCmdLine_] = '\0';
|
---|
| 174 | strcpy(szArgs, &pszCmdLine_[offset + 4 - pszCmdLine_]);
|
---|
| 175 | while (strchr(szCmdLine, '\"'))
|
---|
| 176 | remove_first_occurence_of_character("\"", szCmdLine);
|
---|
| 177 | if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
|
---|
| 178 | !strstr(pszCmdLine_, "\\:")||
|
---|
| 179 | strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
|
---|
| 180 | remove_first_occurence_of_character("\"", szArgs);
|
---|
| 181 | if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
|
---|
| 182 | saymsg(MB_OK, HWND_DESKTOP,
|
---|
| 183 | NullStr,
|
---|
| 184 | GetPString(IDS_QUOTESINARGSTEXT),
|
---|
| 185 | pszCmdLine_);
|
---|
[1247] | 186 | if (!offsetexe && !offsetcom) {
|
---|
[1398] | 187 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 188 | if (*FullPath)
|
---|
| 189 | ret = 0;
|
---|
[920] | 190 | }
|
---|
| 191 | else
|
---|
| 192 | ret = DosQueryAppType(szCmdLine, &ulAppType);
|
---|
| 193 | BldQuotedFileName(pszNewCmdLine, szCmdLine);
|
---|
| 194 | //printf("%d A", ret); fflush(stdout);
|
---|
| 195 | if (ret) {
|
---|
[1394] | 196 | ret = saymsg(MB_YESNO,
|
---|
| 197 | HWND_DESKTOP,
|
---|
| 198 | NullStr,
|
---|
| 199 | GetPString(IDS_PROGRAMNOTFOUNDTEXT),
|
---|
| 200 | pszCmdLine_);
|
---|
| 201 | if (ret == MBID_YES){
|
---|
| 202 | if (szArgs[0] != ' ')
|
---|
| 203 | strcat(pszNewCmdLine, " ");
|
---|
| 204 | strcat(pszNewCmdLine, szArgs);
|
---|
[920] | 205 | }
|
---|
[1394] | 206 | else{
|
---|
| 207 | fCancelAction = TRUE;
|
---|
| 208 | pszNewCmdLine = pszCmdLine_;
|
---|
| 209 | }
|
---|
[920] | 210 | }
|
---|
| 211 | else{
|
---|
[1394] | 212 | if (szArgs[0] != ' ')
|
---|
| 213 | strcat(pszNewCmdLine, " ");
|
---|
| 214 | strcat(pszNewCmdLine, szArgs);
|
---|
[920] | 215 | }
|
---|
| 216 |
|
---|
| 217 | }
|
---|
[1247] | 218 | // if it doesn't have an extension try it with all the standard ones and add if found
|
---|
[920] | 219 | else if (szCmdLine && (!strchr(szCmdLine, '.') ||
|
---|
[1247] | 220 | strrchr(szCmdLine, '.' ) < strrchr(szCmdLine, '\\'))) {
|
---|
[920] | 221 | if (!strchr(szCmdLine, ' ')) {
|
---|
[1394] | 222 | // strip quotes readded by BuildQuotedFileName
|
---|
[920] | 223 | while (strchr(szCmdLine, '\"'))
|
---|
| 224 | remove_first_occurence_of_character("\"", szCmdLine);
|
---|
[1394] | 225 | ret = DosQueryAppType(szCmdLine, &ulAppType); // exe automatically appended
|
---|
| 226 | if (!ret)
|
---|
[1398] | 227 | strcat(szCmdLine, PCSZ_DOTEXE);
|
---|
[1394] | 228 | else {
|
---|
[1398] | 229 | strcat(szCmdLine, PCSZ_DOTCOM);
|
---|
[1394] | 230 | ret = DosQueryAppType(szCmdLine, &ulAppType);
|
---|
| 231 | if (ret) {
|
---|
| 232 | offset = strrchr(szCmdLine, '.' );
|
---|
| 233 | *offset = 0;
|
---|
[1398] | 234 | strcat(szCmdLine, PCSZ_DOTCMD);
|
---|
| 235 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 236 | if (*FullPath)
|
---|
| 237 | ret = 0;
|
---|
| 238 | else {
|
---|
| 239 | *offset = 0;
|
---|
[1398] | 240 | strcat(szCmdLine, PCSZ_DOTBAT);
|
---|
| 241 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 242 | if (*FullPath)
|
---|
| 243 | ret = 0;
|
---|
| 244 | else {
|
---|
| 245 | *offset = 0;
|
---|
[1398] | 246 | strcat(szCmdLine, PCSZ_DOTBTM);
|
---|
| 247 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 248 | if (*FullPath)
|
---|
| 249 | ret = 0;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
[920] | 254 | //printf("%d", ret); fflush(stdout);
|
---|
| 255 | }
|
---|
| 256 | else {
|
---|
| 257 | pszChar = szCmdLine;
|
---|
| 258 | while (pszChar) {
|
---|
| 259 | while (strchr(szCmdLine, '\"'))
|
---|
| 260 | remove_first_occurence_of_character("\"", szCmdLine);
|
---|
[1247] | 261 | if (*pszChar == ' ') { //test at every space for the end of the filename
|
---|
[920] | 262 | *pszChar = '\0';
|
---|
[1394] | 263 | ret = DosQueryAppType(szCmdLine, &ulAppType);
|
---|
| 264 | if (!ret) {
|
---|
[1398] | 265 | strcat(szCmdLine, PCSZ_DOTEXE);
|
---|
[1394] | 266 | break;
|
---|
| 267 | }
|
---|
| 268 | else {
|
---|
[1398] | 269 | strcat(szCmdLine, PCSZ_DOTCOM);
|
---|
[1394] | 270 | ret = DosQueryAppType(szCmdLine, &ulAppType);
|
---|
| 271 | if (ret) {
|
---|
| 272 | offset = strrchr(szCmdLine, '.' );
|
---|
| 273 | *offset = 0;
|
---|
[1398] | 274 | strcat(szCmdLine, PCSZ_DOTCMD);
|
---|
| 275 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 276 | if (*FullPath) {
|
---|
| 277 | ret = 0;
|
---|
| 278 | break;
|
---|
| 279 | }
|
---|
| 280 | else {
|
---|
| 281 | *offset = 0;
|
---|
[1398] | 282 | strcat(szCmdLine, PCSZ_DOTBAT);
|
---|
| 283 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 284 | if (*FullPath) {
|
---|
| 285 | ret = 0;
|
---|
| 286 | break;
|
---|
| 287 | }
|
---|
| 288 | else {
|
---|
| 289 | *offset = 0;
|
---|
[1398] | 290 | strcat(szCmdLine, PCSZ_DOTBTM);
|
---|
| 291 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[1394] | 292 | if (*FullPath) {
|
---|
| 293 | ret = 0;
|
---|
| 294 | break;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | else
|
---|
| 300 | break;
|
---|
| 301 | }
|
---|
[920] | 302 | //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
|
---|
| 303 | }
|
---|
| 304 | strcpy(szCmdLine, pszCmdLine_);
|
---|
| 305 | pszChar++;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | if (!ret){
|
---|
[1394] | 309 | BldQuotedFileName(pszNewCmdLine, szCmdLine);
|
---|
| 310 | strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
|
---|
| 311 | if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
|
---|
| 312 | !strstr(pszCmdLine_, "\\:" ) ||
|
---|
| 313 | strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
|
---|
| 314 | remove_first_occurence_of_character("\"", szArgs);
|
---|
| 315 | if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
|
---|
| 316 | saymsg(MB_OK, HWND_DESKTOP,
|
---|
| 317 | NullStr,
|
---|
| 318 | GetPString(IDS_QUOTESINARGSTEXT),
|
---|
| 319 | pszCmdLine_);
|
---|
| 320 | if (szArgs[0] != ' ')
|
---|
| 321 | strcat(pszNewCmdLine, " ");
|
---|
| 322 | strcat(pszNewCmdLine, szArgs);
|
---|
[920] | 323 | }
|
---|
[1248] | 324 | else { // fail if no extension can be found runemf2 requires one
|
---|
[1394] | 325 | ret = saymsg(MB_OK,
|
---|
| 326 | HWND_DESKTOP,
|
---|
| 327 | NullStr,
|
---|
| 328 | GetPString(IDS_PROGRAMNOTEXE2TEXT),
|
---|
| 329 | pszCmdLine_);
|
---|
| 330 | fCancelAction = TRUE;
|
---|
| 331 | pszNewCmdLine = pszCmdLine_;
|
---|
[920] | 332 | }
|
---|
| 333 | }
|
---|
[1247] | 334 | else { // file has a nonstandard extension for executible
|
---|
[920] | 335 | pszChar = strrchr(szCmdLine, '.');
|
---|
| 336 | while (pszChar && *pszChar !=' ') {
|
---|
| 337 | pszChar++;
|
---|
| 338 | }
|
---|
| 339 | *pszChar = '\0';
|
---|
| 340 | strcpy (szArgs, pszCmdLine_ + strlen(szCmdLine));
|
---|
| 341 | while (strchr(szCmdLine, '\"'))
|
---|
| 342 | remove_first_occurence_of_character("\"", szCmdLine);
|
---|
| 343 | if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
|
---|
| 344 | !strstr(pszCmdLine_, "\\:")||
|
---|
| 345 | strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
|
---|
| 346 | remove_first_occurence_of_character("\"", szArgs);
|
---|
| 347 | if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
|
---|
| 348 | saymsg(MB_OK, HWND_DESKTOP,
|
---|
| 349 | NullStr,
|
---|
| 350 | GetPString(IDS_QUOTESINARGSTEXT),
|
---|
| 351 | pszCmdLine_);
|
---|
[1398] | 352 | FullPath = searchapath(PCSZ_PATH, szCmdLine);
|
---|
[920] | 353 | BldQuotedFileName(pszNewCmdLine, szCmdLine);
|
---|
| 354 | //printf("%d %s ", ret, szCmdLine); fflush(stdout);
|
---|
[1248] | 355 | if (!*FullPath) {
|
---|
[920] | 356 | ret = saymsg(MB_YESNO,
|
---|
| 357 | HWND_DESKTOP,
|
---|
| 358 | NullStr,
|
---|
| 359 | GetPString(IDS_PROGRAMNOTFOUNDTEXT),
|
---|
| 360 | pszCmdLine_);
|
---|
| 361 | if (ret == MBID_YES) {
|
---|
[1394] | 362 | if (szArgs[0] != ' ')
|
---|
| 363 | strcat(pszNewCmdLine, " ");
|
---|
| 364 | strcat(pszNewCmdLine, szArgs);
|
---|
[920] | 365 | }
|
---|
| 366 | else {
|
---|
| 367 | fCancelAction = TRUE;
|
---|
| 368 | pszWorkBuf = pszCmdLine_;
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
[1248] | 371 | else {
|
---|
| 372 | ret = saymsg(MB_YESNOCANCEL,
|
---|
[1394] | 373 | HWND_DESKTOP,
|
---|
| 374 | NullStr,
|
---|
| 375 | GetPString(IDS_PROGRAMNOTEXE3TEXT),
|
---|
| 376 | pszCmdLine_, pszNewCmdLine);
|
---|
| 377 | if (ret == MBID_YES){
|
---|
| 378 | if (szArgs[0] != ' ')
|
---|
| 379 | strcat(pszNewCmdLine, " ");
|
---|
| 380 | strcat(pszNewCmdLine, szArgs);
|
---|
| 381 | }
|
---|
| 382 | if (ret == MBID_CANCEL){
|
---|
| 383 | fCancelAction = TRUE;
|
---|
| 384 | pszNewCmdLine = pszCmdLine_;
|
---|
| 385 | }
|
---|
[920] | 386 | }
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
[1039] | 389 | free(szArgs);
|
---|
| 390 | free(szCmdLine);
|
---|
[920] | 391 | return pszWorkBuf;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
[907] | 394 | #pragma alloc_text(PATHUTIL,BldFullPathName)
|
---|
| 395 | #pragma alloc_text(PATHUTIL,BldQuotedFileName)
|
---|
| 396 | #pragma alloc_text(PATHUTIL,BldQuotedFullPathName)
|
---|
[920] | 397 | #pragma alloc_text(PATHUTIL,NormalizeCmdLine)
|
---|