public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] Support regular file type in _swistat for ARM
@ 2020-06-23 10:30 Joe Ramsay
  2020-07-02  9:06 ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Ramsay @ 2020-06-23 10:30 UTC (permalink / raw)
  To: Jeff Johnston via Newlib

Hi!

Previously, the implementation of _swistat() for ARM assumed all files
to be of type character device. This change adds support for stat-ing
regular files. Tested with arm-none-eabi
---
libgloss/arm/syscalls.c        | 44 +++++++++++++++++++-----------
newlib/libc/sys/arm/syscalls.c | 61 ++++++++++++++++++++++++++++++++++++------
2 files changed, 82 insertions(+), 23 deletions(-)

diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c
index fc394f9..652301c 100644
--- a/libgloss/arm/syscalls.c
+++ b/libgloss/arm/syscalls.c
@@ -59,6 +59,7 @@ struct fdent
};
 #define MAX_OPEN_FILES 20
+#define DEFAULT_BLOCK_SIZE 1024
 /* User file descriptors (fd) are integer indexes into
    the openfiles[] array. Error checking is done by using
@@ -728,33 +729,46 @@ _sbrk (ptrdiff_t incr)
int
 _swistat (int fd, struct stat * st)
{
-  struct fdent *pfd;
-  int res;
-
-  pfd = findslot (fd);
+  struct fdent *pfd = findslot (fd);
   if (pfd == NULL)
     {
       errno = EBADF;
       return -1;
     }
-  /* Always assume a character device,
-     with 1024 byte blocks. */
-  st->st_mode |= S_IFCHR;
-  st->st_blksize = 1024;
+  int isCharDevice;
#ifdef ARM_RDI_MONITOR
-  res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
#else
   asm ("mov r0, %2; swi %a1; mov %0, r0"
-       : "=r" (res)
+       : "=r" (isCharDevice)
+       : "i" (SWI_IsTTY), "r" (pfd->handle)
+       : "r0");
+  checkerror(isCharDevice);
+#endif
+
+  int flen;
+#ifdef ARM_RDI_MONITOR
+  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (flen)
        : "i" (SWI_Flen), "r" (pfd->handle)
        : "r0");
-  checkerror (res);
+  checkerror(flen);
#endif
-  if (res == -1)
-    return -1;
-  /* Return the file size. */
-  st->st_size = res;
+
+  st->st_size = flen;
+  st->st_blksize = DEFAULT_BLOCK_SIZE;
+
+  if (isCharDevice)
+    st->st_mode |= S_IFCHR;
+  else
+    {
+      st->st_mode |= S_IFREG;
+      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
+    }
+
  return 0;
}
diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
index 1f72229..5156f1a 100644
--- a/newlib/libc/sys/arm/syscalls.c
+++ b/newlib/libc/sys/arm/syscalls.c
@@ -28,6 +28,7 @@ int               _unlink                 (const char *);
int          _link                      (const char *, const char *);
int          _stat                      (const char *, struct stat *);
int          _fstat                     (int, struct stat *);
+int     _swistat        (int fd, struct stat * st);
void *   _sbrk                     (ptrdiff_t);
pid_t     _getpid                 (void);
int          _kill                        (int, int);
@@ -84,6 +85,8 @@ poslog;
#define MAX_OPEN_FILES 20
static poslog openfiles [MAX_OPEN_FILES];
+#define DEFAULT_BLOCK_SIZE 1024
+
static int
findslot (int fh)
{
@@ -543,33 +546,75 @@ _sbrk (ptrdiff_t incr)
   return (void *) prev_heap_end;
}
+int
+_swistat (int fd, struct stat * st)
+{
+  struct fdent *pfd = findslot (fd);
+  if (pfd == NULL)
+    {
+      errno = EBADF;
+      return -1;
+    }
+
+  int isCharDevice;
+#ifdef ARM_RDI_MONITOR
+  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (isCharDevice)
+       : "i" (SWI_IsTTY), "r" (pfd->handle)
+       : "r0");
+  checkerror(isCharDevice);
+#endif
+
+  int flen;
+#ifdef ARM_RDI_MONITOR
+  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (flen)
+       : "i" (SWI_Flen), "r" (pfd->handle)
+       : "r0");
+  checkerror(flen);
+#endif
+
+  st->st_size = flen;
+  st->st_blksize = DEFAULT_BLOCK_SIZE;
+
+  if (isCharDevice)
+    st->st_mode |= S_IFCHR;
+  else
+    {
+      st->st_mode |= S_IFREG;
+      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
+    }
+
+  return 0;
+}
+
extern void memset (struct stat *, int, unsigned int);
 int __attribute__((weak))
