ttyname_r is another of the strange functions that returns its error code

instead of setting errno.
This commit is contained in:
ad 2008-06-25 11:47:29 +00:00
parent 3ae2f84118
commit 2a7116daf3
2 changed files with 26 additions and 21 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: ttyname.3,v 1.20 2008/01/30 19:24:59 apb Exp $
.\" $NetBSD: ttyname.3,v 1.21 2008/06/25 11:47:29 ad Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)ttyname.3 8.1 (Berkeley) 6/4/93
.\"
.Dd January 30, 2008
.Dd June 25, 2008
.Dt TTYNAME 3
.Os
.Sh NAME
@ -87,9 +87,7 @@ is the reentrant version of the above, and it places the results in
.Fa buf .
If there is not enough space to place the results (indicated by
.Fa len ) ,
then it returns \-1 and
.Va errno
is set to indicate the error.
then it returns an error.
.Pp
The
.Fn ttyslot
@ -129,9 +127,7 @@ is set to indicate the error.
.Pp
The
.Fn ttyname_r
functions returns 0 on success and \-1 on failure with
.Va errno
set to indicate the error.
functions returns 0 on success and an error code on failure.
.Pp
The
.Fn isatty
@ -168,6 +164,7 @@ The
.Fa fd
argument does not refer to a terminal device.
.El
.Pp
The
.Fn ttyname_r
function will also fail if:

View File

@ -1,4 +1,4 @@
/* $NetBSD: ttyname.c,v 1.23 2006/03/22 00:05:01 christos Exp $ */
/* $NetBSD: ttyname.c,v 1.24 2008/06/25 11:47:29 ad Exp $ */
/*
* Copyright (c) 1988, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94";
#else
__RCSID("$NetBSD: ttyname.c,v 1.23 2006/03/22 00:05:01 christos Exp $");
__RCSID("$NetBSD: ttyname.c,v 1.24 2008/06/25 11:47:29 ad Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -78,25 +78,25 @@ ttyname_r(int fd, char *buf, size_t len)
_DIAGASSERT(fd != -1);
if (len <= DEVSZ) {
errno = ERANGE;
return -1;
return ERANGE;
}
/* If it is a pty, deal with it quickly */
if (ioctl(fd, TIOCPTSNAME, &ptm) != -1) {
if (strlcpy(buf, ptm.sn, len) >= len) {
errno = ERANGE;
return -1;
return ERANGE;
}
return 0;
}
/* Must be a terminal. */
if (tcgetattr(fd, &ttyb) == -1)
return -1;
return errno;
/* Must be a character device. */
if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
return -1;
if (fstat(fd, &sb))
return errno;
if (!S_ISCHR(sb.st_mode))
return ENOTTY;
(void)memcpy(buf, _PATH_DEV, DEVSZ);
if ((db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) != NULL) {
@ -107,8 +107,7 @@ ttyname_r(int fd, char *buf, size_t len)
key.size = sizeof(bkey);
if (!(db->get)(db, &key, &data, 0)) {
if (len - DEVSZ <= data.size) {
errno = ERANGE;
return -1;
return ERANGE;
}
(void)memcpy(buf + DEVSZ, data.data, data.size);
(void)(db->close)(db);
@ -116,7 +115,9 @@ ttyname_r(int fd, char *buf, size_t len)
}
(void)(db->close)(db);
}
return oldttyname(&sb, buf, len);
if (oldttyname(&sb, buf, len) == -1)
return errno;
return 0;
}
static int
@ -165,5 +166,12 @@ char *
ttyname(int fd)
{
static char buf[MAXPATHLEN];
return ttyname_r(fd, buf, sizeof(buf)) == -1 ? NULL : buf;
int rv;
rv = ttyname_r(fd, buf, sizeof(buf));
if (rv != 0) {
errno = rv;
return NULL;
}
return buf;
}