| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   Path handling utility functions
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   Copyright (c) 1993-98 M. Kimes
 | 
|---|
| 9 |   Copyright (c) 2001, 2008 Steven H. Levine
 | 
|---|
| 10 | 
 | 
|---|
| 11 |   05 Jan 08 SHL Move from arccnrs.c and comp.c to here
 | 
|---|
| 12 |   06 Jan 08 GKY Add NormalizeCmdLine to check program strings on entry
 | 
|---|
| 13 | 
 | 
|---|
| 14 | ***********************************************************************/
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <string.h>
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #define INCL_WIN
 | 
|---|
| 19 | #define INCL_DOS
 | 
|---|
| 20 | #define INCL_LONGLONG
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "pathutil.h"
 | 
|---|
| 23 | #include "fm3dll.h"                     // needs_quoting
 | 
|---|
| 24 | #include "fm3str.h"
 | 
|---|
| 25 | #include "errutil.h"                    // Dos_Error...
 | 
|---|
| 26 | #include "strutil.h"                    // GetPString
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // #pragma data_seg(DATA1)
 | 
|---|
| 29 | 
 | 
|---|
| 30 | /**
 | 
|---|
| 31 |  * Build full path name in callers buffer given directory
 | 
|---|
| 32 |  * name and filename
 | 
|---|
| 33 |  * @param pszPathName points to drive/directory if not NULL
 | 
|---|
| 34 |  * @returns pointer to full path name in caller's buffer
 | 
|---|
| 35 |  * @note OK for pszFullPathName and pszPathName to point to same buffer
 | 
|---|
| 36 |  *
 | 
|---|
| 37 |  */
 | 
|---|
| 38 | 
 | 
|---|
| 39 | PSZ BldFullPathName(PSZ pszFullPathName, PSZ pszPathName, PSZ pszFileName)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   UINT c = pszPathName ? strlen(pszPathName) : 0;
 | 
|---|
| 42 |   if (c > 0) {
 | 
|---|
| 43 |     memcpy(pszFullPathName, pszPathName, c);
 | 
|---|
| 44 |     if (pszFullPathName[c - 1] != '\\')
 | 
|---|
| 45 |       pszFullPathName[c++] = '\\';
 | 
|---|
| 46 |   }
 | 
|---|
| 47 |   strcpy(pszFullPathName + c, pszFileName);
 | 
|---|
| 48 |   return pszFullPathName;
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | /**
 | 
|---|
| 52 |  * Build quoted full path name in callers buffer given
 | 
|---|
| 53 |  * directory name and filename
 | 
|---|
| 54 |  * @param pszPathName points to drive/directory if not NULL
 | 
|---|
| 55 |  * @returns pointer to quoted path name in caller's buffer
 | 
|---|
| 56 |  */
 | 
|---|
| 57 | 
 | 
|---|
| 58 | PSZ BldQuotedFullPathName(PSZ pszFullPathName, PSZ pszPathName, PSZ pszFileName)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   UINT c = pszPathName ? strlen(pszPathName) : 0;
 | 
|---|
| 61 |   BOOL q = needs_quoting(pszPathName) ||
 | 
|---|
| 62 |            needs_quoting(pszFileName);
 | 
|---|
| 63 |   PSZ psz = pszFullPathName;
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   if (q)
 | 
|---|
| 66 |     *psz++ = '"';
 | 
|---|
| 67 |   if (c > 0) {
 | 
|---|
| 68 |     memcpy(psz, pszPathName, c);
 | 
|---|
| 69 |     psz += c;
 | 
|---|
| 70 |     if (*(psz - 1) != '\\')
 | 
|---|
| 71 |       *psz++ = '\\';
 | 
|---|
| 72 |   }
 | 
|---|
| 73 |   strcpy(psz, pszFileName);
 | 
