| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: srchpath.c 809 2007-08-26 17:44:52Z stevenhl $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   Path search functions
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   Copyright (c) 1993-98 M. Kimes
 | 
|---|
| 9 |   Copyright (c) 2003, 2007 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 | 
 | 
|---|
| 15 | ***********************************************************************/
 | 
|---|
| 16 | #define INCL_WIN
 | 
|---|
| 17 | #define INCL_WINERRORS
 | 
|---|
| 18 | #define INCL_DOS
 | 
|---|
| 19 | #define INCL_DOSERRORS
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include <os2.h>
 | 
|---|
| 22 | #include <stdlib.h>
 | 
|---|
| 23 | #include <stdio.h>
 | 
|---|
| 24 | #include <string.h>
 | 
|---|
| 25 | #include <stdarg.h>
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "fm3dll.h"
 | 
|---|
| 28 | #include "fm3dlg.h"
 | 
|---|
| 29 | #include "fm3str.h"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | static PSZ pszSrcFile = __FILE__;
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #pragma data_seg(DATA1)
 | 
|---|
| 34 | 
 | 
|---|
| 35 | //== RunFM2Util() Find and run an app from the FM2utilities ==
 | 
|---|
| 36 | //== Search PATH plus 2 default install dirs ==
 | 
|---|
| 37 | 
 | 
|---|
| 38 | INT RunFM2Util(CHAR *appname, CHAR *filename)
 | 
|---|
| 39 | {
 | 
|---|
| 40 |     CHAR fbuf[CCHMAXPATH];
 | 
|---|
| 41 |     APIRET rc, ret = -1;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |     rc = DosSearchPath(SEARCH_IGNORENETERRS |SEARCH_ENVIRONMENT |
 | 
|---|
| 44 |                        SEARCH_CUR_DIRECTORY,"PATH",
 | 
|---|
| 45 |                        appname, fbuf, CCHMAXPATH - 1);
 | 
|---|
| 46 |       if (rc != 0) {
 | 
|---|
| 47 |         if (rc != 2){
 | 
|---|
| 48 |         Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
 | 
|---|
| 49 |                   "DosSearchPath", appname);
 | 
|---|
| 50 |         return ret;
 | 
|---|
| 51 |         }
 | 
|---|
| 52 |         else {
 | 
|---|
| 53 |         rc = DosSearchPath(0, "UTILS;..\\FM2Utils",
 | 
|---|
| 54 |                            appname, fbuf, CCHMAXPATH - 1);
 | 
|---|
| 55 |             if (rc != 0 && rc != 2){
 | 
|---|
| 56 |               Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
 | 
|---|
| 57 |                         "DosSearchPath", appname);
 | 
|---|
| 58 |               return ret;
 | 
|---|
| 59 |             }
 | 
|---|
| 60 |       }
 | 
|---|
| 61 |     }
 | 
|---|
| 62 |     ret = runemf2(SEPARATE | WINDOWED,
 | 
|---|
| 63 |                   HWND_DESKTOP,
 | 
|---|
| 64 |                   NULL,
 | 
|---|
| 65 |                   NULL,
 | 
|---|
| 66 |                   "%s \"%s\"",
 | 
|---|
| 67 |                   fbuf, filename);
 | 
|---|
| 68 |     return ret;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | CHAR *first_path(CHAR * path, CHAR * ret)
 | 
|---|
| 72 | {
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   CHAR *p, *pp;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   if (!path || !ret)
 | 
|---|
| 77 |     return ret;
 | 
|---|
| 78 |   strcpy(ret, path);
 | 
|---|
| 79 |   p = strchr(ret, ';');
 | 
|---|
| 80 |   if (p) {
 | 
|---|
| 81 |     *p = 0;
 | 
|---|
| 82 |     p++;
 | 
|---|
| 83 |     if (*ret == '.') {                  /* skip initial "cur dir" */
 | 
|---|
| 84 |       pp = strchr(p, ';');
 | 
|---|
| 85 |       if (pp)
 | 
|---|
| 86 |         *pp = 0;
 | 
|---|
| 87 |       if (*p)
 | 
|---|
| 88 |         memmove(ret, p, strlen(p) + 1);
 | 
|---|
| 89 |     }
 | 
|---|
| 90 |   }
 | 
|---|
| 91 |   return ret;
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /**
 | 
|---|
| 95 |  * Search for file in name PATH env variable
 | 
|---|
| 96 |  * 23 Aug 07 SHL fixme to be MT safe
 | 
|---|
| 97 |  */
 | 
|---|
| 98 | 
 | 
|---|
| 99 | CHAR *searchapath(CHAR *pathvar, CHAR *filename)
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   static CHAR fbuf[CCHMAXPATH];
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   if (strchr(filename, '\\') || strchr(filename, '/')
 | 
|---|
| 104 |       || strchr(filename, ':')) {
 | 
|---|
| 105 | 
 | 
|---|
| 106 |     FILESTATUS3 fsa;
 | 
|---|
| 107 | 
 | 
|---|
| 108 |     if (!DosQueryPathInfo(filename, FIL_STANDARD, &fsa, (ULONG) sizeof(fsa)))
 | 
|---|
| 109 |       return filename;
 | 
|---|
| 110 |     *fbuf = 0;
 | 
|---|
| 111 |     return fbuf;
 | 
|---|
| 112 |   }
 | 
|---|
| 113 |   *fbuf = 0;
 | 
|---|
| 114 |   if (DosSearchPath(SEARCH_IGNORENETERRS | SEARCH_ENVIRONMENT |
 | 
|---|
| 115 |                     SEARCH_CUR_DIRECTORY,
 | 
|---|
| 116 |                     pathvar, filename, fbuf, CCHMAXPATH - 1))
 | 
|---|
| 117 |     *fbuf = 0;
 | 
|---|
| 118 |   return fbuf;
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | CHAR *searchpath(CHAR * filename)
 | 
|---|
| 122 | {
 | 
|---|
| 123 |   CHAR *found;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   if (!filename)
 | 
|---|
| 126 |     return "";
 | 
|---|
| 127 |   found = searchapath("PATH", filename);
 | 
|---|
| 128 |   if (!*found) {
 | 
|---|
| 129 |     found = searchapath("DPATH", filename);
 | 
|---|
| 130 |     if (!*found)
 | 
|---|
| 131 |       found = searchapath("XPATH", filename);
 | 
|---|
| 132 |   }
 | 
|---|
| 133 |   return found;
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | #pragma alloc_text(MISC9,first_path,searchapath,searchpath,RunFM2Util)
 | 
|---|