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

#define _GNU_SOURCE

#include "io_lib.h"

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// function	eput_dirfd
//
// purpose	Output the path of a directory given as an open descriptor
//		to stderr.
//
// arguments	1 (int) file descriptor of directory
//		2 (FILE *) which file to write to, or NULL for stdout
//
// returns	(size_t) length of string written
//-----------------------------------------------------------------------------
#define eput_dirfd(d) (fput_dirfd((d),(stderr)))

__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// function	put_dirfd
//
// purpose	Output the path of a directory given as an open descriptor
//		to stdout.
//
// arguments	1 (int) file descriptor of directory
//		2 (FILE *) which file to write to, or NULL for stdout
//
// returns	(size_t) length of string written
//-----------------------------------------------------------------------------
#define put_dirfd(d) (fput_dirfd((d),(stdout)))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	fput_dirfd
//
// purpose	Output the path of a directory given as an open descriptor
//		to the specified file.
//
// arguments	1 (int) file descriptor of directory
//		2 (FILE *) which file to write to, or NULL for stdout
//
// returns	(size_t) length of string written
//-----------------------------------------------------------------------------
int
fput_dirfd (
    int			arg_dirfd
    ,
    FILE *		arg_file
    )
__PROTO_END__
{
    struct stat		stat_buf	;
    size_t		cwd_len		;
    int			save_cwd	;
    char		cwd_space	[ PATH_MAX + 1 ];


    if ( ! arg_file ) arg_file = stdout;

    save_cwd = open( ".", O_RDONLY | O_DIRECTORY );
    if ( save_cwd < 0 ) return 0;

    if ( fstat( arg_dirfd, & stat_buf ) < 0 ) {
	fputs( "(BAD_DESCRIPTOR)", arg_file );
	cwd_len = strlen( "(BAD_DESCRIPTOR)" );
    }

    //-- If this is a directory, change to it to output the path.
    else if ( S_ISDIR( stat_buf.st_mode ) ) {
	if ( fchdir( arg_dirfd ) == 0 ) {
	    if ( getcwd( cwd_space, sizeof cwd_space ) ) {
		fputs( cwd_space, arg_file );
	    }
	}
	cwd_len = strlen( cwd_space );
    }

    //-- Else indicate it is not a directory.
    else {
	fputs( "(NOT_A_DIRECTORY)", arg_file );
	cwd_len = strlen( "(NOT_A_DIRECTORY)" );
    }

    //-- If the current directory cannot be restored, abort the whole process.
    if ( fchdir( save_cwd ) < 0 ) abort();
    close( save_cwd );

    return cwd_len;
}

