//-----------------------------------------------------------------------------
// Copyright © 2003 - 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	lstat_join
//
// purpose	Return information about a file where the file name is
//		given as multiple string arguments to be joined together.
//
// arguments	1 (struct stat *) buffer where to store file information
//		2..N (const char *) file name parts
//
// returns	(int) == -1 : [failure, see errno]
//		(int) ==  0 : [success, file info stored] status
//-----------------------------------------------------------------------------
#define lstat_join(b,n...) ((lstat_join)((b),n,(const char*)(NULL)))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	(lstat_join)
//
// purpose	Return information about a file where the file name is
//		given as multiple string arguments to be joined together
//		ended by a NULL argument.
//
// arguments	1 (struct stat *) buffer where to store file information
//		2..N (const char *) file name parts
//		N+1 (const char *) NULL to indicate end of list
//
// returns	(int) == -1 : [failure, see errno]
//		(int) ==  0 : [success, file info stored] status
//-----------------------------------------------------------------------------
int
(lstat_join) (
    struct stat *	arg_buf
    ,
    ...
    )
__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_buf );
    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 -1;

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

    //-------------------------------------------------
    // Stat the constructed name and return the result.
    //-------------------------------------------------
    return lstat( name, arg_buf );
}

