
/*
	file:		unix.lib.c
	purpose:	unix dependent utilities. 

	author: Robert Boyko
	date:	April 1, 1992	
	place:	University of Alberta / Department of Biochemistry
*/


/* these next two definitions are set from the install script */
#define DISPLAY  		"more"
#define EDITOR "vi"


#define MAX_LINE			256

#include <stdio.h> 
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void hold_screen();

/*************************************************************/
/* execute program. 
*/
int prog_execute(program)
char *program;
{	
	int rc;
	char exec_string[MAX_LINE];

	strcpy(exec_string, " ");
	strcat(exec_string, program);

	rc = system(exec_string);

	return(rc);

}	/* prog_execute */


/*************************************************************/
/* return today's date. 
*/
char *get_date()
{	
	time_t	tloc;

	time(&tloc);
	return((char *)ctime(&tloc));

}	/* get_date */


/*************************************************************/
/* return temporary filename by reading the clock. 
*/
void get_temp_file(fname)
char *fname;
{
	static int counter;

	sprintf(fname, "vadar.%d.%d", counter++, getpid());

}	/* get_temp_file */


/*************************************************************/
/* edit a file in the present window. 
*/
void edit_file(fname)
char *fname;
{	
	char sVal[MAX_LINE];

	sprintf(sVal, "%s %s", EDITOR, fname);
	system(sVal);

}	/* edit_file */


/*************************************************************/
/* destroy a file without confirmation.
*/
void remove_file(fname)
char *fname;
{	
	char sVal[MAX_LINE];

	sprintf(sVal, "rm -f %s", fname);
	system(sVal);

}	/* remove_file */


/*************************************************************/
/* rename a file. 
*/
void rename_file(oldFname, newFname)
char *oldFname, *newFname;
{	
	char sVal[MAX_LINE];

	sprintf(sVal, "mv -f %s %s", oldFname, newFname);
	system(sVal);

}	/* rename_file */


/*************************************************************/
/* paste one file onto the end of another
*/
void append_file(oldFname, bigFname)
char *oldFname, *bigFname;
{
FILE *fp1, *fp2;
char line[MAX_LINE];
char *ret;


	fp1 = fopen(oldFname, "r");
	fp2 = fopen(bigFname, "a");
	if ((fp1 == NULL) || (fp2 == NULL)) {
		printf("WARNING: cannot append to file %s\n", bigFname);
		close(fp1);
		close(fp2);
		return;
	}

	ret = fgets(line, MAX_LINE-1, fp1);
	while (ret != NULL) {
		fputs(line, fp2);
		ret = fgets(line, MAX_LINE-1, fp1);
	}

	close(fp1);
	close(fp2);
		
}	/* append_file */


/*************************************************************/
/* display a file in the present window. 
*/
void more_file(fname)
char *fname;
{	
	char sVal[MAX_LINE];

	sprintf(sVal, "%s %s", DISPLAY, fname);
	system(sVal);

}	/* more_file */


/*************************************************************/
/* Parse unix filename for wildcard characters. 
** Place each individual filename in output file.
*/
void parse_fname(inputFname, outputFname)
char *inputFname, *outputFname;
{	
	char 	sVal[MAX_LINE];

	sprintf(sVal, "ls %s > %s", inputFname, outputFname);
	system(sVal);

}	/* parse_fname */


/*****************************************************************************
* PURPOSE: to create new file for output.
*/
FILE *create_file(fname, confirm)
char *fname;
int confirm;
{
FILE *fp;
char line[MAX_LINE];

	if (checkExist(fname)) {
		printf("\n");
		printf("**  File %s already exists.\n", fname);
		if (confirm) {
			printf(">>  Overwrite existing file (y/n)? ");
			fgets(line,MAX_LINE, stdin );
			fgets(line,MAX_LINE, stdin );
			printf("\n");
			if ( (line[0] == 'n') || (line[0] == 'N') )
				return(NULL);
		}
		else return(NULL);
	}

	fp = fopen(fname, "w");
	if (fp == NULL) {
		printf("**  Cannot create output file %s.\n", fname);
		return(NULL);
	}
	
	return(fp);

}	/* create_file */
