home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_02 / 1n02061a < prev    next >
Text File  |  1990-07-09  |  6KB  |  149 lines

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