_fstat (int file, struct stat * st)
{
   memset (st, 0, sizeof (* st));
-  st->st_mode = S_IFCHR;
-  st->st_blksize = 1024;
-  return 0;
-  file = file;
+  return _swistat (file, st);
}
 int __attribute__((weak))
_stat (const char *fname, struct stat *st)
{
   int file;
-
+  memset (st, 0, sizeof (* st));
   /* 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 ((file = _open (fname, O_RDONLY)) < 0)
     return -1;
-  memset (st, 0, sizeof (* st));
   st->st_mode = S_IFREG | S_IREAD;
-  st->st_blksize = 1024;
+  res = _swistat (file, st);
   _swiclose (file); /* Not interested in the error.  */
-  return 0;
+  return res;
}
 int __attribute__((weak))
--
2.7.4


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Support regular file type in _swistat for ARM
  2020-06-23 10:30 [PATCH v2] Support regular file type in _swistat for ARM Joe Ramsay
@ 2020-07-02  9:06 ` Corinna Vinschen
  0 siblings, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2020-07-02  9:06 UTC (permalink / raw)
  To: newlib; +Cc: Richard Earnshaw (lists)

CC'ing Richard.

On Jun 23 10:30, Joe Ramsay wrote:
> Hi!
> 
> Previously, the implementation of _swistat() for ARM assumed all files
> to be of type character device. This change adds support for stat-ing
> regular files. Tested with arm-none-eabi
> ---
> libgloss/arm/syscalls.c        | 44 +++++++++++++++++++-----------
> newlib/libc/sys/arm/syscalls.c | 61 ++++++++++++++++++++++++++++++++++++------
> 2 files changed, 82 insertions(+), 23 deletions(-)
> 
> diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c
> index fc394f9..652301c 100644
> --- a/libgloss/arm/syscalls.c
> +++ b/libgloss/arm/syscalls.c
> @@ -59,6 +59,7 @@ struct fdent
> };
>  #define MAX_OPEN_FILES 20
> +#define DEFAULT_BLOCK_SIZE 1024
>  /* User file descriptors (fd) are integer indexes into
>     the openfiles[] array. Error checking is done by using
> @@ -728,33 +729,46 @@ _sbrk (ptrdiff_t incr)
> int
>  _swistat (int fd, struct stat * st)
> {
> -  struct fdent *pfd;
> -  int res;
> -
> -  pfd = findslot (fd);
> +  struct fdent *pfd = findslot (fd);
>    if (pfd == NULL)
>      {
>        errno = EBADF;
>        return -1;
>      }
> -  /* Always assume a character device,
> -     with 1024 byte blocks. */
> -  st->st_mode |= S_IFCHR;
> -  st->st_blksize = 1024;
> +  int isCharDevice;
> #ifdef ARM_RDI_MONITOR
> -  res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
> +  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
> #else
>    asm ("mov r0, %2; swi %a1; mov %0, r0"
> -       : "=r" (res)
> +       : "=r" (isCharDevice)
> +       : "i" (SWI_IsTTY), "r" (pfd->handle)
> +       : "r0");
> +  checkerror(isCharDevice);
> +#endif
> +
> +  int flen;
> +#ifdef ARM_RDI_MONITOR
> +  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
> +#else
> +  asm ("mov r0, %2; swi %a1; mov %0, r0"
> +       : "=r" (flen)
>         : "i" (SWI_Flen), "r" (pfd->handle)
>         : "r0");
> -  checkerror (res);
> +  checkerror(flen);
> #endif
> -  if (res == -1)
> -    return -1;
> -  /* Return the file size. */
> -  st->st_size = res;
> +
> +  st->st_size = flen;
> +  st->st_blksize = DEFAULT_BLOCK_SIZE;
> +
> +  if (isCharDevice)
> +    st->st_mode |= S_IFCHR;
> +  else
> +    {
> +      st->st_mode |= S_IFREG;
> +      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
> +    }
> +
>   return 0;
> }
> diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
> index 1f72229..5156f1a 100644
> --- a/newlib/libc/sys/arm/syscalls.c
> +++ b/newlib/libc/sys/arm/syscalls.c
> @@ -28,6 +28,7 @@ int               _unlink                 (const char *);
> int          _link                      (const char *, const char *);
> int          _stat                      (const char *, struct stat *);
> int          _fstat                     (int, struct stat *);
> +int     _swistat        (int fd, struct stat * st);
> void *   _sbrk                     (ptrdiff_t);
> pid_t     _getpid                 (void);
> int          _kill                        (int, int);
> @@ -84,6 +85,8 @@ poslog;
> #define MAX_OPEN_FILES 20
> static poslog openfiles [MAX_OPEN_FILES];
> +#define DEFAULT_BLOCK_SIZE 1024
> +
> static int
> findslot (int fh)
> {
> @@ -543,33 +546,75 @@ _sbrk (ptrdiff_t incr)
>    return (void *) prev_heap_end;
> }
> +int
> +_swistat (int fd, struct stat * st)
> +{
> +  struct fdent *pfd = findslot (fd);
> +  if (pfd == NULL)
> +    {
> +      errno = EBADF;
> +      return -1;
> +    }
> +
> +  int isCharDevice;
> +#ifdef ARM_RDI_MONITOR
> +  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
> +#else
> +  asm ("mov r0, %2; swi %a1; mov %0, r0"
> +       : "=r" (isCharDevice)
> +       : "i" (SWI_IsTTY), "r" (pfd->handle)
> +       : "r0");
> +  checkerror(isCharDevice);
> +#endif
> +
> +  int flen;
> +#ifdef ARM_RDI_MONITOR
> +  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
> +#else
> +  asm ("mov r0, %2; swi %a1; mov %0, r0"
> +       : "=r" (flen)
> +       : "i" (SWI_Flen), "r" (pfd->handle)
> +       : "r0");
> +  checkerror(flen);
> +#endif
> +
> +  st->st_size = flen;
> +  st->st_blksize = DEFAULT_BLOCK_SIZE;
> +
> +  if (isCharDevice)
> +    st->st_mode |= S_IFCHR;
> +  else
> +    {
> +      st->st_mode |= S_IFREG;
> +      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
> +    }
> +
> +  return 0;
> +}
> +
> extern void memset (struct stat *, int, unsigned int);
>  int __attribute__((weak))
> _fstat (int file, struct stat * st)
> {
>    memset (st, 0, sizeof (* st));
> -  st->st_mode = S_IFCHR;
> -  st->st_blksize = 1024;
> -  return 0;
> -  file = file;
> +  return _swistat (file, st);
> }
>  int __attribute__((weak))
> _stat (const char *fname, struct stat *st)
> {
>    int file;
> -
> +  memset (st, 0, sizeof (* st));
>    /* 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 ((file = _open (fname, O_RDONLY)) < 0)
>      return -1;
> -  memset (st, 0, sizeof (* st));
>    st->st_mode = S_IFREG | S_IREAD;
> -  st->st_blksize = 1024;
> +  res = _swistat (file, st);
>    _swiclose (file); /* Not interested in the error.  */
> -  return 0;
> +  return res;
> }
>  int __attribute__((weak))
> --
> 2.7.4
> 

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Support regular file type in _swistat for ARM
  2020-08-18 13:07   ` Joe Ramsay
