Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 10 Apr 1998 22:18:22 +0000 (GMT)
From:      Terry Lambert <tlambert@primenet.com>
To:        scrappy@hub.org (The Hermit Hacker)
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: filename from open file descriptor...
Message-ID:  <199804102218.PAA01789@usr07.primenet.com>
In-Reply-To: <Pine.BSF.3.96.980410164633.290G-100000@thelab.hub.org> from "The Hermit Hacker" at Apr 10, 98 04:47:48 pm

next in thread | previous in thread | raw e-mail | index | archive | help
> 	I'm trying to debug some code, and have an open file descriptor
> that I want to find the file name that its associated with...
> 
> 	is this possible?  my first thought was fstat(), but that appears
> to return everything but the name :(

You remember the name when you open the file.

Untested:
----------------------------------------------------------------------------
/*
 * This code is relased under BSD licensce in the name of Terrence
 * Lambert.  Please place appropriate license text into any released
 * version of this software.
 */
static char	**saved_open_names;

/*
 * Overrides weak "open" symbol from libc
 */
int
open( const char *path, int flags, mode_t mode)
{
    int	rv;

    if( saved_open_names == NULL) {
	int	count;

	count = getdtablesize();
	saved_open_names = (char **)calloc( count, sizeof(char *));
    }

    if( saved_open_names == NULL) {
	rv = -1;
	errno = ENOMEM;
    } else {
	char	*p;

	if( ( p = strdup( path)) == NULL) {
	    rv = -1; 
	    errno = ENOMEM;
	} else {
	    if( ( rv = syscall( SYS_open, path, flags, mode)) == -1) {
		free( p);
	    } else {
		if( saved_open_names[ rv] != NULL) {
		    /* could implement 'close' instead...*/
		    free( saved_open_names[ rv]);
		}
		saved_open_names[ rv] = p;
	    }
	}
    }

    return( rv);
}

/*
 * New call...
 */
char *
fdname( int fd)
{
    char	*rv = NULL;

    if( saved_open_names != NULL) {
	if( fd >= 0 && fd < getdtablesize()) {
	    rv = saved_open_names[ fd];
	}
    }

    return( rv);
}
----------------------------------------------------------------------------



					Terry Lambert
					terry@lambert.org
---
Any opinions in this posting are my own and not those of my present
or previous employers.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199804102218.PAA01789>