Changes for 4.5.0 snapshot
[newlib-cygwin.git] / libgloss / arc / hl / hl_fstat.c
blob0a49eece083ad5158e41277b93d35f7fa6627a1e
1 /*
2 * hl_fstat.c -- provide _fstat().
4 * Copyright (c) 2024 Synopsys Inc.
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice is included verbatim in any distributions. No written agreement,
10 * license, or royalty fee is required for any of the authorized uses.
11 * Modifications to this software may be copyrighted by their authors
12 * and need not follow the licensing terms described here, provided that
13 * the new terms are clearly indicated on the first page of each file where
14 * they apply.
18 #include <errno.h>
19 #include <stdarg.h>
20 #include <stdint.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
25 #include "hl_toolchain.h"
26 #include "hl_api.h"
28 /* Hostlink IO version of struct stat. */
29 struct _hl_stat {
30 uint32_t hl_dev; /* ID of device containing file. */
31 uint16_t hl_ino; /* inode number. */
32 uint16_t hl_mode; /* File type and access mode. */
33 int16_t hl_nlink; /* Number of hard links. */
34 int16_t hl_uid; /* Owner's UID. */
35 int16_t hl_gid; /* Owner's GID. */
36 uint8_t hl_pad[2]; /* Padding to match simulator struct. */
37 uint32_t hl_rdev; /* Device ID (if special file). */
38 int32_t hl_size; /* Size in bytes. */
39 int32_t hl_atime; /* Access time. */
40 int32_t hl_mtime; /* Modification time. */
41 int32_t hl_ctime; /* Creation time. */
42 } __packed;
44 /* Map Hostlink version of stat struct into newlib's one. */
45 static __always_inline void
46 _hl_fstat_map (const struct _hl_stat *hl_stat, struct stat *stat)
48 stat->st_dev = hl_stat->hl_dev;
49 stat->st_ino = hl_stat->hl_ino;
50 stat->st_mode = hl_stat->hl_mode;
51 stat->st_nlink = hl_stat->hl_nlink;
52 stat->st_uid = hl_stat->hl_uid;
53 stat->st_gid = hl_stat->hl_gid;
54 stat->st_rdev = hl_stat->hl_rdev;
55 stat->st_size = hl_stat->hl_size;
56 stat->st_atime = hl_stat->hl_atime;
57 stat->st_mtime = hl_stat->hl_mtime;
58 stat->st_ctime = hl_stat->hl_ctime;
61 /* Get host file info. Implements HL_GNUIO_EXT_FSTAT. */
62 static __always_inline int
63 _hl_fstat (int fd, struct stat *buf)
65 struct _hl_stat hl_stat;
66 int32_t ret;
67 uint32_t host_errno;
69 /* Special version of hostlink - retuned values are passed
70 * through inargs.
72 host_errno = _user_hostlink (HL_GNUIO_EXT_VENDOR_ID, HL_GNUIO_EXT_FSTAT,
73 "iii",
74 (uint32_t) fd,
75 (uint32_t) &hl_stat,
76 (uint32_t) &ret);
78 if (ret < 0)
80 errno = host_errno;
81 return ret;
84 _hl_fstat_map (&hl_stat, buf);
86 _hl_delete ();
88 return ret;
91 int
92 _fstat (int fd, struct stat *buf)
94 return _hl_fstat (fd, buf);