@ 2020-08-18 13:54     ` Corinna Vinschen
  0 siblings, 0 replies; 6+ messages in thread
From: Corinna Vinschen @ 2020-08-18 13:54 UTC (permalink / raw)
  To: Joe Ramsay; +Cc: newlib, Richard Earnshaw, Szabolcs Nagy


Cc Szabolcs


On Aug 18 13:07, Joe Ramsay wrote:
> Apologies, full patch is attached here.
> 
> On 18/08/2020, 14:00, "Corinna Vinschen" <vinschen@redhat.com> wrote:
> 
>     On Aug 18 09:49, Joe Ramsay wrote:
>     > Hi all,
>     > 
>     > Just pinging this patch (original at
>     > https://sourceware.org/pipermail/newlib/2020/017719.html), as it looks
>     > to have fallen through the cracks somewhat. I’ve attached the patch
> 
>     Was still waiting for a review from Richard who reviewd v1, see
>     https://sourceware.org/pipermail/newlib/2020/017727.html
> 
>     > file directly, as I think it may have been corrupted in my original
> 
>     Please attach a git patch, not a naked diff.
> 
> 
>     Thanks,
>     Corinna
> 
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Support regular file type in _swistat for ARM
  2020-08-18 12:59 ` Corinna Vinschen
@ 2020-08-18 13:07   ` Joe Ramsay
  2020-08-18 13:54     ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Ramsay @ 2020-08-18 13:07 UTC (permalink / raw)
  To: newlib; +Cc: Richard Earnshaw, Szabolcs Nagy

[-- Attachment #1: Type: text/plain, Size: 677 bytes --]

Apologies, full patch is attached here.

On 18/08/2020, 14:00, "Corinna Vinschen" <vinschen@redhat.com> wrote:

    On Aug 18 09:49, Joe Ramsay wrote:
    > Hi all,
    > 
    > Just pinging this patch (original at
    > https://sourceware.org/pipermail/newlib/2020/017719.html), as it looks
    > to have fallen through the cracks somewhat. I’ve attached the patch

    Was still waiting for a review from Richard who reviewd v1, see
    https://sourceware.org/pipermail/newlib/2020/017727.html

    > file directly, as I think it may have been corrupted in my original

    Please attach a git patch, not a naked diff.


    Thanks,
    Corinna



[-- Attachment #2: 0001-Support-regular-file-type-in-_swistat.patch --]
[-- Type: application/octet-stream, Size: 5073 bytes --]

From 967c45d40709f873f84860f43700f1d2d25c0551 Mon Sep 17 00:00:00 2001
From: Joe Ramsay <Joe.Ramsay@arm.com>
Date: Tue, 18 Aug 2020 13:04:24 +0000
Subject: [PATCH] Support regular file type in _swistat

Previously, the implementation of _swistat() for ARM assumed all files
to be of type character device. This change adds support for stat-ing
regular files
---
 libgloss/arm/syscalls.c        | 44 +++++++++++++++++++-----------
 newlib/libc/sys/arm/syscalls.c | 61 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 82 insertions(+), 23 deletions(-)

diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c
index fc394f94bda37dfb2dfc2d12a5dd923391b9e025..652301cef67489d4b85bd009c14713df29d77d5e 100644
--- a/libgloss/arm/syscalls.c
+++ b/libgloss/arm/syscalls.c
@@ -59,6 +59,7 @@ struct fdent
 };
 
 #define MAX_OPEN_FILES 20
+#define DEFAULT_BLOCK_SIZE 1024
 
 /* User file descriptors (fd) are integer indexes into 
    the openfiles[] array. Error checking is done by using
@@ -728,33 +729,46 @@ _sbrk (ptrdiff_t incr)
 int 
 _swistat (int fd, struct stat * st)
 {
-  struct fdent *pfd;
-  int res;
-
-  pfd = findslot (fd);
+  struct fdent *pfd = findslot (fd);
   if (pfd == NULL)
     {
       errno = EBADF;
       return -1;
     }
 
-  /* Always assume a character device,
-     with 1024 byte blocks. */
-  st->st_mode |= S_IFCHR;
-  st->st_blksize = 1024;
+  int isCharDevice;
 #ifdef ARM_RDI_MONITOR
-  res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
 #else
   asm ("mov r0, %2; swi %a1; mov %0, r0"
-       : "=r" (res)
+       : "=r" (isCharDevice)
+       : "i" (SWI_IsTTY), "r" (pfd->handle)
+       : "r0");
+  checkerror(isCharDevice);
+#endif
+
+  int flen;
+#ifdef ARM_RDI_MONITOR
+  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (flen)
        : "i" (SWI_Flen), "r" (pfd->handle)
        : "r0");
-  checkerror (res);
+  checkerror(flen);
 #endif
-  if (res == -1)
-    return -1;
-  /* Return the file size. */
-  st->st_size = res;
+
+  st->st_size = flen;
+  st->st_blksize = DEFAULT_BLOCK_SIZE;
+
+  if (isCharDevice)
+    st->st_mode |= S_IFCHR;
+  else
+    {
+      st->st_mode |= S_IFREG;
+      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
+    }
+
   return 0;
 }
 
diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
index 1f722298024885d04691a1af1ff509318b36b7a6..5156f1ac81663fffa9b8286133f43181d6f16852 100644
--- a/newlib/libc/sys/arm/syscalls.c
+++ b/newlib/libc/sys/arm/syscalls.c
@@ -28,6 +28,7 @@ int	_unlink		(const char *);
 int	_link		(const char *, const char *);
 int	_stat		(const char *, struct stat *);
 int	_fstat		(int, struct stat *);
+int     _swistat        (int fd, struct stat * st);
 void *	_sbrk		(ptrdiff_t);
 pid_t	_getpid		(void);
 int	_kill		(int, int);
@@ -84,6 +85,8 @@ poslog;
 #define MAX_OPEN_FILES 20
 static poslog openfiles [MAX_OPEN_FILES];
 
+#define DEFAULT_BLOCK_SIZE 1024
+
 static int
 findslot (int fh)
 {
@@ -543,33 +546,75 @@ _sbrk (ptrdiff_t incr)
   return (void *) prev_heap_end;
 }
 
+int 
+_swistat (int fd, struct stat * st)
+{
+  struct fdent *pfd = findslot (fd);
+  if (pfd == NULL)
+    {
+      errno = EBADF;
+      return -1;
+    }
+
+  int isCharDevice;
+#ifdef ARM_RDI_MONITOR
+  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (isCharDevice)
+       : "i" (SWI_IsTTY), "r" (pfd->handle)
+       : "r0");
+  checkerror(isCharDevice);
+#endif
+
+  int flen;
+#ifdef ARM_RDI_MONITOR
+  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (flen)
+       : "i" (SWI_Flen), "r" (pfd->handle)
+       : "r0");
+  checkerror(flen);
+#endif
+
+  st->st_size = flen;
+  st->st_blksize = DEFAULT_BLOCK_SIZE;
+
+  if (isCharDevice)
+    st->st_mode |= S_IFCHR;
+  else
+    {
+      st->st_mode |= S_IFREG;
+      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
+    }
+
+  return 0;
+}
+
 extern void memset (struct stat *, int, unsigned int);
 
 int __attribute__((weak))
 _fstat (int file, struct stat * st)
 {
   memset (st, 0, sizeof (* st));
-  st->st_mode = S_IFCHR;
-  st->st_blksize = 1024;
-  return 0;
-  file = file;
+  return _swistat (file, st);
 }
 
 int __attribute__((weak))
 _stat (const char *fname, struct stat *st)
 {
   int file;
-
+  memset (st, 0, sizeof (* st));
   /* 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 ((file = _open (fname, O_RDONLY)) < 0)
     return -1;
 
-  memset (st, 0, sizeof (* st));
   st->st_mode = S_IFREG | S_IREAD;
-  st->st_blksize = 1024;
+  res = _swistat (file, st);
   _swiclose (file); /* Not interested in the error.  */
-  return 0;
+  return res;
 }
 
 int __attribute__((weak))
-- 
2.7.4


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Support regular file type in _swistat for ARM
  2020-08-18  9:49 Joe Ramsay
@ 2020-08-18 12:59 ` Corinna Vinschen
  2020-08-18 13:07   ` Joe Ramsay
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2020-08-18 12:59 UTC (permalink / raw)
  To: Joe Ramsay; +Cc: newlib, Richard.Earnshaw, Szabolcs Nagy

On Aug 18 09:49, Joe Ramsay wrote:
> Hi all,
> 
> Just pinging this patch (original at
> https://sourceware.org/pipermail/newlib/2020/017719.html), as it looks
> to have fallen through the cracks somewhat. I’ve attached the patch

Was still waiting for a review from Richard who reviewd v1, see
https://sourceware.org/pipermail/newlib/2020/017727.html

> file directly, as I think it may have been corrupted in my original

Please attach a git patch, not a naked diff.


Thanks,
Corinna


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] Support regular file type in _swistat for ARM
@ 2020-08-18  9:49 Joe Ramsay
  2020-08-18 12:59 ` Corinna Vinschen
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Ramsay @ 2020-08-18  9:49 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 447 bytes --]

