home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / cenvi29.zip / WHICH.CMD < prev    next >
OS/2 REXX Batch file  |  1994-11-09  |  3KB  |  98 lines

  1. EXTPROC CEnvi
  2. //********************************************************************
  3. //*** Which.bat - Show when command would be executed by searching ***
  4. //*** ver.1       for command along path.                          ***
  5. //********************************************************************
  6.  
  7. ExecuteOrder = { ".COM", ".EXE", ".CMD", ".BAT" };
  8.  
  9. Instructions()
  10. {
  11.    printf("\a\n")
  12.    puts(`Which - Determine which command in PATH will execute, and what order`)
  13.    puts(``)
  14.    puts(`SYNTAX: Which <CommandSpec>`)
  15.    puts(``)
  16.    puts(`WHERE: CommandSpec is any file spec.  If CommandSpec has no extension then`)
  17.    printf(`       will search executables in this order:`)
  18.    for ( i = 0; i <= GetArraySpan(ExecuteOrder); i++ )
  19.       printf(" %s",ExecuteOrder[i])
  20.    puts(``)
  21.    puts(`EXAMPLES: Which EDIT`)
  22.    puts(`          Which SESSION.C*`)
  23.    exit(EXIT_FAILURE);
  24. }
  25.  
  26. main(argc,argv)
  27. {
  28.    if ( argc != 2 )   Instructions();
  29.  
  30.    NameParts = SplitFileName(argv[1]);
  31.    if ( NameParts.dir[0] ) {
  32.       printf("\aWHICH whill not work with directory specifications.\n");
  33.       exit(EXIT_FAILURE);
  34.    }
  35.  
  36.    // if extension then use it, else use our extension list
  37.    if ( NameParts.ext[0] ) {
  38.       Extensions[0] = NameParts.ext;
  39.       ExtensionCount = 1;
  40.    } else {
  41.       Extensions = ExecuteOrder;
  42.       ExtensionCount = 1 + GetArraySpan(ExecuteOrder);
  43.    }
  44.  
  45.    // get prioritized list of all directories
  46.    PathCount = BuildPathList(PathList);
  47.  
  48.    // for each directory, find all of the matching files
  49.    FindCount = 0;
  50.    for ( PathIdx = 0; PathIdx < PathCount; PathIdx++ ) {
  51.       for ( ExtensionIdx = 0; ExtensionIdx < ExtensionCount; ExtensionIdx++ ) {
  52.          FindCount += FindFileInDirectory(PathList[PathIdx],NameParts.name,Extensions[ExtensionIdx]);
  53.       }
  54.    }
  55.  
  56.    if ( !FindCount ) {
  57.       printf("\aNo commands found for %s\n",argv[1]);
  58.       exit(EXIT_FAILURE);
  59.    }
  60.  
  61. }
  62.  
  63.  
  64. BuildPathList(List)
  65. {
  66.    Count = 0;
  67.    // First path will always be the current one
  68.    List[Count++] = FullPath(".");
  69.  
  70.    // get full path of each directory from the PATH variable
  71.    if ( Path = getenv("PATH") ) {
  72.       for ( Dir = strtok(Path,";"); Dir; Dir = strtok(NULL,";") ) {
  73.          if ( Dir[0] )
  74.             List[Count++] = FullPath(Dir);
  75.       }
  76.    }
  77.  
  78.    // make sure each element in path list ends in '\'
  79.    for ( i = 0; i < Count; i++ ) {
  80.       if ( strcmp(":\\",List[i]+1) )
  81.          strcat(List[i],"\\");
  82.    }
  83.    return Count;
  84. }
  85.  
  86. FindFileInDirectory(DirSpec,NameSpec,ExtSpec)
  87. {
  88.    sprintf(FindSpec,"%s%s%s",DirSpec,NameSpec,ExtSpec);
  89.    if ( !(FindList = Directory(FindSpec,False,FATTR_RDONLY|FATTR_ARCHIVE)) )
  90.       return 0;
  91.  
  92.    Count = 1 + GetArraySpan(FindList);
  93.    for ( i = 0; i < Count; i++ )
  94.       printf("  %s\n",FindList[i].name);
  95.    return Count;
  96. }
  97.  
  98.