home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / ciftomann.tar / sort_dir / sort.c < prev    next >
C/C++ Source or Header  |  1988-01-28  |  3KB  |  182 lines

  1. #include "sort.h"
  2. #include "sort_thing.h"
  3. #include "fd.h"
  4. #include <signal.h>
  5. #include <stdio.h>
  6.  
  7. typedef enum { FALSE = 0, TRUE } boolean;
  8.  
  9. char tmp_dir[101];
  10.  
  11. extern int num_temps;
  12.  
  13. int comparer();
  14. int terminate();
  15. char *sbrk();
  16. thing *start_mem,*end_mem;
  17.  
  18.     /*
  19.      *    This is a re-write of the unix sort command, with
  20.      *    the character oriented sorting changed to binary
  21.      *    format using a user supplied comparison function
  22.      *      The routine reads in a buffer of items,
  23.      *  sorts thems and outputs them into a file. This is
  24.      *    continued until all input has been processed. The
  25.      *    seperate files are merged MAX_FILES at a time into
  26.      *    larger files until the entire input has been merged
  27.      *    into one.
  28.      */
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char **argv;
  33. {
  34.     int first_file,last_file;
  35.  
  36.  
  37.     if (argc > 1) {
  38.     strcpy(tmp_dir,argv[1]);
  39.     } else {
  40.     strcpy(tmp_dir, "/usr/tmp");
  41.     }
  42.  
  43.     allocate();
  44.     check_tempspace();
  45.     catch_signals();
  46.  
  47.     /* make the individual files */
  48.     make_runs();
  49.  
  50.     first_file = 1;
  51.     last_file = MAX_FILES;
  52.  
  53.     /* merge the files together */
  54.  
  55.     while (last_file < num_temps) {
  56.  
  57.     new_outfile();
  58.     merge(first_file,last_file);
  59.     close_outfile();
  60.  
  61.     first_file = last_file + 1;
  62.     last_file = first_file + MAX_FILES - 1;
  63.     }
  64.     
  65.     set_STDOUT();
  66.     merge(first_file,num_temps);
  67.     exit(0);
  68. }
  69.  
  70. allocate()
  71.  
  72. /* Allocate the memory for the sorting of the individual runs.
  73.  * Sets the value of start_mem, a pointer to the start of the
  74.  * block, and end_mem, a pointer to the end of the block.
  75.  */
  76.  
  77. {
  78.     int size;
  79.     int num_things;
  80.  
  81.     size = MAX_MEM;
  82.  
  83.     /* get the largest chunk of memory you can */
  84.  
  85.     while( (start_mem = (thing *)sbrk(size)) == (thing *)(-1) &&
  86.        size > 0) {
  87.     size -= MEM_INC;
  88.     }
  89.  
  90.     if (size < 0) {
  91.     panic("No memory at all!");
  92.     exit(1);
  93.     }
  94.  
  95.     num_things = size / sizeof(thing);
  96.  
  97.     end_mem = &( start_mem[num_things] );
  98. }
  99.  
  100. make_runs()
  101.  
  102.     /* Input and sort the individual runs, outputing the
  103.      * results directly if only one run is necessary
  104.      */
  105.  
  106. {
  107.     thing *current;
  108.     int status;
  109.     boolean eof = FALSE;
  110.     int num_elements;
  111.  
  112.     do {
  113.  
  114.     current = start_mem;
  115.  
  116.     while( current < end_mem && eof == FALSE ) {
  117.         
  118.         status = fread((char *) current, sizeof(thing), 1, stdin);
  119.  
  120.         if (status == 0) {
  121.         eof = TRUE;
  122.         } else {
  123.         current++;
  124.         }
  125.         
  126.     }
  127.  
  128.     num_elements = (current - start_mem);
  129.  
  130.     qsort((char *)start_mem,num_elements,sizeof(thing),comparer);
  131.  
  132.     if ( eof && num_temps == 0) {
  133.  
  134.         /* only one run was necessary */
  135.  
  136.         set_STDOUT();
  137.     } else {
  138.         new_outfile();
  139.     }
  140.  
  141.     output_run(start_mem,current);
  142.  
  143.     close_outfile();
  144.  
  145.     } while ( !eof );
  146.  
  147. }
  148.  
  149. catch_signals()
  150. {
  151.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
  152.     signal(SIGHUP, terminate);
  153.     }
  154.  
  155.     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
  156.     signal(SIGINT, terminate);
  157.     }
  158.  
  159.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
  160.     signal(SIGTERM, terminate);
  161.     }
  162. }
  163.  
  164. panic(str)
  165. char *str;
  166. {
  167.  
  168.     extern int errno;
  169.     extern char *sys_errlist[];
  170.  
  171.     fprintf(stderr,"Panic in sort : %s (%s)\n",str,sys_errlist[errno]);
  172.     terminate();
  173. }
  174.  
  175. terminate()
  176. {
  177.     fprintf(stderr,"\nremoving tempfiles, just a moment...");
  178.     clean_up_files();
  179.     fprintf(stderr,"done!\n");
  180.     exit(1);
  181. }
  182.