home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / snip1091.arj / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1991-09-14  |  6KB  |  155 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 functionally identical to the
  8. **  version 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.  
  16. #define DIR_STACK_SIZE  8
  17. #define MAX_FLEN        67
  18.  
  19. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  20.  
  21. int chdrv(int);
  22.  
  23. static int  PushDir_stack_ptr;
  24. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  25.  
  26. /************************************************************************/
  27. /*                                                                      */
  28. /*  PushDir()                                                           */
  29. /*                                                                      */
  30. /*  Like chdir(), except a drive may be specified and the old directory */
  31. /*  is saved.                                                           */
  32. /*                                                                      */
  33. /*  Arguments: 1 - newdir, the buffer containing the new directory name */
  34. /*                                                                      */
  35. /*  Returns:  -1 - stack overflow                                       */
  36. /*             0 - error                                                */
  37. /*             1 - success, still on same drive                         */
  38. /*             2 - success, changed drive                               */
  39. /*                                                                      */
  40. /*  Side effects: Converts name in newdir to upper case and prepends    */
  41. /*                a drive letter.                                       */
  42. /*                                                                      */
  43. /*  CAUTION: Since a drive will be prepended to newdir, it's buffer     */
  44. /*           should be at at least MAX_FLEN long.                       */
  45. /*                                                                      */
  46. /************************************************************************/
  47.  
  48. int PushDir(char *newdir)
  49. {
  50.       char pname[MAX_FLEN];
  51.       char drive[3];
  52.       char *target = &pname[2];
  53.       int i, new_drv = 0, ercode = 0;
  54.       static int init = 0;
  55.  
  56.       if (!init)
  57.             PushDir_stack_ptr = init = -1;
  58.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  59.       {
  60.             ercode = -1;
  61.             goto ErrEx;
  62.       }
  63.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  64.       strupr(PushDir_stack[PushDir_stack_ptr]);
  65.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  66.       drive[2] = '\0';
  67.       if (':' == newdir[1])
  68.       {     /* If a drive is specified                                  */
  69.             strupr(newdir);
  70.             strcpy(pname, newdir);
  71.             if (strchr(target, ':'))      /* if filename is illegal     */
  72.                   goto ErrEx;
  73.             if (*drive != *newdir)
  74.             {
  75.                   if (ERROR == chdrv(newdir[0]))
  76.                   {     /* If the drive is invalid                      */
  77.                         goto ErrEx;
  78.                   }
  79.                   else  new_drv = 1;
  80.             }
  81.       }
  82.       else
  83.       {     /* If a drive isn't specified                               */
  84.             if (!strchr(strupr(newdir), ':'))
  85.             {     /* If legal filename                                  */
  86.                   strcpy(pname, drive);
  87.                   strcat(pname, newdir);
  88.                   strcpy(newdir, pname);
  89.             }
  90.             else
  91.             {     /* If filename is illegal                             */
  92.                   goto ErrEx;
  93.             }
  94.       }
  95.  
  96.       if (*target)
  97.       {
  98.             if (chdir(target))
  99.             {
  100.                   if (1 == new_drv) /* We already changed drives        */
  101.                         chdrv(*drive);    /* Go home before exit        */
  102.                   goto ErrEx;
  103.             }
  104.       }
  105.       return (new_drv + 1);
  106. ErrEx:
  107.       --PushDir_stack_ptr;
  108.       return (ercode);
  109. }
  110.  
  111. /************************************************************************/
  112. /*                                                                      */
  113. /*  PopDir()                                                            */
  114. /*                                                                      */
  115. /*  Like chdir(), except goes to the drive/directory specified on the   */
  116. /*  top of the PushDir stack.                                           */
  117. /*                                                                      */
  118. /*  Arguments: none                                                     */
  119. /*                                                                      */
  120. /*  Returns:  -1 - stack empty                                          */
  121. /*             0 - error - stack pointer unchanged                      */
  122. /*             1 - success, still on same drive                         */
  123. /*             2 - success, changed drive                               */
  124. /*                                                                      */
  125. /*  Side effects: none                                                  */
  126. /*                                                                      */
  127. /*  CAUTION: chdir() or chdrv() should not be called between PushDir-   */
  128. /*           PopDir calls.                                              */
  129. /*                                                                      */
  130. /************************************************************************/
  131.  
  132. int PopDir(void)
  133. {
  134.       char I_am_here[MAX_FLEN], target_drv, *target;
  135.       int new_drv = 0;
  136.  
  137.       if (0 > PushDir_stack_ptr)
  138.             return -1;
  139.       getcwd(I_am_here, MAX_FLEN);
  140.       target = &PushDir_stack[PushDir_stack_ptr][2];
  141.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  142.       if (I_am_here[0] != target_drv)
  143.       {
  144.             if (ERROR == chdrv(target_drv))
  145.                   return 0;
  146.             new_drv = 1;
  147.       }
  148.       if (!chdir(target))
  149.       {
  150.             --PushDir_stack_ptr;
  151.             return (1 + new_drv);
  152.       }
  153.       else  return 0;
  154. }
  155.