mirror of
https://github.com/apache/nuttx.git
synced 2026-06-08 01:42:58 +08:00
libc: Implement ttyname and ttyname_r
Note: this patch can get file path from root pseudo file handle, but a general infrastructure is setup for other file system too. Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Change-Id: I86cd79ebb741f2f43fdd398bb7498c40687d949b
This commit is contained in:
committed by
Gustavo Henrique Nihei
parent
b7653e2fae
commit
2fa1e55628
@@ -27,6 +27,7 @@ ifeq ($(CONFIG_SERIAL_TERMIOS),y)
|
||||
|
||||
CSRCS += lib_cfspeed.c lib_cfmakeraw.c lib_isatty.c lib_tcflush.c
|
||||
CSRCS += lib_tcflow.c lib_tcgetattr.c lib_tcsetattr.c
|
||||
CSRCS += lib_ttyname.c lib_ttynamer.c
|
||||
|
||||
# Add the termios directory to the build
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/****************************************************************************
|
||||
* libs/libc/termios/lib_ttyname.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ttyname
|
||||
*
|
||||
* Description:
|
||||
* The ttyname() function shall return a pointer to a string containing
|
||||
* a null-terminated pathname of the terminal associated with file
|
||||
* descriptor fildes. The application shall not modify the string returned.
|
||||
* The returned pointer might be invalidated or the string content might
|
||||
* be overwritten by a subsequent call to ttyname(). The returned pointer
|
||||
* and the string content might also be invalidated if the calling thread
|
||||
* is terminated.
|
||||
*
|
||||
* Input Parameters:
|
||||
* fd - The 'fd' argument is an open file descriptor associated with
|
||||
* a terminal.
|
||||
*
|
||||
* Returned Value:
|
||||
* Upon successful completion, ttyname() shall return a pointer to
|
||||
* a string. Otherwise, a null pointer shall be returned and errno
|
||||
* set to indicate the error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *ttyname(int fd)
|
||||
{
|
||||
static char name[TTY_NAME_MAX];
|
||||
int ret;
|
||||
|
||||
ret = ttyname_r(fd, name, TTY_NAME_MAX);
|
||||
if (ret != 0)
|
||||
{
|
||||
set_errno(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/****************************************************************************
|
||||
* libs/libc/termios/lib_ttynamer.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ttyname_r
|
||||
*
|
||||
* Description:
|
||||
* The ttyname_r() function shall store the null-terminated pathname of
|
||||
* the terminal associated with the file descriptor fildes in the
|
||||
* character array referenced by name. The array is namesize characters
|
||||
* long and should have space for the name and the terminating null
|
||||
* character. The maximum length of the terminal name shall be
|
||||
* {TTY_NAME_MAX}.
|
||||
*
|
||||
* Input Parameters:
|
||||
* fd - The 'fd' argument is an open file descriptor associated with
|
||||
* a terminal.
|
||||
* buf - Caller provided buffer to hold tty name.
|
||||
* buflen - The size of the caller-provided buffer.
|
||||
*
|
||||
* Returned Value:
|
||||
* If successful, the ttyname_r() function shall return zero.
|
||||
* Otherwise, an error number shall be returned to indicate the error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int ttyname_r(int fd, FAR char *buf, size_t buflen)
|
||||
{
|
||||
if (!isatty(fd))
|
||||
{
|
||||
return ENOTTY;
|
||||
}
|
||||
|
||||
if (buflen >= TTY_NAME_MAX)
|
||||
{
|
||||
return fcntl(fd, F_GETPATH, buf) < 0 ? get_errno() : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
char name[TTY_NAME_MAX];
|
||||
|
||||
if (fcntl(fd, F_GETPATH, name) < 0)
|
||||
{
|
||||
return get_errno();
|
||||
}
|
||||
|
||||
if (strlen(name) >= buflen)
|
||||
{
|
||||
return ERANGE;
|
||||
}
|
||||
|
||||
strcpy(buf, name);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user