home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_03 / 2n03073a < prev    next >
Text File  |  1991-01-14  |  3KB  |  86 lines

  1. /*
  2. Pop.C   This program, when used with Push.C, provides a
  3.         mechanism by  which the current working disk and
  4.         path may be saved in a memory stack and restored
  5.         later.
  6.  
  7. Warning: This software uses undocumented features of MS-DOS
  8.  
  9.         Copyright Michael Klos 1990, All rights reserved.
  10.         Compiles with Microsoft Quick C, Large or Huge Model
  11. */
  12.  
  13. #ifdef Debug
  14. #include <stdio.h>         /*  For debug only  */
  15. #endif
  16.  
  17. #include <stdlib.h>
  18. #include <direct.h>
  19. #include <dos.h>
  20. #include "Push_Pop.h"
  21.  
  22. void    main(void)
  23. {
  24. word    path_buffer;
  25. byte    owner[2];
  26. word    drives;
  27.  
  28.     /*  Allocate a memory block so that pop has a point in
  29.         the chain to the top of memory to begin searching
  30.         for the saved path.     */
  31.  
  32.     if (_dos_allocmem(1, &path_buffer))
  33.     exit(1);
  34.  
  35.     /*  Point back to the memory control block (MCB)
  36.         for the block just allocated.   */
  37.  
  38.     path_buffer -= 1;
  39.     FP_OFF(mem)  = 0;
  40.     FP_SEG(mem)  = path_buffer;
  41.  
  42.     /*  Save the current process owner number.  This is
  43.         used later to  release the memory block containing
  44.         the saved path; if it is found. */
  45.  
  46.     owner[0] = mem->block[1];
  47.     owner[1] = mem->block[2];
  48.  
  49.             /*  Do until the end of the memory
  50.                 control block chain is found.        */
  51.     for (;mem->block[0] == chain_of_MCB;)
  52.     {
  53.  
  54.     /*  Point to the next memory control block by
  55.         adding the size of the current memory control
  56.         block to our current segment and adding 1 for
  57.         the memory control block header.             */
  58.  
  59.      path_buffer += (mem->block[4] << 8) + mem->block[3] + 1;
  60.      FP_SEG(mem) = path_buffer;
  61.  
  62.        /*  Check the memory control block owner with our
  63.            signature, if they match then pop the path...*/
  64.  
  65.      if (mem->block[2] == high(Signature) &
  66.              mem->block[1] == low(Signature))
  67.      {
  68.       /*  Make this process the owner of the block so that
  69.           MS-DOS releases it when this process terminates. */
  70.  
  71.        mem->block[1] = owner[0];
  72.        mem->block[2] = owner[1];
  73.  
  74. #ifdef Debug
  75.        printf("Saved directory is:%s", &mem->block[16]);
  76. #endif
  77.                     /*  Set the default disk drive.  */
  78.        _dos_setdrive(mem->block[16]-0x40,&drives);
  79.  
  80.                   /*  Set the default path and exit */
  81.        exit(chdir(&mem->block[16]) ? 1 : 0);
  82.      }
  83.     }
  84.     exit(1);       /*  No saved path was found         */
  85. }
  86.