[PATCH v2 3/3] libgloss/aarch64: report semihosted file types in stat calls

Torbjörn SVENSSON torbjorn.svensson@foss.st.com
Thu Oct 8 18:01:31 GMT 2026


Infer file types from the semihosting operations available. Use IsTTY
to identify character devices, the FLEN operation to identify regular
files and obtain their sizes, and a "path/." probe to recognize
directories in _stat.

Tested by compiling for AArch64; runtime testing has not been performed.

Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
---
 libgloss/aarch64/syscalls.c | 77 +++++++++++++++++++++++++++++++++----
 1 file changed, 70 insertions(+), 7 deletions(-)

diff --git a/libgloss/aarch64/syscalls.c b/libgloss/aarch64/syscalls.c
index 5b4071893..982470eef 100644
--- a/libgloss/aarch64/syscalls.c
+++ b/libgloss/aarch64/syscalls.c
@@ -40,6 +40,7 @@
 #include <reent.h>
 #include <unistd.h>
 #include <sys/wait.h>
+#include <limits.h>
 #include "svc.h"
 
 /* Safe casting in both LP64 and ILP32.  */
@@ -75,6 +76,7 @@ void initialise_monitor_handles (void);
 static int checkerror (int);
 static int error (int);
 static int get_errno (void);
+static int path_is_dir (const char *);
 
 /* Semihosting utilities.  */
 static void initialise_semihosting_exts (void);
@@ -344,6 +346,46 @@ checkerror (int result)
   return result;
 }
 
+/* Check if the given path is likely a directory. */
+static int
+path_is_dir (const char *path)
+{
+  size_t path_len;
+  size_t dir_len;
+  char dir_path[PATH_MAX + 3];
+  int fd;
+
+  path_len = strlen (path);
+  if (path_len == 0)
+    return 0;
+
+  if (path_len + 3 > sizeof (dir_path))
+    {
+      errno = ENAMETOOLONG;
+      return -1;
+    }
+
+  /* Build a new string that ends with "/.". */
+  memcpy (dir_path, path, path_len);
+  dir_len = path_len;
+  if (dir_path[dir_len - 1] != '/')
+    dir_path[dir_len++] = '/';
+  dir_path[dir_len++] = '.';
+  dir_path[dir_len] = '\0';
+
+  /* Try to open the directory. */
+  fd = _open (dir_path, O_RDONLY);
+
+  /* Error means that path either does not exist
+   * or is not a directory. */
+  if (fd == -1)
+    return 0;
+
+  /* Not interested in the error.  */
+  _close (fd);
+  return 1;
+}
+
 /* fh, is a valid internal file handle.
    ptr, is a null terminated string.
    len, is the length in bytes to read.
@@ -680,18 +722,27 @@ _swistat (int fd, struct stat *st)
   if (res)
     {
       /* This is a tty. */
-      st->st_mode |= S_IFCHR;
+      st->st_mode = S_IFCHR;
     }
   else
     {
-      /* This is a file, return the file length.  */
-      st->st_mode |= S_IFREG;
       res = checkerror (do_AngelSVC (AngelSVC_Reason_FLen, param));
       if (res == -1)
-	return -1;
-      st->st_size = res;
-      st->st_blksize = 1024;
-      st->st_blocks = (res + 1023) / 1024;
+	{
+	  if (errno != EISDIR)
+	    return -1;
+
+	  /* This is a directory. */
+	  st->st_mode = S_IFDIR;
+	}
+      else
+	{
+	  /* This is a file, return the file length.  */
+	  st->st_mode = S_IFREG;
+	  st->st_size = res;
+	  st->st_blksize = 1024;
+	  st->st_blocks = (res + 1023) / 1024;
+	}
     }
 
   /* Deduce permissions based on mode in which file opened.  */
@@ -714,6 +765,18 @@ int __attribute__((weak))
 _stat (const char *fname, struct stat *st)
 {
   int fd, res;
+
+  res = path_is_dir (fname);
+  if (res == 1)
+    {
+      memset (st, 0, sizeof (*st));
+      st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
+      return 0;
+    }
+  else if (res == -1)
+    /* errno is already set. */
+    return -1;
+
   /* The best we can do is try to open the file readonly.
      If it exists, then we can guess a few things about it. */
   if ((fd = _open (fname, O_RDONLY)) == -1)
-- 
2.43.0



More information about the Newlib mailing list