|---|
| 74 |   if (q) {
 | 
|---|
| 75 |     psz += strlen(psz);
 | 
|---|
| 76 |     *psz++ = '"';
 | 
|---|
| 77 |     *psz = 0;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   return pszFullPathName;
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /**
 | 
|---|
| 83 |  * Build quoted full path name in callers buffer given a filename
 | 
|---|
| 84 |  * @returns pointer to quoted file name in caller's buffer
 | 
|---|
| 85 |  */
 | 
|---|
| 86 | 
 | 
|---|
| 87 | PSZ BldQuotedFileName(PSZ pszQuotedFileName, PSZ pszFileName)
 | 
|---|
| 88 | {
 | 
|---|
| 89 |   BOOL q = needs_quoting(pszFileName);
 | 
|---|
| 90 |   PSZ psz = pszQuotedFileName;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   if (q)
 | 
|---|
| 93 |     *psz++ = '"';
 | 
|---|
| 94 |   strcpy(psz, pszFileName);
 | 
|---|
| 95 |   if (q) {
 | 
|---|
| 96 |     psz += strlen(psz);
 | 
|---|
| 97 |     *psz++ = '"';
 | 
|---|
| 98 |     *psz = 0;
 | 
|---|
| 99 |   }
 | 
|---|
| 100 |   return pszQuotedFileName;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** NormalizeCmdLine
 | 
|---|
| 104 |  * Checks a command line for common errors (missing quotes, missing extension,
 | 
|---|
| 105 |  * no space between exe and args etc)
 | 
|---|
| 106 |  * Command line passed as pszCmdLine_
 | 
|---|
| 107 |  * A pointer to a buffer of the size MAXCOMLINESTRG should be supplied in
 | 
|---|
| 108 |  * pszWorkBuf. This is where the quoted etc as necessary command
 | 
|---|
| 109 |  * line string will be returned.
 | 
|---|
| 110 |  */
 | 
|---|
| 111 | 
 | 
|---|
| 112 | PCSZ NormalizeCmdLine(PSZ pszWorkBuf, PSZ pszCmdLine_)
 | 
|---|
| 113 | {
 | 
|---|
| 114 |   char szCmdLine[MAXCOMLINESTRG], szArgs[MAXCOMLINESTRG];
 | 
|---|
| 115 |   char *offset = '\0', *offsetexe, *offsetcom, *offsetcmd, *offsetbtm, *offsetbat;
 | 
|---|
| 116 |   APIRET ret;
 | 
|---|
| 117 |   ULONG ulAppType;
 | 
|---|
| 118 |   char *pszChar;
 | 
|---|
| 119 |   FILEFINDBUF3 FindBuffer;
 | 
|---|
| 120 |   ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
 | 
|---|
| 121 |   HDIR hdirFindHandle = HDIR_CREATE;
 | 
|---|
| 122 |   ULONG ulFindCount = 1;
 | 
|---|
| 123 |   PSZ pszNewCmdLine = pszWorkBuf;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   bstrip(pszCmdLine_);
 | 
|---|
| 126 |   memset(pszWorkBuf, 0, MAXCOMLINESTRG);
 | 
|---|
| 127 |   strcpy(szCmdLine, pszCmdLine_);
 | 
|---|
| 128 |   if (szCmdLine[0] != '\0') {
 | 
|---|
| 129 |     offsetexe = strstr(strlwr(pszCmdLine_), ".exe");
 | 
|---|
| 130 |     offsetcmd = strstr(strlwr(pszCmdLine_), ".cmd");
 | 
|---|
| 131 |     offsetcom = strstr(strlwr(pszCmdLine_), ".com");
 | 
|---|
| 132 |     offsetbtm = strstr(strlwr(pszCmdLine_), ".btm");
 | 
|---|
| 133 |     offsetbat = strstr(strlwr(pszCmdLine_), ".bat");
 | 
|---|
| 134 |     if (offsetexe)
 | 
|---|
| 135 |       offset = offsetexe;
 | 
|---|
| 136 |     else {
 | 
|---|
| 137 |       if (offsetcom)
 | 
|---|
| 138 |         offset = offsetcom;
 | 
|---|
| 139 |       else {
 | 
|---|
| 140 |         if (offsetcmd)
 | 
|---|
| 141 |           offset = offsetcmd;
 | 
|---|
| 142 |         else {
 | 
|---|
| 143 |           if (offsetbtm)
 | 
|---|
| 144 |             offset = offsetbtm;
 | 
|---|
| 145 |           else {
 | 
|---|
| 146 |             if (offsetbat)
 | 
|---|
| 147 |               offset = offsetexe;
 | 
|---|
| 148 |           }
 | 
|---|
| 149 |         }
 | 
|---|
| 150 |       }
 | 
|---|
| 151 |     }
 | 
|---|
| 152 |     if (offset) {
 | 
|---|
| 153 |       szCmdLine[offset + 4 - pszCmdLine_] = '\0';
 | 
|---|
| 154 |       strcpy(szArgs, &pszCmdLine_[offset + 4 - pszCmdLine_]);
 | 
|---|
| 155 |       while (strchr(szCmdLine, '\"'))
 | 
|---|
| 156 |            remove_first_occurence_of_character("\"", szCmdLine);
 | 
|---|
| 157 |       if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
 | 
|---|
| 158 |            !strstr(pszCmdLine_, "\\:")||
 | 
|---|
| 159 |            strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
 | 
|---|
| 160 |         remove_first_occurence_of_character("\"", szArgs);
 | 
|---|
| 161 |       if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
 | 
|---|
| 162 |         saymsg(MB_OK, HWND_DESKTOP,
 | 
|---|
| 163 |                NullStr,
 | 
|---|
| 164 |                GetPString(IDS_QUOTESINARGSTEXT),
 | 
|---|
| 165 |                pszCmdLine_);
 | 
|---|
| 166 |       if (!offsetexe) {
 | 
|---|
| 167 |         ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
 | 
|---|
| 168 |                            ulResultBufLen, &ulFindCount, FIL_STANDARD);
 | 
|---|
| 169 |         if (ret) {
 | 
|---|
| 170 |           pszChar = szCmdLine;
 | 
|---|
| 171 |           while (pszChar) {
 | 
|---|
| 172 |             if (*pszChar == ' ') {
 | 
|---|
| 173 |               *pszChar = '\0';
 | 
|---|
| 174 |               strcat(szCmdLine, ".exe");
 | 
|---|
| 175 |               ret = DosQueryAppType(szCmdLine, &ulAppType);
 | 
|---|
| 176 |               //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
 | 
|---|
| 177 |               if (!ret) {
 | 
|---|
| 178 |                 strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
 | 
|---|
| 179 |                 break;
 | 
|---|
| 180 |               }
 | 
|---|
| 181 |             }
 | 
|---|
| 182 |             strcpy(szCmdLine, pszCmdLine_);
 | 
|---|
| 183 |             pszChar++;
 | 
|---|
| 184 |           }
 | 
|---|
| 185 |         }
 | 
|---|
| 186 |       }
 | 
|---|
| 187 |       else
 | 
|---|
| 188 |         ret = DosQueryAppType(szCmdLine, &ulAppType);
 | 
|---|
| 189 |       BldQuotedFileName(pszNewCmdLine, szCmdLine);
 | 
|---|
| 190 |       //printf("%d A", ret); fflush(stdout);
 | 
|---|
| 191 |       if (ret) {
 | 
|---|
| 192 |         ret = saymsg(MB_YESNO,
 | 
|---|
| 193 |                      HWND_DESKTOP,
 | 
|---|
| 194 |                      NullStr,
 | 
|---|
| 195 |                      GetPString(IDS_PROGRAMNOTFOUNDTEXT),
 | 
|---|
| 196 |                      pszCmdLine_);
 | 
|---|
| 197 |         if (ret == MBID_YES){
 | 
|---|
| 198 |           pszNewCmdLine = pszCmdLine_;
 | 
|---|
| 199 |       }
 | 
|---|
| 200 |         else{
 | 
|---|
| 201 |           fCancelAction = TRUE;
 | 
|---|
| 202 |           pszNewCmdLine = pszCmdLine_;
 | 
|---|
| 203 |         }
 | 
|---|
| 204 |       }
 | 
|---|
| 205 |       else{
 | 
|---|
| 206 |         if (szArgs[0] != ' ')
 | 
|---|
| 207 |           strcat(pszNewCmdLine, " ");
 | 
|---|
| 208 |         strcat(pszNewCmdLine, szArgs);
 | 
|---|
| 209 |       }
 | 
|---|
| 210 | 
 | 
|---|
| 211 |     }
 | 
|---|
| 212 |     else if (szCmdLine && (!strchr(szCmdLine, '.') ||
 | 
|---|
| 213 |                          strrchr(szCmdLine, '.' ) < strrchr(szCmdLine, '\\'))) {
 | 
|---|
| 214 |       if (!strchr(szCmdLine, ' ')) {
 | 
|---|
| 215 |         while (strchr(szCmdLine, '\"'))
 | 
|---|
| 216 |           remove_first_occurence_of_character("\"", szCmdLine);
 | 
|---|
| 217 |         strcat(szCmdLine, ".exe");
 | 
|---|
| 218 |         ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
 | 
|---|
| 219 |                            ulResultBufLen, &ulFindCount, FIL_STANDARD);
 | 
|---|
| 220 |         //printf("%d", ret); fflush(stdout);
 | 
|---|
| 221 |       }
 | 
|---|
| 222 |       else {
 | 
|---|
| 223 |         pszChar = szCmdLine;
 | 
|---|
| 224 |         while (pszChar) {
 | 
|---|
| 225 |           while (strchr(szCmdLine, '\"'))
 | 
|---|
| 226 |             remove_first_occurence_of_character("\"", szCmdLine);
 | 
|---|
| 227 |           if (*pszChar == ' ') {
 | 
|---|
| 228 |             *pszChar = '\0';
 | 
|---|
| 229 |             strcat(szCmdLine, ".exe");
 | 
|---|
| 230 |             ret = DosQueryAppType(szCmdLine, &ulAppType);
 | 
|---|
| 231 |             //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
 | 
|---|
| 232 |             if (!ret) {
 | 
|---|
| 233 |               break;
 | 
|---|
| 234 |             }
 | 
|---|
| 235 |           }
 | 
|---|
| 236 |           strcpy(szCmdLine, pszCmdLine_);
 | 
|---|
| 237 |           pszChar++;
 | 
|---|
| 238 |         }
 | 
|---|
| 239 |       }
 | 
|---|
| 240 |       if (!ret){
 | 
|---|
| 241 |         BldQuotedFileName(pszNewCmdLine, szCmdLine);
 | 
|---|
| 242 |         strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
 | 
|---|
| 243 |         if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
 | 
|---|
| 244 |              !strstr(pszCmdLine_, "\\:" ) ||
 | 
|---|
| 245 |              strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
 | 
|---|
| 246 |           remove_first_occurence_of_character("\"", szArgs);
 | 
|---|
| 247 |         if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
 | 
|---|
| 248 |           saymsg(MB_OK, HWND_DESKTOP,
 | 
|---|
| 249 |                  NullStr,
 | 
|---|
| 250 |                  GetPString(IDS_QUOTESINARGSTEXT),
 | 
|---|
| 251 |                  pszCmdLine_);
 | 
|---|
| 252 |         if (szArgs[0] != ' ')
 | 
|---|
| 253 |           strcat(pszNewCmdLine, " ");
 | 
|---|
| 254 |         strcat(pszNewCmdLine, szArgs);
 | 
|---|
| 255 |       }
 | 
|---|
| 256 |       else {
 | 
|---|
| 257 |         ret = saymsg(MB_OK,
 | 
|---|
| 258 |                      HWND_DESKTOP,
 | 
|---|
| 259 |                      NullStr,
 | 
|---|
| 260 |                      GetPString(IDS_PROGRAMNOTEXE2TEXT),
 | 
|---|
| 261 |                      pszCmdLine_);
 | 
|---|
| 262 |           fCancelAction = TRUE;
 | 
|---|
| 263 |           pszNewCmdLine = pszCmdLine_;
 | 
|---|
| 264 |       }
 | 
|---|
| 265 |     }
 | 
|---|
| 266 |     else {
 | 
|---|
| 267 |       pszChar = strrchr(szCmdLine, '.');
 | 
|---|
| 268 |       while (pszChar && *pszChar !=' ') {
 | 
|---|
| 269 |         pszChar++;
 | 
|---|
| 270 |       }
 | 
|---|
| 271 |       *pszChar = '\0';
 | 
|---|
| 272 |       strcpy (szArgs, pszCmdLine_ + strlen(szCmdLine));
 | 
|---|
| 273 |       while (strchr(szCmdLine, '\"'))
 | 
|---|
| 274 |         remove_first_occurence_of_character("\"", szCmdLine);
 | 
|---|
| 275 |     if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
 | 
|---|
| 276 |          !strstr(pszCmdLine_, "\\:")||
 | 
|---|
| 277 |          strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
 | 
|---|
| 278 |       remove_first_occurence_of_character("\"", szArgs);
 | 
|---|
| 279 |     if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
 | 
|---|
| 280 |       saymsg(MB_OK, HWND_DESKTOP,
 | 
|---|
| 281 |              NullStr,
 | 
|---|
| 282 |              GetPString(IDS_QUOTESINARGSTEXT),
 | 
|---|
| 283 |              pszCmdLine_);
 | 
|---|
| 284 |     ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
 | 
|---|
| 285 |                          ulResultBufLen, &ulFindCount, FIL_STANDARD);
 | 
|---|
| 286 | 
 | 
|---|
| 287 |     BldQuotedFileName(pszNewCmdLine, szCmdLine);
 | 
|---|
| 288 |     //printf("%d %s ", ret, szCmdLine); fflush(stdout);
 | 
|---|
| 289 |     if (ret) {
 | 
|---|
| 290 |       ret = saymsg(MB_YESNO,
 | 
|---|
| 291 |                    HWND_DESKTOP,
 | 
|---|
| 292 |                    NullStr,
 | 
|---|
| 293 |                    GetPString(IDS_PROGRAMNOTFOUNDTEXT),
 | 
|---|
| 294 |                    pszCmdLine_);
 | 
|---|
| 295 |       if (ret == MBID_YES) {
 | 
|---|
| 296 |         pszWorkBuf = pszCmdLine_;
 | 
|---|
| 297 |       }
 | 
|---|
| 298 |       else {
 | 
|---|
| 299 |         fCancelAction = TRUE;
 | 
|---|
| 300 |         pszWorkBuf = pszCmdLine_;
 | 
|---|
| 301 |       }
 | 
|---|
| 302 |     }
 | 
|---|
| 303 |     ret = saymsg(MB_YESNOCANCEL,
 | 
|---|
| 304 |                    HWND_DESKTOP,
 | 
|---|
| 305 |                    NullStr,
 | 
|---|
| 306 |                    GetPString(IDS_PROGRAMNOTEXE3TEXT),
 | 
|---|
| 307 |                    pszCmdLine_, pszNewCmdLine);
 | 
|---|
| 308 |       if (ret == MBID_YES){
 | 
|---|
| 309 |         if (szArgs[0] != ' ')
 | 
|---|
| 310 |           strcat(pszNewCmdLine, " ");
 | 
|---|
| 311 |         strcat(pszNewCmdLine, szArgs);
 | 
|---|
| 312 |       }
 | 
|---|
| 313 |       if (ret == MBID_CANCEL){
 | 
|---|
| 314 |         fCancelAction = TRUE;
 | 
|---|
| 315 |         pszNewCmdLine = pszCmdLine_;
 | 
|---|
| 316 |       }
 | 
|---|
| 317 |     }
 | 
|---|
| 318 |   }
 | 
|---|
| 319 |   return pszWorkBuf;
 | 
|---|
| 320 | }
 | 
|---|
| 321 | 
 | 
|---|
| 322 | #pragma alloc_text(PATHUTIL,BldFullPathName)
 | 
|---|
| 323 | #pragma alloc_text(PATHUTIL,BldQuotedFileName)
 | 
|---|
| 324 | #pragma alloc_text(PATHUTIL,BldQuotedFullPathName)
 | 
|---|
| 325 | #pragma alloc_text(PATHUTIL,NormalizeCmdLine)
 | 
|---|