home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / WHICH.CMM < prev    next >
Text File  |  1996-01-24  |  2KB  |  76 lines

  1. //********************************************************************
  2. //*** Which.cmm - Show when command would be executed by searching ***
  3. //*** ver.2       for command along path.                          ***
  4. //********************************************************************
  5.  
  6. #include <netware.lib>
  7. #include <filename.lib>
  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(executable_extensions); i++ )
  19.       printf(" %s",executable_extensions[i])
  20.    puts(``)
  21.    puts(`EXAMPLES: Which EDIT`)
  22.    puts(`          Which SESSION.C*`)
  23.    exit(EXIT_FAILURE);
  24. }
  25.  
  26. /* ---------------------------------------------------------------------- */
  27.  
  28. main(argc,argv)
  29. {
  30.    if ( argc != 2 )   Instructions();
  31.  
  32.    NameParts = SplitFileName(argv[1]);
  33.    if ( NameParts.dir[0] ) {
  34.       printf("\aWHICH will not work with directory specifications.\n");
  35.       exit(EXIT_FAILURE);
  36.    }
  37.  
  38.    // if extension then use it, else use our extension list
  39.    if ( NameParts.ext[0] ) {
  40.       Extensions[0] = NameParts.ext;
  41.       ExtensionCount = 1;
  42.    } else {
  43.       Extensions = executable_extensions;
  44.       ExtensionCount = 1 + GetArraySpan(executable_extensions);
  45.    }
  46.    // get prioritized list of all directories
  47.    PathList = build_path_list();
  48.    PathCount = (PathList==NULL)?0:GetArraySpan(PathList)+1;
  49.  
  50.    // for each directory, find all of the matching files
  51.    FindCount = 0;
  52.    for ( PathIdx = 0; PathIdx < PathCount; PathIdx++ ) {
  53.       for ( ExtensionIdx = 0; ExtensionIdx < ExtensionCount; ExtensionIdx++ ) {
  54.          FindCount += FindFileInDirectory(PathList[PathIdx],NameParts.name,Extensions[ExtensionIdx]);
  55.       }
  56.    }
  57.  
  58.    if ( !FindCount ) {
  59.       printf("\aNo commands found for %s\n",argv[1]);
  60.       exit(EXIT_FAILURE);
  61.    }
  62.  
  63. }
  64.  
  65. FindFileInDirectory(DirSpec,NameSpec,ExtSpec)
  66. {
  67.    sprintf(FindSpec,"%s%s%s",DirSpec,NameSpec,ExtSpec);
  68.    if ( !(FindList = Directory(FindSpec,False,_A_RDONLY|_A_ARCH)) )
  69.       return 0;
  70.  
  71.    Count = 1 + GetArraySpan(FindList);
  72.    for ( i = 0; i < Count; i++ )
  73.       printf("  %s\n",FindList[i].name);
  74.    return Count;
  75. }
  76.