/*
***************************************************************************

 Module:      FILEUTIL.C

 Description: A collection of general purpose file manipulation
              primitives.

 Version:     2.5a1 (SuSE Linux 9.0, gcc compiler)

 Author:      Michael Campbell

***************************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**/

FILE *file_open ( filename, open_type, init_dir )

/**************************************************************************
 This function attempts to open the inidcated file, using the indicated open
 type. It searches three different directories, in order, for the requested
 file. The directories are:

 a) The current directory
 b) The init directory (usually the directory from which the calling 
    program was invoked)
 c) The directory associated with the environment variable "HOME" (if such
    a var exists

 If the file is found in any of these directories, a FILE pointer to it is
 returned. Else, NULL is returned.

 **************************************************************************/

char *filename;         /* The name of the file to be opened              */
char *open_type;        /* The type of open requested (read, write, both) */
char *init_dir;         /* The path of the init dir, terminated with '\'  */
{
   char full_filename[82]; /* String for the full filename                */
   char *home_dir;         /* The path of the HOME directory              */
   int  len;               /* Length of file names                        */
   FILE *file_ptr;         /* Returned file pointer                       */

   file_ptr = NULL;

   /* Start by attempting to find the file in the current dir             */

   file_ptr = fopen ( filename, open_type );

   if ( file_ptr == NULL ) {

      /* Didn't find the file in the current directory. Try the init dir  */

      strcpy ( full_filename, init_dir );
      strcat ( full_filename, filename );

      file_ptr = fopen ( full_filename, open_type );

      if ( file_ptr == NULL ) { 

         /* Didn't find the file in the init dir. Try the HOME dir         */

         home_dir = (char *) getenv ( "HOME" );

         if ( home_dir != NULL ) { 

            /* There is a home dir - attempt to open the file there        */

            strcpy ( full_filename, home_dir );

            /* Add a trailing '/' if it is not there already               */

            len = strlen ( full_filename );
            if ( full_filename[len - 1] != '/' )
               strcat ( full_filename, "/" );

            strcat ( full_filename, filename );

            file_ptr = fopen ( full_filename, open_type );
         }
      }
   }
   return ( file_ptr );

} /* file_open */

/**/

int file_exists ( filename )

/**************************************************************************
 This function checks for the existance of the supplied file name  in the   
 current directory. If the file is there, it returns the value 1, else, it 
 returns the value 0.         
 **************************************************************************/

char *filename;         /* The name of the file to be opened              */
{
   FILE *file_ptr;      /* Pointer to the file we are checking            */
   int  return_val;     /* The returned value: 1=file exists, 0=doesn't   */

   file_ptr = fopen ( filename, "r" );

   return_val =  ( file_ptr == NULL ? 0 : 1 );
   
   if ( file_ptr != NULL ) 
      fclose ( file_ptr );
 
   return ( return_val );
}

/**/

int filename_of ( full_pathname, filename )

/**************************************************************************
 This function examines the supplied pathname, and filters out the prefix  
 (directory path) portion. It returns an integer which is the index of the 
 the first character in the actual filename, within the full_pathname, and 
 copies the filename itself to the output variable filename.
 **************************************************************************/

char *full_pathname;    /* The full pathname of a file                    */
char *filename;         /* The filename within the path                   */

{
   char letter;         /* A letter in the pathname                       */
   int i, ix;           /* Indices                                        */

   i = ix = 0;

   while ( letter = full_pathname[i++] ) {

      /* Based on the assumption that directory path components are       */
      /* separated by forward slashes, find the last slash. Everything    */
      /* that follows is the filename portion of the pathname.            */

      if ( letter == '/' )
         ix = i;
   }

   /* Copy the filename to the output variable, and return its index      */

   strcpy ( filename, &full_pathname[ix] );
   
   return ( ix );

} /* filename_of */

/**/

void pathname_of ( full_pathname, pathname )

/**************************************************************************
 This function examines the supplied pathname, and filters out the final   
 (filename) portion. It returns the directory path portion of the pathname
 in the output string pathname.
 **************************************************************************/

char *full_pathname;    /* The full pathname of a file                    */
char *pathname;         /* The pathname within the path                   */

{
   char filename[12];   /* The filename portion of the pathname           */
   int  filename_ix;    /* The index of the filename within the path      */
   int  i;              /* Loop counter                                   */

   /* Get the postition of the final filename portion in the pathname     */

   filename_ix = filename_of ( full_pathname, filename );

   /* If there is no path element in the supplied name, just return a nil */
   /* string. Else, copy over the path portion of the name, and terminate */
   /* it properly, so that it is a string.                                */

   if ( filename_ix == 0 ) 
      pathname[0] = '\0';

   else {
      for ( i = 0; i < filename_ix; i++ )
         pathname[i] = full_pathname[i];

      pathname[filename_ix] = '\0';
   }

} /* pathname_of */
