home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / which / which.cxx < prev   
C/C++ Source or Header  |  1994-10-14  |  1KB  |  62 lines

  1.  
  2.  
  3. // YACL demo: a simple "which" program: displays which file will be
  4. // executed in response to a command, using the PATH environment variable.
  5.  
  6. // M. A. Sridhar, 6/28/94
  7.  
  8.  
  9. #include "base/base.h"
  10. #include "io/io.h"
  11.  
  12. #include <iostream.h>
  13. #include <stdlib.h>
  14.  
  15. char* Extensions[] = {
  16. #ifdef __UNIX__
  17.     "",
  18. #elif defined (__DOS__)
  19.     ".exe",
  20.     ".com",
  21.     ".bat",
  22. #elif defined (__OS2__)
  23.     ".exe",
  24.     ".cmd",
  25. #endif
  26.     0
  27. };
  28.  
  29.  
  30. main (int argc, char* argv[])
  31. {
  32.     char* path = getenv("PATH");
  33.     if (!path)
  34.         return -1;
  35.     CL_String path_string (path);
  36. #if defined ( __DOS__ ) || defined(__OS2__)
  37.     char field_seps[] = ";";
  38.     char dir_sep = '\\';
  39. #else
  40.     char field_seps[] = ":";
  41.     char dir_sep = '/';
  42. #endif
  43.     CL_StringSequence dirs = path_string.Split (field_seps);
  44. #if defined (__DOS__) || defined (__OS2__)
  45.     dirs.Insert (".");
  46. #endif
  47.     register short n = dirs.Size();
  48.     for (short j = 1; j < argc; j++) {
  49.         for (short i = 0; i < n; i++) {
  50.             CL_String base_file = dirs[i] + CL_String (dir_sep, 1) + argv[j];
  51.             for (short k = 0; Extensions[k] != 0; k++) {
  52.                 CL_String file = base_file + Extensions[k];
  53.                 if (CL_BinaryFile::Exists (file)) {
  54.                     cout << file << endl;
  55.                     break;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     return 0;
  61. }
  62.