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 4fe3305f14390dd38ad701ae5a7a1384a311b428
Author: Marco Casaroli <[email protected]>
AuthorDate: Sat Oct 17 19:51:50 2026 +0200

    libs/libc/stdio: Add ctermid().
    
    POSIX declares ctermid() and L_ctermid in <stdio.h>, but NuttX did
    not have them.  Programs that open the controlling terminal with
    ctermid() did not link.
    
    This commit adds ctermid().  It returns "/dev/console", the terminal
    of every task on NuttX.  The function copies the path into the buffer
    of the caller, or into a static buffer when the argument is NULL.
    L_ctermid gives the size of the buffer.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/cxx/cstdio             |  4 +++
 include/stdio.h                |  8 +++++
 libs/libc/libc.csv             |  1 +
 libs/libc/stdio/CMakeLists.txt |  3 +-
 libs/libc/stdio/Make.defs      |  2 +-
 libs/libc/stdio/lib_ctermid.c  | 74 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/include/cxx/cstdio b/include/cxx/cstdio
index 93d06254b0e..459a4f20463 100644
--- a/include/cxx/cstdio
+++ b/include/cxx/cstdio
@@ -95,6 +95,10 @@ namespace std
   using ::tmpnam;
   using ::tempnam;
   using ::remove;
+
+  // Controlling terminal
+
+  using ::ctermid;
 }
 
 #endif // __INCLUDE_CXX_CSTDIO
diff --git a/include/stdio.h b/include/stdio.h
index 0311f96bffb..00487311ee9 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -101,6 +101,10 @@
 
 #define L_tmpnam   CONFIG_LIBC_MAX_TMPFILE
 
+/* Size of character array to hold ctermid() output: "/dev/console" */
+
+#define L_ctermid  13
+
 /* The maximum number of unique temporary file names that can be generated */
 
 #define TMP_MAX 308915776
@@ -367,6 +371,10 @@ FAR char *tmpnam(FAR char *s);
 FAR char *tempnam(FAR const char *dir, FAR const char *pfx) malloc_like;
 int       remove(FAR const char *path);
 
+/* Controlling terminal */
+
+FAR char *ctermid(FAR char *s);
+
 /* Shell operations.  These are not actually implemented in the OS.  See
  * apps/system/popen for implementation.
  */
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index da894be284f..1cceb425198 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -37,6 +37,7 @@
 "closedir","dirent.h","","int","DIR *"
 "crc32","nuttx/crc32.h","","uint32_t","FAR const uint8_t *","size_t"
 "crc32part","nuttx/crc32.h","","uint32_t","FAR const uint8_t 
*","size_t","uint32_t"
+"ctermid","stdio.h","","FAR char *","FAR char *"
 "ctime","time.h","","char *","const time_t *"
 "crypt","unistd.h","defined(CONFIG_CRYPTO)","FAR const char *","FAR const char 
*"
 "crypt_r","unistd.h","defined(CONFIG_CRYPTO)","FAR const char *","FAR const 
char *","FAR char *"
diff --git a/libs/libc/stdio/CMakeLists.txt b/libs/libc/stdio/CMakeLists.txt
index 8d2982192aa..47acb2cde28 100644
--- a/libs/libc/stdio/CMakeLists.txt
+++ b/libs/libc/stdio/CMakeLists.txt
@@ -47,7 +47,8 @@ set(SRCS
     lib_tempnam.c
     lib_tmpnam.c
     lib_renameat.c
-    lib_putwchar.c)
+    lib_putwchar.c
+    lib_ctermid.c)
 
 # The remaining sources files depend upon C streams
 
diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs
index af412a2c915..99bdca23818 100644
--- a/libs/libc/stdio/Make.defs
+++ b/libs/libc/stdio/Make.defs
@@ -29,7 +29,7 @@ CSRCS += lib_vsnprintf.c lib_dprintf.c lib_vdprintf.c 
lib_vprintf.c
 CSRCS += lib_perror.c lib_putchar.c lib_getchar.c lib_puts.c
 CSRCS += lib_gets_s.c lib_gets.c lib_libdgets.c lib_sscanf.c
 CSRCS += lib_vsscanf.c lib_remove.c lib_tempnam.c lib_tmpnam.c
-CSRCS += lib_renameat.c lib_putwchar.c
+CSRCS += lib_renameat.c lib_putwchar.c lib_ctermid.c
 
 # The remaining sources files depend upon C streams
 
diff --git a/libs/libc/stdio/lib_ctermid.c b/libs/libc/stdio/lib_ctermid.c
new file mode 100644
index 00000000000..d4aafe631e2
--- /dev/null
+++ b/libs/libc/stdio/lib_ctermid.c
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * libs/libc/stdio/lib_ctermid.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 <nuttx/config.h>
+
+#include <stdio.h>
+#include <string.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The controlling terminal of every task.  L_ctermid in stdio.h is the
+ * size of this string.
+ */
+
+#define CTERMID_PATH "/dev/console"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ctermid
+ *
+ * Description:
+ *   The ctermid() function generates a string that is the pathname of the
+ *   controlling terminal of the current process.  On NuttX this is always
+ *   the system console.
+ *
+ *   If s is a null pointer, ctermid() writes the string into an internal
+ *   static object and returns a pointer to it.  Otherwise s must point to
+ *   an array of at least L_ctermid bytes; ctermid() writes the string into
+ *   that array and returns s.
+ *
+ * Returned Value:
+ *   A pointer to the pathname of the controlling terminal.
+ *
+ ****************************************************************************/
+
+FAR char *ctermid(FAR char *s)
+{
+  static char path[sizeof(CTERMID_PATH)];
+
+  if (s == NULL)
+    {
+      s = path;
+    }
+
+  return strcpy(s, CTERMID_PATH);
+}

Reply via email to