//-----------------------------------------------------------------------------
// Copyright © 2003,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.
//-----------------------------------------------------------------------------

#include "io_lib.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	dlopen_join
//
// purpose	Open a dynamically linked library where the file name is
//		given as multiple string arguments to be joined together.
//
// arguments	1 (int) option flags, e.g. RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL
//		2..N (const char *) file name parts
//
// returns	(void *) == NULL : [failure, see errno]
//		(void *) != NULL : [success, library open] handle
//-----------------------------------------------------------------------------
#define dlopen_join(f,n...) ((dlopen_join)((dlopen),(f),n,(const char*)(NULL)))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	(dlopen_join)
//
// purpose	Open a dynamically linked library where the file name is
//		given as multiple string arguments to be joined together.
//
// arguments	1 ((*)()) function pointer to dlopen passed by macro
//		2 (int) option flags, e.g. RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL
//		3..N (const char *) file name parts
//		N+1 (const char *) NULL to indicate end of list
//
// returns	(void *) == NULL : [failure, see errno]
//		(void *) != NULL : [success, library open] handle
//
// note		The reference to dlopen is passed from the calling program via
//		the wrapper macro in argument one so that LIBH itself does not
//		generate a reference to libdl.
//-----------------------------------------------------------------------------
void *
(dlopen_join) (
    void *	( *	arg_dlopen	)( const char *, int )
    ,
    int			arg_flags
    ,
    ...
    )
__PROTO_END__
{
    va_list		var_args	;
    const char *	str		;
    char *		name		;
    char *		ptr		;
    size_t		len		;


    //--------------------------------------------------------------
    // First figure out how much space is needed for the given name.
    //--------------------------------------------------------------
    len = 1;
    va_start( var_args, arg_flags );
    while ( ( str = va_arg( var_args, const char * ) ) ) {
	len += strlen( str );
    }
    va_end ( var_args );

    //---------------------------------------------
    // Temporarily allocate the exact space needed.
    //---------------------------------------------
    if ( ! ( name = alloca( len ) ) ) return NULL;

    //--------------------
    // Construct the name.
    //--------------------
    ptr = name;
    va_start( var_args, arg_flags );
    while ( ( str = va_arg( var_args, const char * ) ) ) {
	while ( * str ) * ptr ++ = * str ++;
    }
    va_end ( var_args );
    * ptr = 0;

    //-------------------------------------------------
    // Open the constructed name and return the handle.
    //-------------------------------------------------
    return (*arg_dlopen)( name, arg_flags );
}

