home *** CD-ROM | disk | FTP | other *** search
/ Lion Share / lionsharecd.iso / utils_mz / v10n09.zip / 2DIR.C next >
Text File  |  1991-02-13  |  5KB  |  205 lines

  1. Code Box:
  2.  
  3. 2DIR.C
  4. COMPLETE LISTING
  5.  
  6.  
  7.  
  8. // 2DIR.C: Display 2 Directories.
  9.  
  10. #include<stdio.h>
  11. #include<dos.h>
  12. #include<stdlib.h>
  13. #include<string.h>
  14. #include<direct.h>
  15. #include<graph.h>
  16.  
  17. #define DriveLet(str)   (*str - 'A' + 1)
  18. #define DIRNAMELEN  67
  19. #define MAXFILES    1024
  20.  
  21. char *orgdir = NULL;
  22. char full_names[MAXFILES][13];
  23. long sizes[MAXFILES], lenfiles1 = 0L, lenfiles2 = 0L;
  24. unsigned dates[MAXFILES], times[MAXFILES];
  25. int numfiles1, numfiles2;
  26.  
  27. void terminate(void);
  28. void display_direntry(int x1);
  29. void read_files_info(int mx);
  30. void setdir(char *dirname);
  31. void process_dir(char *dirname);
  32.  
  33. void main(int argc, char *argv[])
  34.     {
  35.     int x1, x2;
  36.     char dirname1[DIRNAMELEN], dirname2[DIRNAMELEN];
  37.  
  38.     if(argc != 3)
  39.         {
  40.         puts("Usage: 2dir <dirname> <dirname>");
  41.         exit(0);
  42.         }
  43.  
  44.     strupr(argv[1]);
  45.     strupr(argv[2]);
  46.     orgdir = getcwd(NULL, DIRNAMELEN);  // save original directory
  47.     
  48.     // switch to first dir
  49.     process_dir(argv[1]);
  50.  
  51.     // switch to 2nd dir and do the same..
  52.     process_dir(argv[2]);
  53.  
  54.     // display the directories...
  55.     _clearscreen(_GCLEARSCREEN);
  56.     sprintf(dirname1,"Directory %s",argv[1]);
  57.     sprintf(dirname2,"Directory %s",argv[2]);
  58.     printf("%-40s%-40s",dirname1,dirname2);
  59.  
  60.     x1 = 0;
  61.     x2 = numfiles1;
  62.     if(x2 > numfiles2)
  63.         numfiles2 = x2;
  64.  
  65.     while(x1 < numfiles1 || x2 < numfiles2)
  66.         {
  67.         if(x1 < numfiles1)
  68.             {
  69.             display_direntry(x1);
  70.             lenfiles1 += sizes[x1];
  71.             x1++;
  72.             }
  73.         else
  74.             printf("\n%37s","");
  75.         if(x2 < numfiles2)
  76.             {
  77.             display_direntry(x2);
  78.             lenfiles2 += sizes[x2];
  79.             x2++;
  80.             }
  81.         }
  82.     if(numfiles1 > (numfiles2-numfiles1))
  83.         printf("\n");
  84.     if(x1 == numfiles1)
  85.         printf("%5u file(s) occupying %ld bytes",numfiles1,lenfiles1);
  86.     printf("        ");
  87.     if(x2 == numfiles2)
  88.         printf("%5u file(s) occupying %ld bytes",
  89.             numfiles2-numfiles1,lenfiles2);
  90.     terminate();
  91.     }
  92.  
  93. void process_dir(char *dirname)
  94.     {
  95.     unsigned drive, numdrives;
  96.     char *original;
  97.  
  98.     if(dirname[1] == ':')
  99.         {
  100.         drive = DriveLet(dirname);
  101.         _dos_setdrive(drive, &numdrives);
  102.         original = getcwd(NULL, DIRNAMELEN-1);
  103.         }
  104.  
  105.     if(chdir(dirname))
  106.         {
  107.         printf("\n        Directory %s Does Not Exist\n", dirname);
  108.         setdir(original);
  109.         free(original);
  110.         terminate();
  111.         }
  112.  
  113.     read_files_info(numfiles1);
  114.  
  115.     setdir(original);
  116.     setdir(orgdir);
  117.     }
  118.  
  119.  
  120. // displays each entry
  121. void display_direntry(int i)
  122.     {
  123.     char *exten, m;
  124.     unsigned xdate, xtime, hour;
  125.  
  126.     if(exten = strchr(full_names[i],'.'))
  127.         {
  128.         *exten = '\0';
  129.         exten++;
  130.         }
  131.     else
  132.         exten = "   ";
  133.     
  134.     xdate = dates[i];
  135.     xtime = times[i];
  136.     if( (hour = (xtime >> 11)) > 12)
  137.         {
  138.         hour -= 12;
  139.         m = 'p';
  140.         }
  141.     else
  142.         m = 'a';
  143.     if(hour == 0)
  144.         hour = 12;
  145.  
  146.     printf("%s%-8s %-3s %8ld %2u-% 02u-%02u %2u:%02u%c",
  147.         (i < numfiles1) ? "\n" : "     ",
  148.         full_names[i], exten, sizes[i],
  149.         (xdate >> 5) & 0x000f, (xdate & 0x001f), (xdate >> 9) + 80,
  150.         hour, (xtime >> 5) & 0x3f, m);
  151.     }
  152.     
  153. // resets all directories and terminates
  154. void terminate(void)
  155.     {
  156.     setdir(orgdir);
  157.     printf("\n");
  158.     exit(0);
  159.     }
  160.  
  161. // sets drive and directory
  162. void setdir(char *dirname)
  163.     {
  164.     unsigned numdrives, drive;
  165.  
  166.     if(dirname && dirname[1] == ':')
  167.         {
  168.         drive = DriveLet(dirname);
  169.         _dos_setdrive(drive, &numdrives);
  170.         chdir(dirname);
  171.         }
  172.     }
  173.  
  174. // reads each directory
  175. void read_files_info(int mx)
  176.     {
  177.     int i = mx, status;
  178.     struct find_t fileinfo;
  179.  
  180.     status = _dos_findfirst("*.*",
  181.                     (_A_NORMAL | _A_RDONLY | _A_ARCH), &fileinfo);
  182.     while(!status)
  183.         {
  184.         strcpy(full_names[i], fileinfo.name);
  185.         sizes[i] = fileinfo.size;
  186.         dates[i] = fileinfo.wr_date;
  187.         times[i] = fileinfo.wr_time;
  188.  
  189.         i++;
  190.         if(i >= MAXFILES)
  191.             {
  192.             puts("Too many directory entries for 2dir!\n");
  193.             terminate();
  194.             }
  195.  
  196.         status = _dos_findnext(&fileinfo);
  197.         }
  198.     if(mx == 0)
  199.         numfiles1 = i;
  200.     else
  201.         numfiles2 = i;
  202.     }
  203.  
  204.  
  205. Figure C: 2DIR displays two directories from any drive on the screen for easy comparison.