xref: /freebsd/lib/libc/gen/fdevname.c (revision 26d4f5e969a5850cbb0b2344835d515c368d5652)
126d4f5e9SEd Schouten /*-
226d4f5e9SEd Schouten  * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
326d4f5e9SEd Schouten  * All rights reserved.
426d4f5e9SEd Schouten  *
526d4f5e9SEd Schouten  * Redistribution and use in source and binary forms, with or without
626d4f5e9SEd Schouten  * modification, are permitted provided that the following conditions
726d4f5e9SEd Schouten  * are met:
826d4f5e9SEd Schouten  * 1. Redistributions of source code must retain the above copyright
926d4f5e9SEd Schouten  *    notice, this list of conditions and the following disclaimer.
1026d4f5e9SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
1126d4f5e9SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
1226d4f5e9SEd Schouten  *    documentation and/or other materials provided with the distribution.
1326d4f5e9SEd Schouten  *
1426d4f5e9SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1526d4f5e9SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1626d4f5e9SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1726d4f5e9SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1826d4f5e9SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1926d4f5e9SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2026d4f5e9SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2126d4f5e9SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2226d4f5e9SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2326d4f5e9SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2426d4f5e9SEd Schouten  * SUCH DAMAGE.
2526d4f5e9SEd Schouten  */
2626d4f5e9SEd Schouten 
2726d4f5e9SEd Schouten #include <sys/cdefs.h>
2826d4f5e9SEd Schouten __FBSDID("$FreeBSD$");
2926d4f5e9SEd Schouten 
3026d4f5e9SEd Schouten #include "namespace.h"
3126d4f5e9SEd Schouten #include <sys/param.h>
3226d4f5e9SEd Schouten #include <sys/ioctl.h>
3326d4f5e9SEd Schouten #include "un-namespace.h"
3426d4f5e9SEd Schouten 
3526d4f5e9SEd Schouten char *
3626d4f5e9SEd Schouten fdevname_r(int fd, char *buf, int len)
3726d4f5e9SEd Schouten {
3826d4f5e9SEd Schouten 	struct fiodgname_arg fgn;
3926d4f5e9SEd Schouten 
4026d4f5e9SEd Schouten 	fgn.buf = buf;
4126d4f5e9SEd Schouten 	fgn.len = len;
4226d4f5e9SEd Schouten 
4326d4f5e9SEd Schouten 	if (_ioctl(fd, FIODGNAME, &fgn) == -1)
4426d4f5e9SEd Schouten 		return (NULL);
4526d4f5e9SEd Schouten 	return (buf);
4626d4f5e9SEd Schouten }
4726d4f5e9SEd Schouten 
4826d4f5e9SEd Schouten char *
4926d4f5e9SEd Schouten fdevname(int fd)
5026d4f5e9SEd Schouten {
5126d4f5e9SEd Schouten 	static char buf[SPECNAMELEN + 1];
5226d4f5e9SEd Schouten 
5326d4f5e9SEd Schouten 	return (fdevname_r(fd, buf, sizeof(buf)));
5426d4f5e9SEd Schouten }
55