11573Srgrimes/*-
2225897Sed * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3225897Sed * All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes *
14225897Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17225897Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241573Srgrimes * SUCH DAMAGE.
251573Srgrimes */
261573Srgrimes
2792986Sobrien#include <sys/cdefs.h>
2892986Sobrien__FBSDID("$FreeBSD$");
291573Srgrimes
30225897Sed#include <sys/param.h>
31225897Sed#include <sys/stat.h>
32225897Sed#include <sys/sysctl.h>
33225897Sed
34225897Sed#include <errno.h>
35225897Sed#include <paths.h>
361573Srgrimes#include <stdio.h>
371573Srgrimes#include <string.h>
381573Srgrimes
39225897Sed#define	LEN_PATH_DEV	(sizeof(_PATH_DEV) - 1)
40225897Sed
411573Srgrimeschar *
4253863Swesctermid(char *s)
431573Srgrimes{
44225897Sed	static char def[sizeof(_PATH_DEV) + SPECNAMELEN];
45225897Sed	struct stat sb;
46225897Sed	size_t dlen;
47225897Sed	int sverrno;
481573Srgrimes
49225897Sed	if (s == NULL) {
50225897Sed		s = def;
51225897Sed		dlen = sizeof(def) - LEN_PATH_DEV;
52225897Sed	} else
53225897Sed		dlen = L_ctermid - LEN_PATH_DEV;
54225897Sed	strcpy(s, _PATH_TTY);
55225897Sed
56225897Sed	/* Attempt to perform a lookup of the actual TTY pathname. */
57225897Sed	sverrno = errno;
58225897Sed	if (stat(_PATH_TTY, &sb) == 0 && S_ISCHR(sb.st_mode))
59225897Sed		(void)sysctlbyname("kern.devname", s + LEN_PATH_DEV,
60225897Sed		    &dlen, &sb.st_rdev, sizeof(sb.st_rdev));
61225897Sed	errno = sverrno;
62225897Sed	return (s);
631573Srgrimes}
6453863Swes
6553863Sweschar *
6653863Swesctermid_r(char *s)
6753863Swes{
68225897Sed
69225897Sed	return (s != NULL ? ctermid(s) : NULL);
7053863Swes}
71