/*
   <p>Force a string to conform to a correct local pathname.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/


#include "environ.h"
#include <ctype.h>

#ifdef OS9
#include <strings.h>
#endif

#ifdef MSDOS
#include <string.h>
#include <stdlib.h>
#endif

#ifdef LINUX
#include <string.h>
#include <stdlib.h>
#endif

/*
   Force the pathname in <u>path</u> to conform
   to a correct local pathname.
   The default extension is in <u>ext</u>.
   <u>type</u> sets the file type. It is "f" for
   a regular file or "d" for a directory.
   
   If the pathname is a local name this is
   equivalent to calling <i>fixname()</i>. If there is
   more than one term all except the last will
   be treated as directories. The last term will
   be treated as directory or file depending on 
   the type parameter. To process a path:
   <ol>
       <li>copy separators unchanged</li>
       <li>extract terms and pass them through <i>fixname()</i></li>
       <li>terms followed by a separator are treated as directories</li>
       <li>the final term, if null-terminated, is treated as
            requested in the type parameter.</li>
   </ol>
   
   <b>NOTE:</b>    The function allocates storage for the
                   modified name in a safe place. If it is
                   called repeatedly this space should be
                   released as soon as possible by calling
                   <i>free()</i>.

   The function returns the modified pathname
   on success and null on failure.
*/
char *fixpath(char *path, char *ext, char *type)
{
    int     sepcount,
            i,
            j,
            k;
    char    *newterm,
            *newpath,
            *ptr,
            workspace[MAXPATH+1];
            
    /*
        Make a place to hold the new filename
    */
    newpath = (char*)malloc(MAXPATH+1);
    if (newpath == NULL)
        return (newpath);

    /*
        See if this is a local name or a (partial) pathname.
    */
    for (sepcount = i = 0; path[i]; i++)
        if (path[i] == SEPARATOR)
            sepcount++;

    /*
        Local name - treat it as a special case
    */
    if (sepcount == 0)
    {
        newterm = fixname(path,ext,type);
        strcpy(newpath,newterm);
        free(newterm);
        return (newpath);
    }
        
    /*
        Process a path;
        1    copy separators unchanged
        2    extract terms and pass them through fixname()
        3    terms followed by a separator are treated as directories
        4    the final term, if null-terminated, is treated as
             requested in the type parameter.
    */
    i = j = 0;
    while (path[i])
    {
        while (path[i] == SEPARATOR)
            newpath[j++] = path[i++];
        for (k = 0; path[i] != SEPARATOR && path[i]; i++, k++)
            workspace[k] = path[i];
        workspace[k] = 0;
        if (path[i] == SEPARATOR)
            ptr = fixname(workspace,"","d");
        else
            ptr = fixname(workspace,ext,type);
        for (k = 0; ptr[k]; j++, k++)
            newpath[j] = ptr[k];
        free(ptr);
    }
    newpath[j] = 0;
        
    return (newpath);
}

