1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2023 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "arc_semihost.h"
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <stdarg.h>
44 
45 struct arc_stat
46 {
47     uint16_t my_dev;
48     uint16_t my___pad1;
49     uint32_t my_ino;
50     uint16_t my_mode;
51     uint16_t my_nlink;
52     uint16_t my_uid;
53     uint16_t my_gid;
54     uint16_t my_rdev;
55     uint16_t my___pad2;
56     uint32_t my_size;
57     uint32_t my_blksize;
58     uint32_t my_blocks;
59     uint32_t my_atime;
60     uint32_t my___unused1;
61     uint32_t my_mtime;
62     uint32_t my___unused2;
63     uint32_t my_ctime;
64     uint32_t my___unused3;
65     uint32_t my___unused4;
66     uint32_t my___unused5;
67 };
68 
69 int
fstat(int fd,struct stat * restrict statbuf)70 fstat(int fd, struct stat *restrict statbuf)
71 {
72     struct arc_stat arc_stat;
73 
74     int ret = arc_semihost2(SYS_SEMIHOST_fstat, fd, (uintptr_t) &arc_stat);
75     if (ret < 0) {
76         arc_semihost_errno(EBADF);
77     } else {
78         statbuf->st_dev = arc_stat.my_dev;
79         statbuf->st_ino = arc_stat.my_ino;
80         statbuf->st_mode = arc_stat.my_mode;
81         statbuf->st_nlink = arc_stat.my_nlink;
82         statbuf->st_uid = arc_stat.my_uid;
83         statbuf->st_gid = arc_stat.my_gid;
84         statbuf->st_rdev = arc_stat.my_rdev;
85         statbuf->st_size = arc_stat.my_size;
86         statbuf->st_blksize = arc_stat.my_blksize;
87         statbuf->st_blocks = arc_stat.my_blocks;
88         statbuf->st_atime = arc_stat.my_atime;
89         statbuf->st_mtime = arc_stat.my_mtime;
90         statbuf->st_ctime = arc_stat.my_ctime;
91     }
92     return ret;
93 }
94