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

  1. /*
  2.  *    Name        PD
  3.  *
  4.  *    Function    Push Directory Utility
  5.  *
  6.  *    Author        Joseph Boykin
  7.  *            47-4 Sheridan Drive
  8.  *            Shresbury, MA    01545
  9.  *            617-845-1074
  10.  *
  11.  *    Version        1.00
  12.  *
  13.  */
  14.  
  15. #define    PATHSZ    66
  16. char    *stkfile = "f:\dir.stk";    /* Stack file, preferably on a ramdisk */
  17. int    fd;                /* Stack file File Descriptor */
  18. short    int    nrecs;            /* Number of records on stack */
  19. char    name[PATHSZ] = "\\";        /* Current directory */
  20.  
  21. int    rflg, sflg;            /* Command line flags */
  22.  
  23. extern    long    _xlseek();
  24.  
  25. main(argc, argv)
  26. int    argc;
  27. char    *argv[];
  28. {
  29.     char *s;
  30.  
  31.     /*
  32.      * Parse the command line and deal with
  33.      *  command line options.
  34.      */
  35.     rflg = sflg = 0;
  36.     while(--argc > 0 && (*++argv)[0] == '-') {
  37.         for(s=argv[0]+1; *s ; s++) {
  38.             switch(*s) {
  39.  
  40.                 case 'r':
  41.                 case 'R':    rflg++;
  42.                         break;
  43.  
  44.                 case 's':
  45.                 case 'S':    sflg++;
  46.                         break;
  47.  
  48.                 default:    printf("Unknown command: %c\r\n", *s);
  49.                         argc = -1;
  50.                         break;
  51.             }
  52.         }
  53.     }
  54.  
  55.     if(_xgetdir(name+1, 0) < 0) {
  56.         cprintf("Can't get current directory.\r\n");
  57.         _xexit(1);
  58.     }
  59.  
  60.     /*
  61.      * See if the stack file exists. If so, read the number of records
  62.      *  currently on the stack, otherwise, try to create and initialize
  63.      *  the file.
  64.      */
  65.  
  66.     if((fd = _xopen(stkfile, 2)) < 0) {
  67.         if((fd = _xcreat(stkfile, 0)) < 0) {
  68.             cprintf("Cannot open/create stack file.\r\n");
  69.             _xexit(1);
  70.         } else {
  71.             nrecs = 0;
  72.             if(_xwrite(fd, (char *)&nrecs, sizeof(short int)) !=
  73.               sizeof(short int)) {
  74.                 cprintf("Unable to initialize stack file.\r\n");
  75.                 _xclose(fd);
  76.                 _xexit(1);
  77.             }
  78.         }
  79.     } else
  80.         doread(fd, &nrecs, sizeof(short int));
  81.  
  82.     /*
  83.      * Deal with the case of the user only wanting to print out 
  84.      *  the stack contents.  This needs to be done after we
  85.      *  read/initialize the stack file so if there was a command
  86.      *  error we went through alot of work for nothing.
  87.      */
  88.  
  89.     if(sflg && argc == 0 && !rflg) {
  90.         prstk();
  91.         _xexit(0);
  92.     } else {
  93.         if(argc != 1) {
  94.             cprintf("Usage: pd [-rs] directory.\r\n");
  95.             _xexit(2);
  96.         }
  97.     }
  98.  
  99.     if(rflg && nrecs) {
  100.         rflg = -1;
  101.         nrecs--;
  102.     }
  103.  
  104.     /*
  105.      * Find the right place in the file to store the current
  106.      *  directory name.
  107.      */
  108.  
  109.     if(_xlseek(fd, ((long) PATHSZ*(long) nrecs) + sizeof(short int),0) < 0) {
  110.         cprintf("Seek error on stack file.\r\n");
  111.         _xexit(1);
  112.     }
  113.  
  114.     if(_xchdir(argv[0]) < 0) {
  115.         cprintf("Couldn't change directories.\r\n");
  116.         _xchdir(name);
  117.         _xexit(1);
  118.     }
  119.  
  120.     if(_xwrite(fd, name, PATHSZ) != PATHSZ) {
  121.         cprintf("Error writing to stack file.\r\n");
  122.         _xchdir(name);
  123.         _xexit(1);
  124.     }
  125.  
  126.     nrecs++;
  127.     prstk();
  128.     if(rflg == -1) {
  129.         _xexit(0);
  130.     }
  131.  
  132.     /*
  133.      * Update nrecs field in file and quit.
  134.      */
  135.  
  136.     if(_xlseek(fd, (long) 0, 0) < 0) {
  137.         cprintf("Seek error on stack file.\r\n");
  138.         _xchdir(name);
  139.         _xexit(1);
  140.     }
  141.  
  142.     if(_xwrite(fd, (char *)&nrecs, sizeof(short int)) != sizeof(short int)) {
  143.         cprintf("Error writing to stack file.\r\n");
  144.         _xchdir(name);
  145.         _xexit(1);
  146.     }
  147.  
  148.     _xexit(0);
  149. }
  150.  
  151. /*
  152.  * Perform a read operation.  Checks for error and
  153.  *  and terminate if got one.
  154.  */
  155.  
  156. doread(fid, name, sz)
  157. int    fid;
  158. char    *name;
  159. int    sz;
  160. {
  161.     if(_xread(fid, name, sz) != sz) {
  162.         cprintf("Error reading file.\r\n");
  163.         _xexit(0);
  164.     }
  165. }
  166.  
  167. /*
  168.  * Print out the contents of the directory stack.
  169.  */
  170.  
  171. prstk()
  172. {
  173.     int    i;
  174.     char    tname[PATHSZ];
  175.  
  176.     if(sflg) {
  177.         if(nrecs == 0) {
  178.             cprintf("Directory stack is empty.\r\n");
  179.             _xexit(0);
  180.         }
  181.         cprintf("Current Stack:\r\n");
  182.         for(i=0; i < nrecs; i++) {
  183.             _xlseek(fd, ((long)PATHSZ*(long)i)+sizeof(short int),0);
  184.             doread(fd, tname, PATHSZ);
  185.             cprintf("    %s\r\n", tname);
  186.         }
  187.     }
  188. }
  189.  
  190.