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

   Utility routines
*/
#include "string.h"  /* strcat() */
#include "ioserv.h"  /* NPATHMAX */

/* new_extension: replace an old extension, or add a new one.
   The extension must include the period, since the old name
   is copied up to but not including the period or ending null.
   The extension must be no more than three characters (plus
   the period and the null).

   Modified 5/2/91 - JR - changed algorithm (old version
      searched from start for a '.' and interpreted
      remainder as extension, thereby mishandling names
      that had '.' in a path context, e.g., '../cal/a901')

   Modified 93/09/22 - JR - erased 4-char limit on extension

*/
#if PROTOTYPE_ALLOWED
void new_extension(char *new_name, char *old_name, char *extension)
#else
void new_extension(new_name, old_name, extension)
char *new_name, *old_name, *extension;
#endif
{
   char path_name[NPATHMAX];
   int i;

   split_path(old_name, path_name, new_name);
   if ( (i = strlen(path_name)) > 0 ) /* special char. found */ 
   {
      if (path_name[i-1] == '.') /* path_name has prefix part */ 
      {
         path_name[i-1] = '\0';  /* drop the '.' */
         strcpy(new_name, path_name); /* preserve path */
      }
      else /* then path_name must be path part */
      {    /* & old_name had no extension to begin with */
         strcpy(new_name, old_name);
      }
   }
   new_name[NPATHMAX - strlen(extension) - 1] = '\0'; /* truncate if too long */
   strcat(new_name, extension);
}
      
