home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol1 / dos / which.c < prev    next >
C/C++ Source or Header  |  1990-01-26  |  5KB  |  230 lines

  1. (c)  Copyright 1989 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice, and 
  3. is provided "as is" without warranty of any kind, either expressed or implied.  
  4. The entire risk as to the use of this information is assumed by the user.
  5.  
  6.  
  7.  
  8.                     WHICH.C - Locates your commands
  9.  
  10.      Which.c is a command that helps you to find your other commands.  It 
  11. searches your current directory, then your paths, then C: just as AmigaDOS
  12. does when trying to load a CLI command.  It is extremely useful when you 
  13. want to update a command on your hard drive but have forgotten where you 
  14. put it.  It can also tell you the name of a directory assigned to a logical 
  15. label (example:  which devs:).  In addition, which.c provides example source 
  16. for path searching and building of full path names.
  17.  
  18.  
  19. /*
  20.  *  which.c  ---  C. Scheppner  CBM  1/87
  21.  *
  22.  *     Usage:  which filename
  23.  *     Tells you the path to named command or directory
  24.  */
  25.  
  26. #include <exec/types.h>
  27. #include <exec/memory.h>
  28. #include <libraries/dos.h>
  29. #include <libraries/dosextens.h>
  30.  
  31. #define SBUFSZ 256
  32. #define CBUFSZ 80
  33.  
  34. extern BOOL getPath();
  35.  
  36. struct Path
  37.    {
  38.    BPTR  path_Next;
  39.    LONG  path_Lock;
  40.    };
  41.  
  42. main(argc,argv)
  43. int argc;
  44. char **argv;
  45.    {
  46.    struct Process *proc;
  47.    struct CommandLineInterface *cli;
  48.    struct Path *path;
  49.    struct FileInfoBlock *fib;
  50.    APTR oldWindowPtr;
  51.    LONG lock, startcd;
  52.    BOOL Found, InitialCD, FullPath;
  53.    int  i;
  54.    char *command, sbuf[SBUFSZ], cbuf[CBUFSZ];
  55.  
  56.    /* Fail from WB, give usage from CLI if command name not passed */
  57.    if(argc == 0)  exit(RETURN_ERROR);
  58.    if((argc != 2)||(*argv[1]=='?'))
  59.       printf("Usage: which filename\n"), exit(RETURN_OK);
  60.    command = argv[1];
  61.  
  62.    /* Fail if not CLI process */
  63.    proc = (struct Process *)FindTask(NULL);
  64.    cli = (struct CommandLineInterface *)(proc->pr_CLI << 2);
  65.    if(!cli)  exit(RETURN_ERROR);
  66.  
  67.    /* Allocate a FileInfoBlock - must be longword aligned */
  68.    if(!(fib=(struct FileInfoBlock *)
  69.      AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)))
  70.       printf("Not enough memory\n"), exit(RETURN_FAIL);
  71.  
  72.    /* Save old WindowPtr, and disable volume requesters */
  73.    oldWindowPtr = proc->pr_WindowPtr;
  74.    proc->pr_WindowPtr = (APTR)-1L;
  75.  
  76.    /* Were we given full path in command name ? */
  77.    for(FullPath = FALSE, i=0; i<strlen(command); i++)
  78.      {
  79.      if(command[i] == ':')
  80.         {
  81.         FullPath = TRUE;
  82.         break;
  83.         }
  84.      }
  85.  
  86.    /* Check current directory */
  87.    if(Found = getPath(command,fib,sbuf))
  88.       {
  89.       if((!FullPath)&&(command[0]))  strcpy(sbuf,command);
  90.       }
  91.  
  92.    /* Check paths */
  93.    if((!Found)&&(!FullPath))
  94.       {
  95.       InitialCD = TRUE;
  96.       /* Follow the BPTR path list */
  97.       for(path = (struct Path *) BADDR(cli->cli_CommandDir);
  98.           (path) && (!Found);
  99.             path = (struct Path *) BADDR(path->path_Next))
  100.          {
  101.          /* CD to each path */
  102.          lock = CurrentDir(path->path_Lock);
  103.          if(InitialCD)  startcd = lock, InitialCD = FALSE;
  104.  
  105.          /* See if command is there */
  106.          Found = getPath(command,fib,sbuf);
  107.          }
  108.       /* If we CD'd anywhere, restore initial CD */
  109.       if(! InitialCD)  CurrentDir(startcd);
  110.       }
  111.  
  112.    /* Check C: */
  113.    if((!Found)&&(!FullPath))
  114.       {
  115.       strcpy(cbuf,"C:");
  116.       strcpy(&cbuf[2],command);
  117.       if(Found = getPath(cbuf,fib,sbuf))  strcpy(sbuf,cbuf);
  118.       }
  119.  
  120.    if(Found)  printf("%s\n",sbuf);
  121.    else  printf("%s Not Found\n",command);
  122.  
  123.    /* Re-enable volume requesters */
  124.    proc->pr_WindowPtr = oldWindowPtr;
  125.    /* We wouldn't have got here if we hadn't allocated this */
  126.    FreeMem(fib, sizeof(struct FileInfoBlock));
  127.  
  128.    /* Return warning if not found */
  129.    exit(Found ? RETURN_OK : RETURN_WARN);
  130.    }
  131.  
  132.  
  133. BOOL
  134. getPath(command,fib,buf)
  135. char *command;
  136. struct FileInfoBlock *fib;
  137. char *buf;
  138.    {
  139.    LONG lock;
  140.    BOOL Success = FALSE;
  141.  
  142.    if(lock = Lock(command,ACCESS_READ))
  143.       {
  144.       if(Examine(lock,fib))
  145.          {
  146.          /* File or Dir found.  To distinguish, check fib_DirEntryType */
  147.          Success = TRUE;
  148.          buildPath(lock,fib,buf);
  149.          }
  150.       UnLock(lock);
  151.       }
  152.    return(Success);
  153.    }
  154.  
  155.  
  156. buildPath(inlock,fib,buf)
  157. LONG inlock;
  158. struct FileInfoBlock *fib;
  159. char *buf;
  160.    {
  161.    int i;
  162.    LONG lock,oldlock;
  163.    BOOL MyOldLock = FALSE;
  164.  
  165.    buf[0] = NULL;
  166.    lock = inlock;
  167.  
  168.    while(lock)
  169.       {
  170.       if(Examine(lock,fib))
  171.          {
  172.          if(fib->fib_FileName[0] > ' ')
  173.             {
  174.             if(buf[0]) insert(buf,"/");
  175.             insert(buf,fib->fib_FileName);
  176.             }
  177.          }
  178.       oldlock = lock;
  179.       lock = ParentDir(lock);
  180.       if(MyOldLock)  UnLock(oldlock);
  181.       else           MyOldLock = TRUE;
  182.       }
  183.  
  184.    if(fib->fib_FileName[0] > ' ')
  185.       {
  186.       for(i=0; i<(strlen(buf)); i++)
  187.          {
  188.          if(buf[i] == '/')
  189.             {
  190.             buf[i] = ':';
  191.             break;
  192.             }
  193.          }
  194.       }
  195.    else  insert(buf,"RAM:");
  196.  
  197.    return(strlen(buf));
  198.    }
  199.  
  200. insert(buf,s)
  201. char *buf,*s;
  202.    {
  203.    char tmp[SBUFSZ];
  204.  
  205.    strcpy(tmp,buf);
  206.    strcpy(buf,s);
  207.    strcpy(&buf[strlen(s)],tmp);
  208.    }
  209.  
  210. strlen(s)
  211. char *s;
  212.    {
  213.    int i = 0;
  214.    while(*s++) i++;
  215.    return(i);
  216.    }
  217.  
  218.  
  219. strcpy(to,from)
  220. char *to, *from;
  221.    {
  222.    do
  223.       {
  224.       *to++ = *from;
  225.       }
  226.    while(*from++);
  227.    }
  228.  
  229.  
  230.