home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / dirutl / dups.arc / DUPS.C < prev    next >
C/C++ Source or Header  |  1987-05-15  |  24KB  |  536 lines

  1. /*
  2.  
  3.           The Microsoft C 4.0 program lists the duplicate file names on 
  4.      a drive sorted by file name, date, time, size, and path.
  5.  
  6.           Written by James L. Dean
  7.                      406 40th Street
  8.                      New Orleans, LA 70124
  9.                      May 15, 1987       
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <malloc.h>
  18. #include <dos.h>
  19.  
  20. #define FALSE 0
  21. #define TRUE 1
  22.  
  23. typedef struct dir_node
  24.                  {
  25.                    char            file_name [14];
  26.                    char            attributes;
  27.                    unsigned int    time_stamp;
  28.                    unsigned int    date_stamp;
  29.                    unsigned long   file_size;
  30.                    struct dir_node *next;
  31.                  } *dir_node_ptr;
  32.  
  33. typedef struct path_node
  34.                  {
  35.                    char             *path_name;
  36.                    unsigned int     time_stamp;
  37.                    unsigned int     date_stamp;
  38.                    unsigned long    file_size;
  39.                    struct path_node *next;
  40.                    struct path_node *previous;
  41.                  } *path_node_ptr;
  42.  
  43. typedef struct file_node
  44.                  {
  45.                    char             *file_name;
  46.                    struct path_node *path_head;
  47.                    struct file_node *predecessor;
  48.                    struct file_node *lesser;
  49.                    struct file_node *greater;
  50.                  } *file_node_ptr;
  51.  
  52. static void build_file_tree(char *,file_node_ptr *);
  53. char far    *far_ptr(unsigned int,unsigned int);
  54. static void get_dir(char *,dir_node_ptr *);
  55. static void report_dups(file_node_ptr *);
  56.  
  57. struct dta {
  58.              char          reserved [21];
  59.              unsigned char attributes;
  60.              unsigned int  time_stamp;
  61.              unsigned int  date_stamp;
  62.              unsigned long file_size;
  63.              char          file_name [13];
  64.            }  *dta_ptr;
  65.  
  66. int           fatal_error;
  67.  
  68. void main(argc,argv)
  69.  int  argc;
  70.  char *argv[];
  71.   {
  72.     static file_node_ptr file_head;
  73.     static union REGS    inreg;
  74.     static union REGS    outreg;
  75.  
  76.     inreg.h.ah=(unsigned char) 98;
  77.     intdos(&inreg,&outreg);
  78.     dta_ptr=(struct dta *) far_ptr(outreg.x.bx,128);
  79.     fatal_error=FALSE;
  80.     file_head=NULL;
  81.     if (argc == 1)
  82.       {
  83.         build_file_tree("",&file_head);
  84.         if (! fatal_error)
  85.           report_dups(&file_head);
  86.       }
  87.     else
  88.       if ((strlen(argv[1]) == 2) 
  89.       &&  (argv[1][1] == ':')
  90.       &&  (isalpha((int) argv[1][0])))
  91.         {
  92.           build_file_tree(argv[1],&file_head);
  93.           if (! fatal_error)
  94.             report_dups(&file_head);
  95.         }
  96.       else
  97.         {
  98.           printf("Finds duplicated file names on a drive.\n\n");
  99.           printf("Usage:  DUPS [drive]\n");
  100.           printf("Example:  DUPS C:\n");
  101.         }
  102.     return;
  103.   }
  104.  
  105. static void build_file_tree(path_name,file_head)
  106.   char          *path_name;
  107.   file_node_ptr *file_head;
  108.     {
  109.       dir_node_ptr  dir_head;
  110.       char          dir_name[256];
  111.       dir_node_ptr  dir_ptr;
  112.       file_node_ptr file_ptr_1;
  113.       file_node_ptr file_ptr_2;
  114.       int           finished;
  115.       char          new_path_name[256];
  116.       path_node_ptr path_ptr_1;
  117.       path_node_ptr path_ptr_2;
  118.       path_node_ptr path_ptr_3;
  119.       int           relation;
  120.       
  121.       strcpy(dir_name,path_name);
  122.       strcat(dir_name,"\\*.*"); 
  123.       dir_head=NULL;
  124.       get_dir(dir_name,&dir_head);
  125.       if (! fatal_error)
  126.         {
  127.           while (dir_head != NULL)
  128.             {
  129.               if (dir_head->file_name[0] != '.')
  130.                 {
  131.                   if ((dir_head->attributes) & 16)
  132.                     {
  133.                       strcpy(new_path_name,path_name);
  134.                       strcat(new_path_name,"\\");
  135.                       strcat(new_path_name,dir_head->file_name);
  136.                       build_file_tree(new_path_name,file_head);
  137.                     }
  138.                   else
  139.                     {
  140.                       if ((path_ptr_1=(struct path_node *) malloc(
  141.                        (unsigned) sizeof(struct path_node))) == NULL)
  142.                         {
  143.                           fatal_error=TRUE;
  144.                           printf("Fatal error:  out of memory.\n");
  145.                         }
  146.                       else
  147.                         {
  148.                           path_ptr_1->next=NULL;
  149.                           path_ptr_1->previous=NULL;
  150.                           if ((path_ptr_1->path_name=malloc((unsigned)
  151.                            (1+strlen(path_name)))) == NULL)
  152.                             {
  153.                               fatal_error=TRUE;
  154.                               printf("Fatal error:  out of memory.\n");
  155.                             }
  156.                           else
  157.                             {
  158.                               strcpy(path_ptr_1->path_name,path_name);  
  159.                               path_ptr_1->time_stamp=dir_head->time_stamp;
  160.                               path_ptr_1->date_stamp=dir_head->date_stamp;
  161.                               path_ptr_1->file_size=dir_head->file_size;
  162.                             }
  163.                         }
  164.                       if (! fatal_error)
  165.                         {
  166.                           if (*file_head == NULL)
  167.                             {
  168.                               if ((*file_head
  169.                                =(struct file_node *)
  170.                                malloc((unsigned) sizeof(struct file_node))) 
  171.                                == NULL)
  172.                                 {
  173.                                   fatal_error=TRUE;
  174.                                   printf("Fatal error:  out of memory.\n");
  175.                                 }
  176.                               else    
  177.                                 {
  178.                                   (*file_head)->predecessor=NULL;
  179.                                   (*file_head)->lesser=NULL;
  180.                                   (*file_head)->greater=NULL;
  181.                                   (*file_head)->path_head=path_ptr_1;
  182.                                   if (((*file_head)->file_name=malloc(
  183.                                    (unsigned) (1+strlen(dir_head->file_name))))
  184.                                    == NULL)
  185.                                     {
  186.                                       fatal_error=TRUE;
  187.                                       printf("Fatal error:  out of memory.\n");
  188.                                     }
  189.                                   else
  190.                                     strcpy((*file_head)->file_name,
  191.                                      dir_head->file_name);
  192.                                 }
  193.                             }
  194.                           else
  195.                             {
  196.                               finished=FALSE;
  197.                               file_ptr_1=*file_head;
  198.                               while (! finished)
  199.                                 {
  200.                                   relation=strcmp(dir_head->file_name,
  201.                                    file_ptr_1->file_name);
  202.                                   if (relation < 0)
  203.                                     if (file_ptr_1->lesser == NULL)
  204.                                       {
  205.                                         if ((file_ptr_2
  206.                                          =(struct file_node *)
  207.                                          malloc((unsigned)
  208.                                          sizeof(struct file_node))) 
  209.                                          == NULL)
  210.                                           {
  211.                                             fatal_error=TRUE;
  212.                                             printf(
  213.                                              "Fatal error:  out of memory.\n");
  214.                                           }
  215.                                         else    
  216.                                           {
  217.                                             file_ptr_1->lesser=file_ptr_2;
  218.                                             file_ptr_2->predecessor
  219.                                              =file_ptr_1;
  220.                                             file_ptr_2->lesser=NULL;
  221.                                             file_ptr_2->greater=NULL;
  222.                                             file_ptr_2->path_head=path_ptr_1;
  223.                                             if ((file_ptr_2->file_name=malloc(
  224.                                              (unsigned)
  225.                                              (1+strlen(dir_head->file_name))))
  226.                                              == NULL)
  227.                                               {
  228.                                                 fatal_error=TRUE;
  229.                                                 printf(
  230.                                              "Fatal error:  out of memory.\n");
  231.                                               }
  232.                                             else
  233.                                               strcpy(file_ptr_2->file_name,
  234.                                                dir_head->file_name);
  235.                                           }
  236.                                         finished=TRUE;
  237.                                       }
  238.                                     else
  239.                                       file_ptr_1=file_ptr_1->lesser;
  240.                                   else
  241.                                     if (relation > 0) 
  242.                                       if (file_ptr_1->greater == NULL)
  243.                                         {
  244.                                           if ((file_ptr_2
  245.                                            =(struct file_node *)
  246.                                            malloc((unsigned)
  247.                                            sizeof(struct file_node))) 
  248.                                            == NULL)
  249.                                             {
  250.                                               fatal_error=TRUE;
  251.                                               printf(
  252.                                              "Fatal error:  out of memory.\n");
  253.                                             }
  254.                                           else    
  255.                                             {
  256.                                               file_ptr_1->greater=file_ptr_2;
  257.                                               file_ptr_2->predecessor
  258.                                                =file_ptr_1;
  259.                                               file_ptr_2->lesser=NULL;
  260.                                               file_ptr_2->greater=NULL;
  261.                                               file_ptr_2->path_head=path_ptr_1;
  262.                                               if ((file_ptr_2->file_name
  263.                                                =malloc((unsigned) (1+strlen(
  264.                                                dir_head->file_name)))) == NULL)
  265.                                                 {
  266.                                                   fatal_error=TRUE;
  267.                                                   printf(
  268.                                              "Fatal error:  out of memory.\n");
  269.                                                 }
  270.                                               else
  271.                                                 strcpy(file_ptr_2->file_name,
  272.                                                  dir_head->file_name);
  273.                                             }
  274.                                           finished=TRUE;
  275.                                         }
  276.                                       else
  277.                                         file_ptr_1=file_ptr_1->greater;
  278.                                     else
  279.                                       {
  280.                                         path_ptr_2=file_ptr_1->path_head;
  281.                                         path_ptr_3=NULL;
  282.                                         while ((path_ptr_2 != NULL)
  283.                                         &&     ((path_ptr_1->date_stamp
  284.                                                   > path_ptr_2->date_stamp)
  285.                                              || ((path_ptr_1->date_stamp
  286.                                                    == path_ptr_2->date_stamp)
  287.                                               && (path_ptr_1->time_stamp
  288.                                                    > path_ptr_2->time_stamp))
  289.                                              || ((path_ptr_1->date_stamp
  290.                                                    == path_ptr_2->date_stamp)
  291.                                               && (path_ptr_1->time_stamp
  292.                                                    == path_ptr_2->time_stamp)
  293.                                               && (path_ptr_1->file_size
  294.                                                    > path_ptr_2->file_size))
  295.                                              || ((path_ptr_1->date_stamp
  296.                                                    == path_ptr_2->date_stamp)
  297.                                               && (path_ptr_1->time_stamp
  298.                                                    == path_ptr_2->time_stamp)
  299.                                               && (path_ptr_1->file_size
  300.                                                    == path_ptr_2->file_size)
  301.                                               && (strcmp(path_ptr_1->path_name,
  302.                                                    path_ptr_2->path_name) 
  303.                                                    > 0))))
  304.                                            {
  305.                                              path_ptr_3=path_ptr_2;
  306.                                              path_ptr_2=path_ptr_2->next;
  307.                                            }
  308.                                         if (path_ptr_2 == NULL)
  309.                                           path_ptr_3->next=path_ptr_1;
  310.                                         else
  311.                                           {
  312.                                             if (path_ptr_3 == NULL)
  313.                                               file_ptr_1->path_head=path_ptr_1;
  314.                                             else
  315.                                               path_ptr_3->next=path_ptr_1;
  316.                                             path_ptr_2->previous=path_ptr_1;
  317.                                           }
  318.                                         path_ptr_1->next=path_ptr_2;
  319.                                         path_ptr_1->previous=path_ptr_3;
  320.                                         finished=TRUE;
  321.                                       }
  322.                                 } 
  323.                             }
  324.                         }
  325.                     }
  326.                 }
  327.               dir_ptr=dir_head;
  328.               dir_head=dir_head->next;
  329.               free((char *) dir_ptr);
  330.             }
  331.         }
  332.     }
  333.  
  334. static void get_dir(dir_name,dir_head)
  335.   char far     *dir_name;
  336.   dir_node_ptr *dir_head;
  337.     {
  338.       static union REGS    inreg;
  339.       static dir_node_ptr  new_dir_entry;
  340.       static union REGS    outreg;
  341.       static struct SREGS  segreg;
  342.  
  343.       inreg.h.ah=(unsigned char) 78;
  344.       segreg.ds=FP_SEG(dir_name);
  345.       inreg.x.dx=FP_OFF(dir_name);
  346.       inreg.x.cx=(unsigned int) 0x3f;
  347.       intdosx(&inreg,&outreg,&segreg);
  348.       if ((outreg.x.ax != (unsigned int) 2)
  349.       &&  (outreg.x.ax != (unsigned int) 18))
  350.         {
  351.           while ((outreg.x.ax != (unsigned int) 18)
  352.           &&     (! fatal_error))
  353.             {
  354.               if (*dir_head == NULL)
  355.                 {
  356.                   if ((*dir_head
  357.                    =(struct dir_node *)
  358.                    malloc((unsigned) sizeof(struct dir_node))) == NULL)
  359.                     {
  360.                       fatal_error=TRUE;
  361.                       printf("Fatal error:  out of memory.\n");
  362.                     }
  363.                   else    
  364.                     {
  365.                       (*dir_head)->next=NULL;
  366.                       strcpy((*dir_head)->file_name,dta_ptr->file_name);
  367.                       (*dir_head)->attributes=dta_ptr->attributes;
  368.                       (*dir_head)->time_stamp=dta_ptr->time_stamp;
  369.                       (*dir_head)->date_stamp=dta_ptr->date_stamp;
  370.                       (*dir_head)->file_size=dta_ptr->file_size;
  371.                     }
  372.                 }
  373.               else
  374.                 {
  375.                   if ((new_dir_entry=(struct dir_node *)
  376.                    malloc((unsigned) sizeof(struct dir_node))) == NULL)
  377.                     {
  378.                       fatal_error=TRUE;
  379.                       printf("Fatal error:  out of memory.\n");
  380.                     }
  381.                   else
  382.                     {
  383.                       strcpy(new_dir_entry->file_name,dta_ptr->file_name);
  384.                       new_dir_entry->attributes=dta_ptr->attributes;
  385.                       new_dir_entry->time_stamp=dta_ptr->time_stamp;
  386.                       new_dir_entry->date_stamp=dta_ptr->date_stamp;
  387.                       new_dir_entry->file_size=dta_ptr->file_size;
  388.                       new_dir_entry->next=(*dir_head);
  389.                       (*dir_head)=new_dir_entry;
  390.                     }
  391.                 }
  392.               inreg.h.ah=(unsigned char) 79;
  393.               intdosx(&inreg,&outreg,&segreg);
  394.             }
  395.         }
  396.       return;
  397.     }
  398.  
  399. static void report_dups(file_head)
  400.   file_node_ptr *file_head;
  401.     {
  402.       static   char           am_or_pm [2];
  403.       static   int            bypass_smaller;
  404.       static   int            char_index;
  405.       static   char           *char_ptr;
  406.       static   file_node_ptr  current_file_ptr;
  407.       static   char           day [3];
  408.       static   unsigned int   day_num;
  409.       static   char           file_name [14];
  410.       static   int            finished;
  411.       static   int            hit;
  412.       static   char           hour [3];
  413.       static   unsigned int   hour_num;
  414.       static   int            larger_predecessor_found;
  415.       static   char           minute [3];
  416.       static   unsigned int   minute_num;
  417.       static   char           month [3];
  418.       register unsigned int   month_num;
  419.       static   path_node_ptr  path_ptr;
  420.       static   file_node_ptr  previous_file_ptr;
  421.       static   char           second [3];
  422.       static   unsigned int   second_num;
  423.       static   char           year [3];
  424.       register unsigned int   year_num;
  425.  
  426.       if (*file_head == NULL)
  427.         printf("There are no files!\n");
  428.       else
  429.         {      
  430.           hit=FALSE;
  431.           current_file_ptr=*file_head;
  432.           finished=FALSE;
  433.           bypass_smaller=FALSE;
  434.           do
  435.             {
  436.               if (! bypass_smaller)
  437.                 while (current_file_ptr->lesser != NULL)
  438.                   current_file_ptr=current_file_ptr->lesser;
  439.               if (current_file_ptr->path_head->next != NULL)
  440.                 {
  441.                   hit=TRUE;
  442.                   char_index=0;
  443.                   char_ptr=current_file_ptr->file_name;
  444.                   while (file_name[char_index]=*char_ptr)
  445.                     {
  446.                       char_ptr++;
  447.                       char_index++;
  448.                     }
  449.                   while (char_index < 13) file_name[char_index++]=' ';
  450.                   file_name[char_index]='\0';
  451.                   path_ptr=current_file_ptr->path_head;
  452.                   while (path_ptr != NULL)
  453.                     {
  454.                       printf("%s",file_name);
  455.                       year_num=path_ptr->date_stamp/(unsigned int) 512;
  456.                       path_ptr->date_stamp-=((unsigned int) 512*year_num);
  457.                       year_num+=(unsigned int) 80;
  458.                       if (year_num > 100) year_num-=(unsigned int) 100;
  459.                       month_num=path_ptr->date_stamp/(unsigned int) 32;
  460.                       day_num=path_ptr->date_stamp-(unsigned int) 32*month_num;
  461.                       sprintf(year,"%2d",year_num);
  462.                       if (year[0] == ' ') year[0]='0';
  463.                       sprintf(month,"%2d",month_num);
  464.                       sprintf(day,"%2d",day_num);
  465.                       if (day[0] == ' ') day[0]='0';
  466.                       hour_num=path_ptr->time_stamp/(unsigned int) 2048;
  467.                       path_ptr->time_stamp-=((unsigned int) 2048*hour_num);
  468.                       minute_num=path_ptr->time_stamp/(unsigned int) 32;
  469.                       path_ptr->time_stamp-=((unsigned int) 32*minute_num);
  470.                       second_num=2*path_ptr->time_stamp;
  471.                       if (hour_num >= 12)
  472.                         am_or_pm[0]='p';
  473.                       else
  474.                         am_or_pm[0]='a';
  475.                       am_or_pm[1]='\0';
  476.                       if (hour_num > 12) hour_num-=12;
  477.                       sprintf(hour,"%2d",hour_num);
  478.                       sprintf(minute,"%2d",minute_num);
  479.                       if (minute[0] == ' ') minute[0]='0';
  480.                       sprintf(second,"%2d",second_num);
  481.                       if (second[0] == ' ') second[0]='0';
  482.                       printf("%s/%s/%s %s:%s:%s%s %10ld ",
  483.                        month,day,year,hour,minute,second,am_or_pm,
  484.                        path_ptr->file_size);
  485.                       if (*(path_ptr->path_name) == '\0')
  486.                         printf("\\\n");
  487.                       else
  488.                         printf("%s\n",path_ptr->path_name);
  489.                       path_ptr=path_ptr->next;
  490.                     }
  491.                   printf("\n");
  492.                 }
  493.               while (current_file_ptr->path_head != NULL)
  494.                 {
  495.                   path_ptr=current_file_ptr->path_head;
  496.                   current_file_ptr->path_head=path_ptr->next;
  497.                   free(path_ptr->path_name);
  498.                   free((char *) path_ptr);
  499.                 }
  500.               if (current_file_ptr->greater != NULL)
  501.                 {
  502.                   current_file_ptr=current_file_ptr->greater;
  503.                   bypass_smaller=FALSE;
  504.                 }
  505.               else
  506.                 {
  507.                   larger_predecessor_found=FALSE;
  508.                   do
  509.                     {
  510.                       if (current_file_ptr->predecessor == NULL)
  511.                         finished=TRUE;
  512.                       else
  513.                         {
  514.                           previous_file_ptr=current_file_ptr;
  515.                           current_file_ptr=previous_file_ptr->predecessor;
  516.                           if (strcmp(current_file_ptr->file_name,
  517.                            previous_file_ptr->file_name) > 0)
  518.                             larger_predecessor_found=TRUE;
  519.                           free(previous_file_ptr->file_name);
  520.                           free((char *) previous_file_ptr);
  521.                         }
  522.                     }
  523.                   while ((! finished) && (! larger_predecessor_found));
  524.                   bypass_smaller=TRUE;
  525.                 }
  526.             }
  527.           while (! finished);
  528.           free((*file_head)->file_name);
  529.           free((char *) *file_head);
  530.           *file_head=NULL;
  531.           if (! hit)
  532.             printf("No duplicate file names were found.\n");
  533.         }
  534.       return;
  535.     }
  536.