Hi all,

Just pinging this patch (original at https://sourceware.org/pipermail/newlib/2020/017719.html), as it looks to have fallen through the cracks somewhat. I’ve attached the patch file directly, as I think it may have been corrupted in my original email. If anyone is interested in this then a review would be really appreciated – if it’s OK for master then please commit on my behalf as I don’t have write access.

Thanks,
Joe

[-- Attachment #2: p.diff --]
[-- Type: application/octet-stream, Size: 4367 bytes --]

diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c
index fc394f9..652301c 100644
--- a/libgloss/arm/syscalls.c
+++ b/libgloss/arm/syscalls.c
@@ -59,6 +59,7 @@ struct fdent
 };
 
 #define MAX_OPEN_FILES 20
+#define DEFAULT_BLOCK_SIZE 1024
 
 /* User file descriptors (fd) are integer indexes into 
    the openfiles[] array. Error checking is done by using
@@ -728,33 +729,46 @@ _sbrk (ptrdiff_t incr)
 int 
 _swistat (int fd, struct stat * st)
 {
-  struct fdent *pfd;
-  int res;
-
-  pfd = findslot (fd);
+  struct fdent *pfd = findslot (fd);
   if (pfd == NULL)
     {
       errno = EBADF;
       return -1;
     }
 
-  /* Always assume a character device,
-     with 1024 byte blocks. */
-  st->st_mode |= S_IFCHR;
-  st->st_blksize = 1024;
+  int isCharDevice;
 #ifdef ARM_RDI_MONITOR
-  res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
 #else
   asm ("mov r0, %2; swi %a1; mov %0, r0"
-       : "=r" (res)
+       : "=r" (isCharDevice)
+       : "i" (SWI_IsTTY), "r" (pfd->handle)
+       : "r0");
+  checkerror(isCharDevice);
+#endif
+
+  int flen;
+#ifdef ARM_RDI_MONITOR
+  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (flen)
        : "i" (SWI_Flen), "r" (pfd->handle)
        : "r0");
