home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 225_01 / cleanup.c < prev    next >
Text File  |  1987-06-09  |  9KB  |  249 lines

  1.  
  2. /*-----------------------------------------------------------------*/
  3. /*       STANDARD HEADER FOR BDS C PROGRAMMES
  4.          ------------------------------------
  5.  
  6.          Programme:     CLEANUP.C
  7.          ---------
  8.          Purpose:       This programme will delete all inactive
  9.          -------        entries from the directory and then re-write
  10.                         the directory in (almost) alphabetical order.
  11.  
  12.          Written:       05/11/85
  13.          -------
  14.          Amended:       15/11/86
  15.          -------
  16.          Version:       1.1
  17.          -------
  18.  
  19.          Copyright 1986 - Cogar Computer Services Pty. Ltd.        */
  20. /*-----------------------------------------------------------------*/
  21. #include <bdscio.h>
  22. #include <pec.h>
  23. /*-----------------------------------------------------------------*/
  24. #define    VERSION "1.1\0"
  25. #define NAME    "CLEANUP\0"
  26. /*-----------------------------------------------------------------*/
  27.  
  28. main(argc, argv)    /* For Command Line processing             */
  29. int argc;
  30. char *argv[];
  31. {
  32. /*-----------------------------------------------------------------*/
  33. /*  Space reserved for variables used in programme                 */
  34. /*-----------------------------------------------------------------*/
  35.     int i, j, k, l, FD;
  36.     char c;
  37.     int CPM;    /* To check the CP/M Version number        */
  38.     char DRIVE;    /* The active drive                        */
  39.     char OLD_DRIVE;    /* The active drive at start of programme  */
  40.     char OLD_USER;    /* The User No. at start of programme      */
  41.     char USER;    /* The User No. for this programme         */
  42.     char my_dma[128];    /* The dma buffer to use           */
  43.     char **skew_table;
  44.     int  PHYS_SEC;    /* The physical sector to read/write       */
  45.     unsigned STORAGE;    /* The size of the big buffer      */
  46.     struct dpb *THIS;    /* Disk parameter information      */
  47.     int  DIRECTORY;    /* The directory track                     */
  48.     int  SECTORS;    /* Sectors per track                       */
  49.     int  ENTRIES;    /* The active entries in directory         */
  50.     char *BIG_BUF;
  51. /*-----------------------------------------------------------------*/
  52.     pec_clear();    /* Universal routine                       */
  53.     printf("%s - Version %s\n",NAME, VERSION);
  54.     printf("Copyright 1986 - Cogar Computer Services Pty.Ltd.\n");
  55.     printf("----------------------------------------------------------\n");
  56.     printf("This programme is designed to 'clean up' the directory\n");
  57.     printf("tracks by removing any inactive (0xe5) entries.\n");
  58.     printf("At the same time as it does this it sorts the active\n");
  59.     printf("entries into (approximate) alphabetical order.\n");
  60.     printf("----------------------------------------------------------\n");
  61. /*-----------------------------------------------------------------*/
  62. /*  First check the CP/M Version in use.   If it is less than
  63.     Version 2.0 then inform the user and terminate programme.      */
  64. /*-----------------------------------------------------------------*/
  65.  
  66.     CPM = get_cpm();    /* Obtain the CP/M version and No. */
  67.  
  68.     i = (CPM & 0xff) - 0x20; /* Mask off the MP/M bit          */
  69.  
  70.     if(i < 0)        /* Must be less than V 2.0         */
  71.     {
  72.     printf("This programme requires at least V 2.x of CP/M.\n");
  73.         printf("Sorry but it won't run for you.\n");
  74.         exit();
  75.     }
  76. /*-----------------------------------------------------------------*/
  77. /*  Get the starting drive in case this is changed later.          */
  78. /*-----------------------------------------------------------------*/
  79.     OLD_DRIVE = get_default() + 0x41;
  80. /*-----------------------------------------------------------------*/
  81. /*  The CP/M Version is OK, so save the starting User No. in case
  82.     this is changed later in the programme.                        */
  83. /*-----------------------------------------------------------------*/
  84.  
  85.     OLD_USER = user_id(0xff);
  86.  
  87. /*-----------------------------------------------------------------*/
  88. /*  Now check the Command Line to see if a Drive Code was entered.
  89.     Other checks can also be used, as required but then it will be
  90.     necessary to change this coding.                               */
  91. /*-----------------------------------------------------------------*/
  92.     if(argc < 2)
  93.     {
  94.         printf("You have to enter the DRIVE you wish to");
  95.         printf(" check...A, B, C, D - \n");
  96.         DRIVE = toupper(getchar());
  97.     }
  98.     else if(argc > 2)
  99.     {
  100.         printf("Too many arguments entered.   We only need the");
  101.         printf(" DRIVE you wish to check.\n");
  102.         printf("Please enter the DRIVE...A, B, C, D - \n");
  103.         DRIVE = toupper(getchar());
  104.     }
  105.     else if(argc == 2)
  106.         DRIVE = toupper(argv[1][0]);
  107.     lines(2);
  108. /*-----------------------------------------------------------------*/
  109. /*  Check that the selected drive is available/on-line.   If not
  110.     then terminate the programme.   You may need to add a message
  111.     about what is going on if your version of CP/M doesn't do
  112.     this automatically, as mine does.                              */
  113. /*-----------------------------------------------------------------*/
  114.     if(!(skew_table = seldsk(DRIVE)))
  115.         exit();        /* Must be something wrong         */
  116.     if(select_dsk(DRIVE) != 0)
  117.         exit();
  118. /*-----------------------------------------------------------------*/
  119. /*  Calculate the values we need to scan the directory.            */
  120. /*-----------------------------------------------------------------*/
  121.  
  122.     THIS      = dpb_adr();
  123.     DIRECTORY = THIS->OFF;
  124.     SECTORS   = THIS->SPT;
  125.  
  126.     set_dma(&my_dma[0]);    /* Create DMA buffer               */
  127.     set_trk(DIRECTORY);
  128.  
  129. /*-----------------------------------------------------------------*/
  130. /*  Read the directory track and count the number of active
  131.     entries.   This information is required for the size of
  132.     the big buffer.                                                */
  133. /*-----------------------------------------------------------------*/
  134.     printf("Counting the active directory entries.\n");
  135.     ENTRIES = 0;        /* Starting conditions             */
  136.     for(i = 0; i < SECTORS; i++)
  137.     {
  138.         PHYS_SEC = biosh(16, i, *skew_table);
  139.         set_sec(PHYS_SEC);
  140.         setmem(&my_dma[0], 128, 0xe5);
  141.         read_sec();    /* Put information into DMA buffer */
  142.  
  143.     for(j = 0; j < 4; j++)
  144.     {
  145.         if(my_dma[j*32] != 0xe5)
  146.             ENTRIES++;
  147.     }
  148.     }
  149.     printf("Active entries (all extents) = %d\n", ENTRIES);
  150. /*-----------------------------------------------------------------*/
  151. /*  Now secure enough storage to hold all the active entries.      */
  152. /*-----------------------------------------------------------------*/
  153.     STORAGE = (((ENTRIES/4)*4) + 4)*32;
  154.     BIG_BUF = alloc(STORAGE);
  155.     setmem(BIG_BUF, STORAGE, 0xe5);    /* Clear the buffer        */
  156. /*-----------------------------------------------------------------*/
  157. /*  Then transfer the active entries to the big buffer.            */
  158. /*-----------------------------------------------------------------*/
  159.     set_trk(DIRECTORY);
  160.     j = 0;
  161.     for(i = 0; i < SECTORS; i++)
  162.     {
  163.         PHYS_SEC = biosh(16, i, *skew_table);
  164.         set_sec(PHYS_SEC);
  165.         setmem(&my_dma[0], 128, 0xe5);
  166.         read_sec();    /* Put information into DMA buffer */
  167.  
  168.         for(k = 0; k < 4; k++)
  169.         {
  170.             if(my_dma[k*32] != 0xe5)
  171.             {
  172.                 movmem(&my_dma[k*32], &BIG_BUF[j], 32);
  173.                 j = j + 32;
  174.             }
  175.         }
  176.     }
  177. /*-----------------------------------------------------------------*/
  178. /*  All the active entries are now in the big buffer so we can
  179.     erase the existing directory.   After this we write the active
  180.     entries back to the directory track in (almost) alphabetical
  181.     order.   This completes the programme.                         */
  182. /*-----------------------------------------------------------------*/
  183.     printf("Scrubbing old directory.\n");
  184.     for(i = 0; i < 1000; i++)
  185.         ;        /* Empty loop                      */
  186.     setmem(&my_dma[0], 128, 0xe5);
  187.     set_trk(DIRECTORY);
  188.     for(i = 0; i < SECTORS; i++)
  189.     {
  190.         set_sec(i);
  191.         write_sec();
  192.     }
  193.     printf("Writing re-formatted directory.\n");
  194.     set_trk(DIRECTORY);
  195.     k = 0;        /* Count of active files in DMA buffer     */
  196.     l = 0;        /* The directory sector to write           */
  197.  
  198.     for(c = 0x41; c < 0x60; c++)
  199.     {
  200.     for(i = 0; i < STORAGE; i = i + 32)
  201.     {
  202.     if(BIG_BUF[i + 1] == c)
  203.     {
  204.         movmem(&BIG_BUF[i], &my_dma[k*32], 32);
  205.         k++;
  206.         if(k == 4)
  207.         {
  208.             PHYS_SEC = biosh(16, l, *skew_table);
  209.             set_sec(PHYS_SEC);
  210.             write_sec();    /* Write to directory      */
  211.             l++;    /* Advance sector count            */
  212.             k = 0;
  213.             setmem(&my_dma[0], 128, 0xe5);
  214.         }
  215.     }
  216.     }
  217.     }
  218.     if(k > 0 && k < 4)
  219.     {
  220.         PHYS_SEC = biosh(16, l, *skew_table);
  221.         set_sec(PHYS_SEC);
  222.         write_sec();
  223.     }
  224.  
  225.     printf("Directory now re-written.");
  226.  
  227. /*-----------------------------------------------------------------*/
  228. /*  Before closing, make sure the User No. returns to original, and
  229.     the original drive is restored.                                */
  230. /*-----------------------------------------------------------------*/
  231.  
  232.     user_id(OLD_USER);
  233.     if(select_dsk(OLD_DRIVE) != 0)
  234.         exit();
  235.  
  236.     if((FD = open("UTIL.COM", 0)) != -1)
  237.     {
  238.         close(FD);
  239.         exec("UTIL");
  240.     }
  241.  
  242. }
  243. /*-----------------------------------------------------------------*/to DMA buffer */
  244.  
  245.         for(k = 0; k < 4; k++)
  246.         {
  247.             if(my_dma[k*32] != 0xe5)
  248.             {
  249.