home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / c / cc03.arc / POPD.C < prev    next >
Text File  |  1985-08-08  |  3KB  |  132 lines

  1. #include "ctype.h"
  2. /*
  3.  *    Name        Popd
  4.  *
  5.  *    Function    Pop Directory Utility
  6.  *
  7.  *    Author        Joseph Boykin
  8.  *            47-4 Sheridan Drive
  9.  *            Shrewsbury, MA    01545
  10.  *            617-845-1074
  11.  *
  12.  *    Version        1.00
  13.  */
  14.  
  15. #define    PATHSZ    66
  16.  
  17. char    *stkfile = "F:\DIR.STK";    /* Stack file */
  18.  
  19. int    fd;                /* Open file File Descriptor */
  20. short    int    nrecs;            /* Number of records on stack */
  21. char    name[PATHSZ];            /* Name of directory to go to */
  22.  
  23. extern    long    _xlseek();
  24.  
  25. main(argc, argv)
  26. int    argc;
  27. char    *argv[];
  28. {
  29.     int    pflg = 0;
  30.  
  31.     /*
  32.      * Parse command line
  33.      *  -p says to not remove the last entry from the stack
  34.      */
  35.  
  36.     if(argc >= 2) {
  37.         if(argv[1][0] == '-' && (tolower(argv[1][1]) == 'p')) {
  38.             pflg++;
  39.             argc--;
  40.             argv++;
  41.         } else {
  42.             cprintf("Invalid command line option\r\n");
  43.             _xexit(1);
  44.         }
  45.     }
  46.  
  47.     if(argc != 1) {
  48.         cprintf("Usage: popd\r\n");
  49.         _xexit(1);
  50.     }
  51.  
  52.     /*
  53.      * Open stack file (if present) and read number of records field
  54.      */
  55.  
  56.     if((fd = _xopen(stkfile, 2)) < 0) {
  57.         cprintf("Stack is empty\r\n");
  58.         _xexit(0);
  59.     }
  60.     if(_xread(fd,(char *)&nrecs, sizeof(short int)) != sizeof(short int)) {
  61.         cprintf("Unable to read stack file\r\n");
  62.         _xexit(1);
  63.     }
  64.  
  65.     /*
  66.      * Nrecs should never be read as being zero since we delete
  67.      *  the file when a popd command causes nrecs to become
  68.      *  zero, but deal with the possibility anyway.
  69.      */
  70.     if(nrecs == 0) {
  71.         _xclose(fd);
  72.         _xunlink(stkfile);
  73.         cprintf("Stack is empty\r\n");
  74.         _xexit(0);
  75.     }
  76.  
  77.     /*
  78.      * Seek to the appropriate entry in the file and
  79.      *  read the directory name.
  80.      */
  81.     if(_xlseek(fd, ((long)PATHSZ*(long)(nrecs-1))+sizeof(short int), 0) <
  82.        (long) 0) {
  83.         cprintf("Seek error on stack file\r\n");
  84.         _xexit(1);
  85.     }
  86.     if(_xread(fd, name, PATHSZ) != PATHSZ) {
  87.         cprintf("Error reading stack file\r\n");
  88.         _xexit(1);
  89.     }
  90.  
  91.     /*
  92.      * Change to the destination directory.  An error would mean
  93.      *  that the user changed disks (physically or logically)
  94.      *  between the push and pop commands.
  95.      */
  96.     if(_xchdir(name) < 0) {
  97.         cprintf("Error changing directories\r\n");
  98.         _xexit(1);
  99.     }
  100.  
  101.     /*
  102.      * If we don't want to remove the entry from the stack
  103.      *  just exit now.
  104.      */
  105.     if(pflg)
  106.         _xexit(0);
  107.  
  108.     /*
  109.      * If that was the last entry on the stack remove the file.
  110.      *  Since the size of the file is never truncated on other
  111.      *  popd commands, this is our one way of making sure that
  112.      *  the file maintains a reasonable size.
  113.      * Otherwise, seek to the beginning of the file and rewrite
  114.      *  the nrecs field.
  115.      */
  116.     if(--nrecs <= 0) {
  117.         _xclose(fd);
  118.         _xunlink(stkfile);
  119.     } else {
  120.         if(_xlseek(fd, (long) 0, 0) < (long) 0) {
  121.             cprintf("Seek error on stack file\r\n");
  122.             _xexit(1);
  123.         }
  124.         if(_xwrite(fd, (char *)&nrecs, sizeof(short int)) !=
  125.            sizeof(short int)) {
  126.             cprintf("Error writing to stack file\r\n");
  127.             _xexit(1);
  128.         }
  129.     }
  130.     _xexit(0);
  131. }
  132.