-  checkerror (res);
+  checkerror(flen);
 #endif
-  if (res == -1)
-    return -1;
-  /* Return the file size. */
-  st->st_size = res;
+
+  st->st_size = flen;
+  st->st_blksize = DEFAULT_BLOCK_SIZE;
+
+  if (isCharDevice)
+    st->st_mode |= S_IFCHR;
+  else
+    {
+      st->st_mode |= S_IFREG;
+      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
+    }
+
   return 0;
 }
 
diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
index 1f72229..5156f1a 100644
--- a/newlib/libc/sys/arm/syscalls.c
+++ b/newlib/libc/sys/arm/syscalls.c
@@ -28,6 +28,7 @@ int	_unlink		(const char *);
 int	_link		(const char *, const char *);
 int	_stat		(const char *, struct stat *);
 int	_fstat		(int, struct stat *);
+int     _swistat        (int fd, struct stat * st);
 void *	_sbrk		(ptrdiff_t);
 pid_t	_getpid		(void);
 int	_kill		(int, int);
@@ -84,6 +85,8 @@ poslog;
 #define MAX_OPEN_FILES 20
 static poslog openfiles [MAX_OPEN_FILES];
 
+#define DEFAULT_BLOCK_SIZE 1024
+
 static int
 findslot (int fh)
 {
@@ -543,33 +546,75 @@ _sbrk (ptrdiff_t incr)
   return (void *) prev_heap_end;
 }
 
