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

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

commit 6085d86c0ba666185fff1e334e8468a2e1e20a35
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Oct 23 14:24:45 2026 +0200

    libs/libc/netdb: Add hstrerror() and herror().
    
    gethostbyname() and res_query() give their errors in h_errno, but the C
    library had no function that gives the message for an h_errno value.
    Programs from other systems call hstrerror() or herror() for this and
    did not compile.  hstrerror() gives the message for HOST_NOT_FOUND,
    NO_DATA, NO_RECOVERY and TRY_AGAIN, with the texts of other C libraries.
    herror() writes the message for h_errno to stderr.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/netdb.h                |   3 ++
 libs/libc/libc.csv             |   2 +
 libs/libc/netdb/CMakeLists.txt |   1 +
 libs/libc/netdb/Make.defs      |   2 +-
 libs/libc/netdb/lib_herror.c   | 113 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 120 insertions(+), 1 deletion(-)

diff --git a/include/netdb.h b/include/netdb.h
index ba8379fcf8c..cf4bb7bddf3 100644
--- a/include/netdb.h
+++ b/include/netdb.h
@@ -333,6 +333,9 @@ void                 setservent(int);
 
 /* Non-standard interfaces similar to Glibc 2 interfaces */
 
+void herror(FAR const char *s);
+FAR const char *hstrerror(int errnum);
+
 int gethostbyaddr_r(FAR const void *addr, socklen_t len, int type,
                     FAR struct hostent *host, FAR char *buf,
                     size_t buflen, FAR struct hostent **result,
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index d190764acb7..078726d6fcf 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -126,6 +126,8 @@
 "getwc","wchar.h","defined(CONFIG_FILE_STREAM)","wint_t","FAR FILE *"
 "gmtime","time.h","","FAR struct tm *","FAR const time_t *"
 "gmtime_r","time.h","","FAR struct tm *","FAR const time_t *","FAR struct tm *"
+"herror","netdb.h","defined(CONFIG_LIBC_NETDB)","void","FAR const char *"
+"hstrerror","netdb.h","defined(CONFIG_LIBC_NETDB)","FAR const char *","int"
 "htonl","arpa/inet.h","","uint32_t","uint32_t"
 "htonq","arpa/inet.h","","uint64_t","uint64_t"
 "htons","arpa/inet.h","","uint16_t","uint16_t"
diff --git a/libs/libc/netdb/CMakeLists.txt b/libs/libc/netdb/CMakeLists.txt
index a909195839c..4c4666a7236 100644
--- a/libs/libc/netdb/CMakeLists.txt
+++ b/libs/libc/netdb/CMakeLists.txt
@@ -41,6 +41,7 @@ if(CONFIG_LIBC_NETDB)
     lib_getservbyport.c
     lib_getservbyportr.c
     lib_gaistrerror.c
+    lib_herror.c
     lib_freeaddrinfo.c
     lib_getaddrinfo.c
     lib_getnameinfo.c
diff --git a/libs/libc/netdb/Make.defs b/libs/libc/netdb/Make.defs
index 1e3da679157..c47d80d6693 100644
--- a/libs/libc/netdb/Make.defs
+++ b/libs/libc/netdb/Make.defs
@@ -30,7 +30,7 @@ CSRCS += lib_gethostentbynamer.c
 CSRCS += lib_gethostbyaddr.c lib_gethostbyaddrr.c
 CSRCS += lib_getservbyname.c lib_getservbynamer.c
 CSRCS += lib_getservbyport.c lib_getservbyportr.c
-CSRCS += lib_gaistrerror.c lib_freeaddrinfo.c lib_getaddrinfo.c
+CSRCS += lib_gaistrerror.c lib_herror.c lib_freeaddrinfo.c lib_getaddrinfo.c
 CSRCS += lib_getnameinfo.c lib_rexec.c lib_dn.c lib_resmkquery.c
 CSRCS += lib_proto.c lib_protor.c
 
diff --git a/libs/libc/netdb/lib_herror.c b/libs/libc/netdb/lib_herror.c
new file mode 100644
index 00000000000..e083586ff69
--- /dev/null
+++ b/libs/libc/netdb/lib_herror.c
@@ -0,0 +1,113 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_herror.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 <unistd.h>
+#include <netdb.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: hstrerror
+ *
+ * Description:
+ *   Give the message for an h_errno value: HOST_NOT_FOUND, NO_DATA,
+ *   NO_RECOVERY or TRY_AGAIN.  The messages are the same as in other C
+ *   libraries.
+ *
+ * Input Parameters:
+ *   errnum - The h_errno value.
+ *
+ * Returned Value:
+ *   A pointer to a constant string.  It is never NULL.
+ *
+ ****************************************************************************/
+
+FAR const char *hstrerror(int errnum)
+{
+  switch (errnum)
+    {
+      case 0:
+        return "Resolver Error 0 (no error)";
+
+      case HOST_NOT_FOUND:
+        return "Unknown host";
+
+      case NO_DATA:
+        return "No address associated with name";
+
+      case NO_RECOVERY:
+        return "Unknown server error";
+
+      case TRY_AGAIN:
+        return "Host name lookup failure";
+
+      default:
+        return "Unknown resolver error";
+    }
+}
+
+/****************************************************************************
+ * Name: herror
+ *
+ * Description:
+ *   Write the message for the current h_errno value to stderr.  When s is
+ *   not NULL and not empty, the message starts with s and ": ".
+ *
+ * Input Parameters:
+ *   s - The text in front of the message, or NULL.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+void herror(FAR const char *s)
+{
+#ifdef CONFIG_FILE_STREAM
+  if (s != NULL && *s != '\0')
+    {
+      fprintf(stderr, "%s: %s\n", s, hstrerror(h_errno));
+    }
+  else
+    {
+      fprintf(stderr, "%s\n", hstrerror(h_errno));
+    }
+#else
+  if (s != NULL && *s != '\0')
+    {
+      dprintf(STDERR_FILENO, "%s: %s\n", s, hstrerror(h_errno));
+    }
+  else
+    {
+      dprintf(STDERR_FILENO, "%s\n", hstrerror(h_errno));
+    }
+#endif
+}

Reply via email to