//-----------------------------------------------------------------------------
// Copyright © 2006 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// 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 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.
//-----------------------------------------------------------------------------
// package	libh/io
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#define _GNU_SOURCE

#include "io_lib.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	open_deep
//
// purpose	Open a file and return a file descriptor where the file name is
//		possibly longer than PATH_MAX allows.
//
// arguments	1 (const char *) file name, possibly longer than PATH_MAX
//		2 (int) option flags, e.g. O_RDONLY, O_WRONLY, etc
//		3 (mode_t) mode for created files
//
// returns	(int)  < 0 : [failure, see errno]
//		(int) >= 0 : [success, file opened] file descriptor
//-----------------------------------------------------------------------------
int
open_deep (
    const char *	arg_name
    ,
    int			arg_flags
    ,
    mode_t		arg_mode
    )
__PROTO_END__
{
    char *		ptr		;
    char *		end		;
    int			result		;
    int			save_cwd	;
    int			save_errno	;

    char		name		[NAME_MAX];


    //----------------------------------------------------------
    // First just try to see if the file can be opened normally.
    //----------------------------------------------------------
    result = open( arg_name, arg_flags, arg_mode );
    if ( result >= 0 ) return result;

    //--------------------------------------------
    // Save the current directory as a descriptor.
    //--------------------------------------------
    save_cwd = open( ".", O_RDONLY | O_DIRECTORY );
    if ( save_cwd < 0 ) return save_cwd;

    //---------------------------------------------------------
    // If the file name begins with '/' then start at the root.
    //---------------------------------------------------------
    if ( * arg_name == '/' ) {
	++ arg_name;
	if ( chdir( "/" ) < 0 ) return -1;
    }

    //------------------------------------------------------------
    // Keep a pointer at the end of the name space for comparison.
    //------------------------------------------------------------
    end = name + sizeof name - 1;

    //------------------------------------------------
    // Step down through each directory one at a time.
    //------------------------------------------------
    result = 0;
    for (;;) {

	//---------------------------------------------------------
	// If the next name component is an extra '/' then skip it.
	//---------------------------------------------------------
	while ( * arg_name == '/' ) ++ arg_name;

	//------------------------------------------------
	// Copy the next name component to the work space.
	//------------------------------------------------
	ptr = name;
	while ( ptr < end && * arg_name && * arg_name != '/' ) {
	    * ptr ++ = * arg_name ++;
	}
	* ptr = 0;

	//------------------------------------------------------
	// If this is the last name component, break to open it.
	//------------------------------------------------------
	if ( ! * arg_name ) break;

	//-----------------------------------------------------
	// If the name component is too long, this is an error.
	//-----------------------------------------------------
	if ( * arg_name != '/' ) {
	    errno = ENAMETOOLONG;
	    result = -1;
	    break;
	}

	//----------------------------------------------------
	// If the name component is not "." then change to it.
	//----------------------------------------------------
	if ( name[0] != '.' || name[1] != 0 ) {
	    if ( chdir( name ) < 0 ) {
		result = -1;
		break;
	    }
	}
    }

    //------------------------------------------------
    // Open the last component with the given options.
    //------------------------------------------------
    if ( result == 0 ) {
	result = open( name, arg_flags, arg_mode );
    }

    //--------------------------------
    // Restore the original directory.
    //--------------------------------
    save_errno = errno;
    fchdir( save_cwd );
    close( save_cwd );
    errno = save_errno;

    //-------------------------
    // Return the final result.
    //-------------------------
    return result;
}