+int 
+_swistat (int fd, struct stat * st)
+{
+  struct fdent *pfd = findslot (fd);
+  if (pfd == NULL)
+    {
+      errno = EBADF;
+      return -1;
+    }
+
+  int isCharDevice;
+#ifdef ARM_RDI_MONITOR
+  isCharDevice = checkerror (do_AngelSWI (AngelSWI_Reason_IsTTY, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (isCharDevice)
+       : "i" (SWI_IsTTY), "r" (pfd->handle)
+       : "r0");
+  checkerror(isCharDevice);
+#endif
+
+  int flen;
+#ifdef ARM_RDI_MONITOR
+  flen = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+#else
+  asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (flen)
+       : "i" (SWI_Flen), "r" (pfd->handle)
+       : "r0");
+  checkerror(flen);
+#endif
+
+  st->st_size = flen;
+  st->st_blksize = DEFAULT_BLOCK_SIZE;
+
+  if (isCharDevice)
+    st->st_mode |= S_IFCHR;
+  else
+    {
+      st->st_mode |= S_IFREG;
+      st->st_blocks = (flen + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
+    }
+
+  return 0;
+}
+
 extern void memset (struct stat *, int, unsigned int);
 
 int __attribute__((weak))
 _fstat (int file, struct stat * st)
 {
   memset (st, 0, sizeof (* st));
-  st->st_mode = S_IFCHR;
-  st->st_blksize = 1024;
-  return 0;
-  file = file;
+  return _swistat (file, st);
 }
 
 int __attribute__((weak))
 _stat (const char *fname, struct stat *st)
 {
   int file;
-
+  memset (st, 0, sizeof (* st));
   /* 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 ((file = _open (fname, O_RDONLY)) < 0)
     return -1;
 
-  memset (st, 0, sizeof (* st));
   st->st_mode = S_IFREG | S_IREAD;
-  st->st_blksize = 1024;
+  res = _swistat (file, st);
   _swiclose (file); /* Not interested in the error.  */
-  return 0;
+  return res;
 }
 
 int __attribute__((weak))

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-08-18 13:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23 10:30 [PATCH v2] Support regular file type in _swistat for ARM Joe Ramsay
2020-07-02  9:06 ` Corinna Vinschen
2020-08-18  9:49 Joe Ramsay
2020-08-18 12:59 ` Corinna Vinschen
2020-08-18 13:07   ` Joe Ramsay
2020-08-18 13:54     ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).