//-----------------------------------------------------------------------------
// Copyright © 2004 - 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"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	make_parent_dir
//
// purpose	Make sure the parent directory exists for the given path.
//
// arguments	1 (const char *) pathname of object a parent is needed for
//		2 (int) directory access mode
//
// returns	(int)  <  0 : error returned by first mkdir()
//		(int) == -1 : error
//		(int) ==  0 : parent directory is created
//		(int) ==  1 : parent directory existed
//		(int) ==  2 : path does not need a parent
//
// WARNING	This function may be unsafe when executed as root for the
//		purpose of creating directories for the real user.  Swap
//		the euid and ruid first.
//-----------------------------------------------------------------------------
int
make_parent_dir (
    const char *	arg_path
    ,
    int			arg_mode
    )
__PROTO_END__
{
    struct stat		stat_buf	;
    char *		slash_ptr	;
    char		work_path	[ PATH_MAX + 1 ];

    //-- Copy the given path to the work space.
    work_path[ PATH_MAX ] = 0;
    strncpy( work_path, arg_path, PATH_MAX + 1 );
    if ( work_path[ PATH_MAX ] != 0 ) {
	errno = ENAMETOOLONG;
	return -1;
    }

    //-- Must allow at least write and execute permission to owner.
    arg_mode |= ( S_IWUSR | S_IXUSR );

    //-- Step through directory levels and verify access.
    slash_ptr = work_path;
    while ( * slash_ptr ) {

	if ( * slash_ptr == '/' ) {
	    * slash_ptr = 0;

	    //-- If the path does not exist, make it.
	    if ( stat( work_path, & stat_buf ) < 0 ) {
		if ( mkdir( work_path, arg_mode ) < 0 ) return -1;
	    }

	    //-- If it is not a directory, quit now.
	    else if ( ! S_ISDIR( stat_buf.st_mode ) ) {
		errno = ENOTDIR;
		return -2;
	    }

	    * slash_ptr = '/';
	}

	++ slash_ptr;
    }

    return 0;
}

