home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / SNIP9404.ZIP / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  5KB  |  193 lines

  1. /*
  2. **  PushDir() and PopDir()
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is an expanded version of the one
  8. **  originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <dos.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16.  
  17. #ifdef __TURBOC__
  18.  #include <dir.h>
  19. #else
  20.  #include <direct.h>
  21. #endif
  22.  
  23. #define DIR_STACK_SIZE  8
  24. #define MAX_FLEN        67
  25.  
  26. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  27.  
  28. #define BOOL(x) (!(!(x)))
  29.  
  30. /*
  31. **  NOTE: Uses the author's chdrv(), also in SNIPPETS!
  32. */
  33.  
  34. int chdrv(int);
  35.  
  36. static int  PushDir_stack_ptr;
  37. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  38.  
  39. /*
  40. **  PushDir()
  41. **
  42. **  Like chdir(), except a drive may be specified and the old directory
  43. **  is saved.
  44. **
  45. **  Arguments: 1 - newdir, the buffer containing the new directory name
  46. **
  47. **  Returns:  -1 - stack overflow
  48. **             0 - error
  49. **             1 - success, still on same drive
  50. **             2 - success, changed drive
  51. **
  52. **  Side effects: Converts name in newdir to upper case and prepends
  53. **                a drive letter.
  54. **
  55. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  56. **           should be at at least MAX_FLEN long.
  57. */
  58.  
  59. int PushDir(char *newdir)
  60. {
  61.       char pname[MAX_FLEN];
  62.       char drive[3];
  63.       char *target = &pname[2];
  64.       int  new_drv = 0, ercode = 0;
  65.       static int init = 0;
  66.  
  67.       if (!init)
  68.             PushDir_stack_ptr = init = -1;
  69.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  70.       {
  71.             ercode = -1;
  72.             goto ErrEx;
  73.       }
  74.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  75.       strupr(PushDir_stack[PushDir_stack_ptr]);
  76.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  77.       drive[2] = '\0';
  78.       if (':' == newdir[1])
  79.       {     /* If a drive is specified                                  */
  80.             strupr(newdir);
  81.             strcpy(pname, newdir);
  82.             if (strchr(target, ':'))      /* if filename is illegal     */
  83.                   goto ErrEx;
  84.             if (*drive != *newdir)
  85.             {
  86.                   if (ERROR == chdrv(newdir[0] - 'A'))
  87.                   {     /* If the drive is invalid                      */
  88.                         goto ErrEx;
  89.                   }
  90.                   else  new_drv = 1;
  91.             }
  92.       }
  93.       else
  94.       {     /* If a drive isn't specified                               */
  95.             if (!strchr(strupr(newdir), ':'))
  96.             {     /* If legal filename                                  */
  97.                   strcpy(pname, drive);
  98.                   strcat(pname, newdir);
  99.                   strcpy(newdir, pname);
  100.             }
  101.             else
  102.             {     /* If filename is illegal                             */
  103.                   goto ErrEx;
  104.             }
  105.       }
  106.  
  107.       if (*target)
  108.       {
  109.             if (chdir(target))
  110.             {
  111.                   if (1 == new_drv) /* We already changed drives        */
  112.                         chdrv(*drive - 'A');    /* Go home before exit  */
  113.                   goto ErrEx;
  114.             }
  115.       }
  116.       return (new_drv + 1);
  117. ErrEx:
  118.       --PushDir_stack_ptr;
  119.       return (ercode);
  120. }
  121.  
  122. /*
  123. **  PopDir()
  124. **
  125. **  Like chdir(), except goes to the drive/directory specified on the
  126. **  top of the PushDir stack.
  127. **
  128. **  Arguments: none
  129. **
  130. **  Returns:  -1 - stack empty
  131. **             0 - error - stack pointer unchanged
  132. **             1 - success, still on same drive
  133. **             2 - success, changed drive
  134. **
  135. **  Side effects: none
  136. **
  137. **  CAUTION: chdir() or chdrv() should not be called between PushDir-
  138. **           PopDir calls.
  139. */
  140.  
  141. int PopDir(void)
  142. {
  143.       char I_am_here[MAX_FLEN], target_drv, *target;
  144.       int new_drv = 0;
  145.  
  146.       if (0 > PushDir_stack_ptr)
  147.             return -1;
  148.       getcwd(I_am_here, MAX_FLEN);
  149.       target = &PushDir_stack[PushDir_stack_ptr][2];
  150.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  151.       if (I_am_here[0] != target_drv)
  152.       {
  153.             if (ERROR == chdrv(target_drv - 'A'))
  154.                   return 0;
  155.             new_drv = 1;
  156.       }
  157.       if (!chdir(target))
  158.       {
  159.             --PushDir_stack_ptr;
  160.             return (1 + new_drv);
  161.       }
  162.       else  return 0;
  163. }
  164.  
  165. /*
  166. **  isdir()
  167. **
  168. **  Checks to see if a drive and/or path are a valid directory.
  169. **
  170. **  Arguments: 1 - dir, the buffer containing the new directory name
  171. **
  172. **  Returns: ERROR - push/popdir stack overflow
  173. **           FALSE - not a valid directory
  174. **           TRUE  - valid directory
  175. **
  176. **  Side effects: Converts name in dir to upper case and prepends a
  177. **                drive letter.
  178. **
  179. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  180. **           should be at at least MAX_FLEN long.
  181. */
  182.  
  183. int isdir(char *dir)
  184. {
  185.       int ercode;
  186.  
  187.       if (-1 == (ercode = PushDir(dir)))
  188.             return ercode;
  189.       if (ercode)
  190.             PopDir();
  191.       return BOOL(ercode);
  192. }
  193.