//-----------------------------------------------------------------------------
// 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	multi_close
//
// purpose	Close multiple file descriptors from a variable argument list.
//
// arguments	1..N (int) file descriptor to close
//
// returns	(int) number of descriptors closed successfully
//-----------------------------------------------------------------------------
#define multi_close(f,a...) ((multi_close)((f),a,-1))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	(multi_close)
//
// purpose	Close multiple file descriptors from a variable argument list
//		ending in a negative number.
//
// arguments	1..N (int) file descriptor to close
//		N+1 (int) -1 to indicate end of list
//
// returns	(int) number of descriptors closed successfully
//-----------------------------------------------------------------------------
int
(multi_close) (
    int		arg_fd
    ,
    ...
    )
__PROTO_END__
{
    va_list		var_args	;
    int			this_fd		;
    int			count		;

    count = 0;
    this_fd = arg_fd;
    va_start( var_args, arg_fd );
    while ( this_fd >= 0 ) {
	if ( close( this_fd ) == 0 ) ++ count;
	this_fd = va_arg( var_args, int );
    }
    va_end( var_args );
    return count;
}

