This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 0427beea1729acf98e652777d1702b3cb1305e51
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 27 19:19:15 2026 +0100

    libs/libc/stdlib: Make grantpt() a function.
    
    <stdlib.h> defined grantpt as a macro that gives 0, and libc had no
    grantpt symbol.  A program that declares grantpt() itself, as vim's
    pty.c does ("int grantpt(int);"), then did not compile, and a link test
    did not find it.
    
    Make it a function of libc.  NuttX creates the slave pseudoterminal with
    the owner and the mode it needs, so grantpt() only checks that fd is a
    pseudoterminal: it fails with EBADF or ENOTTY otherwise.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/stdlib.h                |  5 +---
 libs/libc/stdlib/CMakeLists.txt |  9 ++++++-
 libs/libc/stdlib/Make.defs      |  1 +
 libs/libc/stdlib/lib_grantpt.c  | 55 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h
index 34dd9561cfe..138d290fb58 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -262,10 +262,7 @@ int       posix_openpt(int oflag);
 FAR char *ptsname(int fd);
 int       ptsname_r(int fd, FAR char *buf, size_t buflen);
 int       unlockpt(int fd);
-
-/* int grantpt(int fd); Not implemented */
-
-#define grantpt(fd) (0)
+int       grantpt(int fd);
 #endif
 
 /* Arithmetic */
diff --git a/libs/libc/stdlib/CMakeLists.txt b/libs/libc/stdlib/CMakeLists.txt
index 3df38bf0df6..d489e2220f5 100644
--- a/libs/libc/stdlib/CMakeLists.txt
+++ b/libs/libc/stdlib/CMakeLists.txt
@@ -69,7 +69,14 @@ if(NOT CONFIG_LIBM_NONE)
 endif()
 
 if(CONFIG_PSEUDOTERM)
-  list(APPEND SRCS lib_ptsname.c lib_ptsnamer.c lib_unlockpt.c lib_openpty.c)
+  list(
+    APPEND
+    SRCS
+    lib_ptsname.c
+    lib_ptsnamer.c
+    lib_unlockpt.c
+    lib_openpty.c
+    lib_grantpt.c)
 endif()
 
 target_sources(c PRIVATE ${SRCS})
diff --git a/libs/libc/stdlib/Make.defs b/libs/libc/stdlib/Make.defs
index ae9d24db3fe..1fafdacaf83 100644
--- a/libs/libc/stdlib/Make.defs
+++ b/libs/libc/stdlib/Make.defs
@@ -38,6 +38,7 @@ endif
 
 ifeq ($(CONFIG_PSEUDOTERM),y)
 CSRCS += lib_ptsname.c lib_ptsnamer.c lib_unlockpt.c lib_openpty.c
+CSRCS += lib_grantpt.c
 endif
 
 # Add the stdlib directory to the build
diff --git a/libs/libc/stdlib/lib_grantpt.c b/libs/libc/stdlib/lib_grantpt.c
new file mode 100644
index 00000000000..dd9668588f9
--- /dev/null
+++ b/libs/libc/stdlib/lib_grantpt.c
@@ -0,0 +1,55 @@
+/****************************************************************************
+ * libs/libc/stdlib/lib_grantpt.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <sys/ioctl.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: grantpt
+ *
+ * Description:
+ *   Give the slave pseudoterminal of fd its owner and mode.  NuttX creates
+ *   the slave with them already, so this only checks that fd is a
+ *   pseudoterminal.
+ *
+ * Returned Value:
+ *   0 on success.  On failure, -1 with errno set by ioctl(): EBADF if fd
+ *   is not open, ENOTTY if fd is not a pseudoterminal.
+ *
+ ****************************************************************************/
+
+int grantpt(int fd)
+{
+  int ptyno;
+
+  return ioctl(fd, TIOCGPTN, (unsigned long)((uintptr_t)&ptyno)) < 0 ?
+         -1 : 0;
+}

Reply via email to