home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CDIR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  135 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **                          CDIR.C
  5. **
  6. ** Written By:    Lynn R. Lively
  7. ** Date Written:  9/18/91
  8. **
  9. ** Purpose: To provide a change directory facility that will cross
  10. **          drive/partition boundaries. Never did understand why
  11. **          MSDOS cd wouldn't do this already.
  12. **
  13. **-----------------------------------------------------------------
  14. ** I hereby place this work into the Public Domain. It may be used
  15. ** for any legal purpose public or private. Use this material at
  16. ** your own risk. I accept no responsibility for the accuracy or
  17. ** usability of the information contained herein. Neither do I
  18. ** accept liability for any possible damage caused by use of this
  19. ** material. However, should you have a problem, question, or
  20. ** suggestion I would be glad to help in any way that I can. You
  21. ** can reach me at (H) 713-893-7875 or (W) 713-591-6611 x 149.
  22. **-----------------------------------------------------------------
  23. */
  24.  
  25. /*
  26. **                          Change History
  27. **
  28. **  Rev #   Date       By      Description of change
  29. **  1.00  | 09/18/91 | LRL | Original Version
  30. **  1.01  | 09/18/91 | RBS | Added MSC, ZTC support for SNIPPETS
  31. **-----------------------------------------------------------------
  32. ** Directory of initials:
  33. ** Initials          Name
  34. ** LRL        Lynn R. Lively
  35. ** RBS        Bob Stout
  36. */
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #if defined(__TURBOC__) || defined(__POWERC)
  43.  #include <dir.h>
  44. #else
  45.  #include <dos.h>
  46.  #include <direct.h>
  47.  
  48.  #ifdef __ZTC__
  49.   #define _dos_getdrive(d) dos_getdrive(d)
  50.   #define _dos_setdrive(d,m) dos_setdrive(d,m)
  51.  #endif
  52.  
  53.  #if defined(__ZTC__) || defined(__WATCOMC__)
  54.   #define drive_t unsigned
  55.  #else
  56.   #define drive_t int
  57.  #endif
  58.  
  59.  #if defined(_MSC_VER) || defined(__WATCOMC__)
  60.  drive_t getdisk(void)
  61.  {
  62.        drive_t drive;
  63.  
  64.        _dos_getdrive(&drive);
  65.        return drive - 1;
  66.  }
  67.  
  68.  drive_t setdisk(drive_t drive)
  69.  {
  70.        drive_t max_drives;
  71.  
  72.        _dos_setdrive(drive + 1, &max_drives);
  73.        return max_drives - 1;
  74.  }
  75.  #endif
  76. #endif
  77.  
  78. main (int argc, char * argv[])
  79. {
  80.       int d;
  81.       int max_d;
  82.  
  83.       char wk_str[128];
  84.  
  85.       if (argc > 1)
  86.       {
  87.             strupr (argv[1]);
  88.             if (argv[1][1] == ':')
  89.             {
  90.                   /*
  91.                   ** Find out what the maximum drive number can be.
  92.                   */
  93.  
  94.                   max_d = getdisk ();
  95.                   max_d = setdisk (max_d);
  96.  
  97.                   d = argv[1][0] - 'A';
  98.                   if (d < max_d)
  99.                   {
  100.                         /*
  101.                         ** If the drive specification was valid position
  102.                         ** to it and then do a change directory.
  103.                         */
  104.  
  105.                         setdisk (d);
  106.                         chdir (argv[1]);
  107.                   }
  108.                   else
  109.                   {
  110.                         puts ("Invalid drive specification");
  111.                         return -1;
  112.                   }
  113.             }
  114.             else
  115.             {
  116.                   /*
  117.                   ** If the argument has no drive spec just do a regular
  118.                   ** change directory.
  119.                   */
  120.  
  121.                   chdir (argv[1]);
  122.             }
  123.       }
  124.       else
  125.       {
  126.             /*
  127.             ** If no arguments are passed, return the current working
  128.             ** directory path just like MSDOS cd does.
  129.             */
  130.  
  131.             puts (getcwd (wk_str, sizeof (wk_str)));
  132.       }
  133.       return 0;
  134. }
  135.