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

  1. /*
  2.  * this file contains the various routines to manipulate the 
  3.  * all the temporary files generated during the sort
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "sort_thing.h"
  8. #include "cmd.h"
  9.  
  10. #define MAX_TEMPS 17576
  11.  
  12. static char filename[100];
  13. static char *tail;
  14.  
  15. extern char tmp_dir[];
  16.  
  17. int num_temps = 0;
  18.  
  19. FILE *outfile_desc = NULL;
  20. FILE *infile_desc  = NULL;
  21.  
  22. char *temp_dirs[] = {
  23.     tmp_dir,
  24.     "/usr/tmp",
  25.     "/tmp",
  26.     (char *) 0
  27. };
  28.  
  29.     /*
  30.      *    Check the various temporary directories listed in temp_dirs
  31.      *    and find one that we can write in
  32.      */
  33. check_tempspace()
  34. {
  35.     int i;
  36.     int status;
  37.  
  38.     i = 0;
  39.  
  40.     do {
  41.     sprintf(filename,"%s/xsrt%05dXXX",temp_dirs[i],getpid());
  42.     status = creat(filename,0);
  43.     i++;
  44.     } while ( temp_dirs[i] != (char *) 0 && status == -1);
  45.  
  46.     if (status == -1) {
  47.     panic("Cannot open any temp file space");
  48.     }
  49.  
  50.     /* set tail to point toward the last three chars in filename */
  51.     
  52.     for (tail = filename; *tail != '\0'; tail++)
  53.     ;
  54.  
  55.     tail -= 3;
  56.  
  57.     unlink(filename);
  58. }
  59.  
  60.     /* create the file name associated with index 'index' */
  61.  
  62. static set_filename(index)
  63. int index;
  64. {
  65.     if (index > MAX_TEMPS) {
  66.     panic("Too many temp files");
  67.     }
  68.  
  69.     index--;
  70.  
  71.     tail[0] =  index/676 + 'a';
  72.     tail[1] =  (index%676)/26 + 'a';
  73.     tail[2] =  index%26 + 'a';
  74. }
  75.  
  76. close_outfile()
  77. {
  78.     int status;
  79.  
  80.     if ( fclose(outfile_desc) == EOF) {
  81.     panic("can't close temp file");;
  82.     }
  83. }
  84.  
  85. remove_file(index)
  86. int index;
  87. {
  88.         /* delete the file associated with index 'index' */
  89.     set_filename(index);
  90.     unlink(filename);
  91. }
  92.  
  93.     /* open the next sequential file */
  94.  
  95. new_outfile()
  96. {
  97.     set_filename(++num_temps);
  98.  
  99.     if ( (outfile_desc = fopen(filename,"w")) == NULL) {
  100.     panic("Cannot open tempfile");
  101.     }
  102. }
  103.  
  104. set_STDOUT()
  105. {
  106.     outfile_desc = stdout;
  107. }
  108.  
  109.     /* reopen the file associated with index 'index' */
  110.  
  111. FILE *open_file(index)
  112. {
  113.     FILE *desc;
  114.  
  115.     set_filename(index);
  116.  
  117.     if ( (desc = fopen(filename,"r")) == NULL) {
  118.     panic("cannot reopen tempfile");
  119.     }
  120.  
  121.     return(desc);
  122. }
  123.     
  124.     /* delete all of the temporary files */
  125.  
  126. clean_up_files()
  127. {
  128.     int index;
  129.  
  130.     for(index = 1; index <= num_temps && index <= MAX_TEMPS; index++) {
  131.     set_filename(index);
  132.     unlink(filename);
  133.     }
  134. }
  135.  
  136.     /* copy the block from start_ptr to end_ptr to the output file */
  137.  
  138. output_run(start_ptr,end_ptr)
  139. thing *start_ptr,*end_ptr;
  140. {
  141.     register thing *ptr;
  142.  
  143.     for (ptr = start_ptr; ptr < end_ptr; ptr++) {
  144.     if (fwrite(ptr,sizeof(thing),1,outfile_desc) == 0 ) {
  145.         panic("bad write onto temp/output file");
  146.     }
  147.     }
  148. }
  149.