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

  1. /*  Push.C - This program, when used with Pop.C, provides
  2.             a mechanism by which the current working disk
  3.             and path may be saved in a memory stack and
  4.             restored later by Pop.C.
  5.  
  6.             Warning: This software uses undocumented
  7.                      features of MS-DOS
  8.  
  9.     Copyright Michael Klos 1990, All rights reserved.
  10.  
  11.     Compiles with Microsoft Quick C, Large or Huge Model
  12. */
  13.  
  14. #ifdef   Debug
  15. #include <stdio.h>         /*  For debug only   */
  16. #endif
  17.  
  18. #include <stdlib.h>
  19. #include <direct.h>
  20. #include <dos.h>
  21. #include "Push_Pop.h"
  22.  
  23. void    main(void)
  24. {
  25. word    allocation_mode;
  26. word    path_buffer;
  27.  
  28.             /*  Get and save the current memory
  29.                 allocation strategy. */
  30.     allocation_mode = _dos_getmemmode();
  31.  
  32.             /*  Change the strategy to get a block 
  33.                 from the end of memory. */
  34.     _dos_setmemmode(_allocate_last);
  35.  
  36.     if (_dos_allocmem((_MAX_PATH+15)/16, &path_buffer)) {
  37.        _dos_setmemmode(allocation_mode);    /*  Restore */
  38.        exit(1);
  39.        }
  40.                        /*  Restore strategy.  */
  41.    _dos_setmemmode(allocation_mode);
  42.  
  43. #ifdef Debug
  44.     printf("Segment allocated for path is: %4X\n", path_buffer);
  45. #endif
  46.  
  47.     FP_OFF(mem) = 0;
  48.     FP_SEG(mem) = path_buffer - 1;
  49.  
  50. /*  Assign ownership of this memory block to push/pop by
  51.     assigning a block owner that MS-DOS cannot assign and
  52.     which pop can identify later */
  53.     
  54.     mem->block[1] = low(Signature);
  55.     mem->block[2] = high(Signature);
  56.  
  57.     /* Get the current working directory. */
  58.     getcwd(&mem->block[16], _MAX_PATH);
  59.  
  60. #ifdef Debug
  61.     printf("Current directory is: %s\n", &mem->block[16]);
  62. #endif
  63.     exit(0);    /*  Report success                  */
  64. }
  65.  
  66. int     _dos_getmemmode(void)
  67. {
  68. union REGS registers;
  69.  
  70.     /* WARNING: UNDOCUMENTED MS-DOS FUNCTION   */
  71.     /* Get the current memory allocation strategy. */
  72.     registers.x.ax = 0x5800;
  73.     return intdos(®isters, ®isters);
  74. }
  75.  
  76. void    _dos_setmemmode(int strategy)
  77. {
  78. union REGS registers;
  79.  
  80.     /* WARNING: UNDOCUMENTED MS-DOS FUNCTION   */
  81.     /* Set a new memory allocation strategy.  */
  82.     registers.x.ax = 0x5800;
  83.     registers.h.bl = (unsigned char) strategy;
  84.     intdos(®isters, ®isters);
  85. }
  86.