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 >
Wrap
C/C++ Source or Header
|
1988-01-28
|
3KB
|
149 lines
/*
* this file contains the various routines to manipulate the
* all the temporary files generated during the sort
*/
#include <stdio.h>
#include "sort_thing.h"
#include "cmd.h"
#define MAX_TEMPS 17576
static char filename[100];
static char *tail;
extern char tmp_dir[];
int num_temps = 0;
FILE *outfile_desc = NULL;
FILE *infile_desc = NULL;
char *temp_dirs[] = {
tmp_dir,
"/usr/tmp",
"/tmp",
(char *) 0
};
/*
* Check the various temporary directories listed in temp_dirs
* and find one that we can write in
*/
check_tempspace()
{
int i;
int status;
i = 0;
do {
sprintf(filename,"%s/xsrt%05dXXX",temp_dirs[i],getpid());
status = creat(filename,0);
i++;
} while ( temp_dirs[i] != (char *) 0 && status == -1);
if (status == -1) {
panic("Cannot open any temp file space");
}
/* set tail to point toward the last three chars in filename */
for (tail = filename; *tail != '\0'; tail++)
;
tail -= 3;
unlink(filename);
}
/* create the file name associated with index 'index' */
static set_filename(index)
int index;
{
if (index > MAX_TEMPS) {
panic("Too many temp files");
}
index--;
tail[0] = index/676 + 'a';
tail[1] = (index%676)/26 + 'a';
tail[2] = index%26 + 'a';
}
close_outfile()
{
int status;
if ( fclose(outfile_desc) == EOF) {
panic("can't close temp file");;
}
}
remove_file(index)
int index;
{
/* delete the file associated with index 'index' */
set_filename(index);
unlink(filename);
}
/* open the next sequential file */
new_outfile()
{
set_filename(++num_temps);
if ( (outfile_desc = fopen(filename,"w")) == NULL) {
panic("Cannot open tempfile");
}
}
set_STDOUT()
{
outfile_desc = stdout;
}
/* reopen the file associated with index 'index' */
FILE *open_file(index)
{
FILE *desc;
set_filename(index);
if ( (desc = fopen(filename,"r")) == NULL) {
panic("cannot reopen tempfile");
}
return(desc);
}
/* delete all of the temporary files */
clean_up_files()
{
int index;
for(index = 1; index <= num_temps && index <= MAX_TEMPS; index++) {
set_filename(index);
unlink(filename);
}
}
/* copy the block from start_ptr to end_ptr to the output file */
output_run(start_ptr,end_ptr)
thing *start_ptr,*end_ptr;
{
register thing *ptr;
for (ptr = start_ptr; ptr < end_ptr; ptr++) {
if (fwrite(ptr,sizeof(thing),1,outfile_desc) == 0 ) {
panic("bad write onto temp/output file");
}
}
}