//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// file		open_unique.c
//
// purpose	Source for related functions
//-----------------------------------------------------------------------------
#include <libh/time.h>
#include "io_lib.h"


__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	open_unique
//
// purpose	Create and open a new unique file for output.
//
// arguments	1 (const char *) filename format pattern (etime format specs)
//		2 (int) non-zero to also read
//		3 (int) open mode
//
// returns	(int)  < 0 : error returned by last open() after many tries
//		(int) >= 0 : open file descriptor handle
//-----------------------------------------------------------------------------
int
open_unique (
    const char *	arg_format
    ,
    int			arg_flags
    ,
    int			arg_mode
    )
__PROTO_END__
{
    int		dir_mode	;
    int		tries		;
    int		fd		;
    char	filename	[PATH_MAX]	;

    //-- Setup proper open flags.
    if ( ( arg_flags & (int) ( O_RDONLY | O_WRONLY | O_RDWR ) ) != O_WRONLY &&
	 ( arg_flags & (int) ( O_RDONLY | O_WRONLY | O_RDWR ) ) != O_RDWR ) return -1;
    arg_flags |= ( O_CREAT | O_EXCL );

    //-- Setup proper file and directory modes.
    dir_mode = arg_mode | 0300;
    if ( dir_mode & 0060 ) dir_mode |= 0010;
    if ( dir_mode & 0006 ) dir_mode |= 0001;

    //-- Now try to make a unique file name
    tries = 1024;
    do {
	if ( etime_to_str_cpy( filename, PATH_MAX, arg_format, 0 ) == ~0 ) return -1;
	make_parent_dir( filename, dir_mode );
	fd = open( filename, arg_flags, arg_mode );
	if ( fd >= 0 ) break;
    } while ( -- tries );

    return fd;
}

