openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/glibc/2.29-r0/git/sysdeps/unix/sysv/linux/ttyname_r.c
int __ttyname_r(int fd, char* buf, size_t buflen)
{
    char procname[30];
    struct stat64 st, st1;
    int dostat = 0;
    int doispty = 0;
    int save = errno;

    /* Test for the absolute minimal size.  This makes life easier inside
       the loop.  */
    if (!buf)
    {
        __set_errno(EINVAL);
        return EINVAL;
    }

    if (buflen < sizeof("/dev/pts/"))
    {
        __set_errno(ERANGE);
        return ERANGE;
    }

    /* isatty check, tcgetattr is used because it sets the correct
       errno (EBADF resp. ENOTTY) on error.  */
    struct termios term;
    if (__glibc_unlikely(__tcgetattr(fd, &term) < 0))
        return errno;

    if (__fxstat64(_STAT_VER, fd, &st) < 0)
        return errno;

    /* We try using the /proc filesystem.  */
    *_fitoa_word(fd, __stpcpy(procname, "/proc/self/fd/"), 10, 0) = '\0';

    ssize_t ret = __readlink(procname, buf, buflen - 1);
    if (__glibc_unlikely(ret == -1 && errno == ENAMETOOLONG))
    {
        __set_errno(ERANGE);
        return ERANGE;
    }

    if (__glibc_likely(ret != -1))
    {
#define UNREACHABLE_LEN strlen("(unreachable)")
        if (ret > UNREACHABLE_LEN &&
            memcmp(buf, "(unreachable)", UNREACHABLE_LEN) == 0)
        {
            memmove(buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
            ret -= UNREACHABLE_LEN;
        }

        /* readlink need not terminate the string.  */
        buf[ret] = '\0';

        /* Verify readlink result, fall back on iterating through devices.  */
        if (buf[0] == '/' && __xstat64(_STAT_VER, buf, &st1) == 0 &&
            is_mytty(&st, &st1))
            return 0;

        doispty = 1;
    }

    /* Prepare the result buffer.  */
    memcpy(buf, "/dev/pts/", sizeof("/dev/pts/"));
    buflen -= sizeof("/dev/pts/") - 1;

    if (__xstat64(_STAT_VER, buf, &st1) == 0 && S_ISDIR(st1.st_mode))
    {
        ret = getttyname_r(buf, buflen, &st, save, &dostat);
    }
    else
    {
        __set_errno(save);
        ret = ENOENT;
    }

    if (ret && dostat != -1)
    {
        buf[sizeof("/dev/") - 1] = '\0';
        buflen += sizeof("pts/") - 1;
        ret = getttyname_r(buf, buflen, &st, save, &dostat);
    }

    if (ret && dostat != -1)
    {
        buf[sizeof("/dev/") - 1] = '\0';
        dostat = 1;
        ret = getttyname_r(buf, buflen, &st, save, &dostat);
    }

    if (ret && doispty && is_pty(&st))
    {
        /* We failed to figure out the TTY's name, but we can at least
           signal that we did verify that it really is a PTY slave.
           This happens when we have inherited the file descriptor from
           a different mount namespace.  */
        __set_errno(ENODEV);
        return ENODEV;
    }

    return ret;
}
/* Store at most BUFLEN character of the pathname of the terminal FD is
   open on in BUF.  Return 0 on success,  otherwise an error number.  */
Store at most BUFLEN character of the pathname of the terminal FD is
open on in BUF.  Return 0 on success,  otherwise an error number.
Store at most BUFLEN character of the pathname of the terminal FD is open on in BUF . Return 0 on success otherwise an error number .
int __ttyname_r(int fd, char* buf, size_t buflen)
{
    char procname[30];
    struct stat64 st, st1;
    int dostat = 0;
    int doispty = 0;
    int save = errno;
    if (!buf)
    {
        __set_errno(EINVAL);
        return EINVAL;
    }
    if (buflen < sizeof("/dev/pts/"))
    {
        __set_errno(ERANGE);
        return ERANGE;
    }
    struct termios term;
    if (__glibc_unlikely(__tcgetattr(fd, &term) < 0))
        return errno;
    if (__fxstat64(_STAT_VER, fd, &st) < 0)
        return errno;
    *_fitoa_word(fd, __stpcpy(procname, "/proc/self/fd/"), 10, 0) = '\0';
    ssize_t ret = __readlink(procname, buf, buflen - 1);
    if (__glibc_unlikely(ret == -1 && errno == ENAMETOOLONG))
    {
        __set_errno(ERANGE);
        return ERANGE;
    }
    if (__glibc_likely(ret != -1))
    {
#define UNREACHABLE_LEN strlen("(unreachable)")
        if (ret > UNREACHABLE_LEN &&
            memcmp(buf, "(unreachable)", UNREACHABLE_LEN) == 0)
        {
            memmove(buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
            ret -= UNREACHABLE_LEN;
        }
        buf[ret] = '\0';
        if (buf[0] == '/' && __xstat64(_STAT_VER, buf, &st1) == 0 &&
            is_mytty(&st, &st1))
            return 0;
        doispty = 1;
    }
    memcpy(buf, "/dev/pts/", sizeof("/dev/pts/"));
    buflen -= sizeof("/dev/pts/") - 1;
    if (__xstat64(_STAT_VER, buf, &st1) == 0 && S_ISDIR(st1.st_mode))
    {
        ret = getttyname_r(buf, buflen, &st, save, &dostat);
    }
    else
    {
        __set_errno(save);
        ret = ENOENT;
    }
    if (ret && dostat != -1)
    {
        buf[sizeof("/dev/") - 1] = '\0';
        buflen += sizeof("pts/") - 1;
        ret = getttyname_r(buf, buflen, &st, save, &dostat);
    }
    if (ret && dostat != -1)
    {
        buf[sizeof("/dev/") - 1] = '\0';
        dostat = 1;
        ret = getttyname_r(buf, buflen, &st, save, &dostat);
    }
    if (ret && doispty && is_pty(&st))
    {
        __set_errno(ENODEV);
        return ENODEV;
    }
    return ret;
}
int __ttyname_r ( int fd , char * buf , size_t buflen ) { char procname [ 30 ] ; struct stat64 st , st1 ; int dostat = 0 ; int doispty = 0 ; int save = errno ; if ( ! buf ) { __set_errno ( EINVAL ) ; return EINVAL ; } if ( buflen < sizeof ( " " ) ) { __set_errno ( ERANGE ) ; return ERANGE ; } struct termios term ; if ( __glibc_unlikely ( __tcgetattr ( fd , & term ) < 0 ) ) return errno ; if ( __fxstat64 ( _STAT_VER , fd , & st ) < 0 ) return errno ; * _fitoa_word ( fd , __stpcpy ( procname , " " ) , 10 , 0 ) = ' \0 ' ; ssize_t ret = __readlink ( procname , buf , buflen - 1 ) ; if ( __glibc_unlikely ( ret == -1 && errno == ENAMETOOLONG ) ) { __set_errno ( ERANGE ) ; return ERANGE ; } if ( __glibc_likely ( ret != -1 ) ) { #define UNREACHABLE_LEN  strlen("(unreachable)") if ( ret > UNREACHABLE_LEN && memcmp ( buf , " " , UNREACHABLE_LEN ) == 0 ) { memmove ( buf , buf + UNREACHABLE_LEN , ret - UNREACHABLE_LEN ) ; ret -= UNREACHABLE_LEN ; } buf [ ret ] = ' \0 ' ; if ( buf [ 0 ] == ' ' && __xstat64 ( _STAT_VER , buf , & st1 ) == 0 && is_mytty ( & st , & st1 ) ) return 0 ; doispty = 1 ; } memcpy ( buf , " " , sizeof ( " " ) ) ; buflen -= sizeof ( " " ) - 1 ; if ( __xstat64 ( _STAT_VER , buf , & st1 ) == 0 && S_ISDIR ( st1 . st_mode ) ) { ret = getttyname_r ( buf , buflen , & st , save , & dostat ) ; } else { __set_errno ( save ) ; ret = ENOENT ; } if ( ret && dostat != -1 ) { buf [ sizeof ( " " ) - 1 ] = ' \0 ' ; buflen += sizeof ( " " ) - 1 ; ret = getttyname_r ( buf , buflen , & st , save , & dostat ) ; } if ( ret && dostat != -1 ) { buf [ sizeof ( " " ) - 1 ] = ' \0 ' ; dostat = 1 ; ret = getttyname_r ( buf , buflen , & st , save , & dostat ) ; } if ( ret && doispty && is_pty ( & st ) ) { __set_errno ( ENODEV ) ; return ENODEV ; } return ret ; }
Store at most BUFLEN character of the pathname of the terminal FD is
open on in BUF.
Store at most BUFLEN character of the pathname of the terminal FD is open on in BUF .
/* Test for the absolute minimal size.  This makes life easier inside
       the loop.  */ /* isatty check, tcgetattr is used because it sets the correct
       errno (EBADF resp. ENOTTY) on error.  */ /* We try using the /proc filesystem.  */ /* readlink need not terminate the string.  */ /* Verify readlink result, fall back on iterating through devices.  */ /* Prepare the result buffer.  */ /* We failed to figure out the TTY's name, but we can at least
           signal that we did verify that it really is a PTY slave.
           This happens when we have inherited the file descriptor from
           a different mount namespace.  */