GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
stat-wrappers.c
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26// These functions may be provided by gnulib. We don't include gnulib
27// headers directly in Octave's C++ source files to avoid problems that
28// may be caused by the way that gnulib overrides standard library
29// functions.
30
31#if defined (HAVE_CONFIG_H)
32# include "config.h"
33#endif
34
35#include <time.h>
36
37#include <sys/types.h>
38#include <sys/stat.h>
39
40// Silence compiler warning if stat doesn't support nanosecond-precision
41// time stamps (e.g., on Windows).
42#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
43# pragma GCC diagnostic push
44# pragma GCC diagnostic ignored "-Wunused-parameter"
45#endif
46
47#include "stat-time.h"
48
49#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
50# pragma GCC diagnostic pop
51#endif
52
53#include "stat-wrappers.h"
54#include "uniconv-wrappers.h"
55
56#if defined (OCTAVE_USE_WINDOWS_API)
57# include <windows.h>
58# include <wchar.h>
59#endif
60
61int
62octave_mkdir_wrapper (const char *name, mode_t mode)
63{
64#if defined (OCTAVE_USE_WINDOWS_API)
65 wchar_t *wname = u8_to_wchar (name);
66 int status = _wmkdir (wname);
67 free ((void *) wname);
68 octave_unused_parameter (mode);
69 return status;
70#else
71 return mkdir (name, mode);
72#endif
73}
74
75int
76octave_mkfifo_wrapper (const char *name, mode_t mode)
77{
78 return mkfifo (name, mode);
79}
80
81int
83{
84 return umask (mode);
85}
86
87static inline void
88assign_stat_fields (struct stat *buf, mode_t *mode, ino_t *ino,
89 dev_t *dev, nlink_t *nlink, uid_t *uid,
90 gid_t *gid, off_t *size, time_t *atime,
91 long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
92 time_t *ctime, long int *ctime_nsec, dev_t *rdev,
93 long *blksize, long *blocks)
94{
95 *mode = buf->st_mode;
96 *ino = buf->st_ino;
97 *dev = buf->st_dev;
98 *nlink = buf->st_nlink;
99 *uid = buf->st_uid;
100 *gid = buf->st_gid;
101 *size = buf->st_size;
102 *atime = buf->st_atime;
103 *atime_nsec = get_stat_atime_ns (buf);
104 *mtime = buf->st_mtime;
105 *mtime_nsec = get_stat_mtime_ns (buf);
106 *ctime = buf->st_ctime;
107 *ctime_nsec = get_stat_ctime_ns (buf);
108
109#if defined (HAVE_STRUCT_STAT_ST_RDEV)
110 *rdev = buf->st_rdev;
111#else
112 *rdev = 0;
113#endif
114
115#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
116 *blksize = buf->st_blksize;
117#else
118 *blksize = 0;
119#endif
120
121#if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
122 *blocks = buf->st_blocks;
123#else
124 *blocks = 0;
125#endif
126}
127
128int
129octave_stat_wrapper (const char *fname, mode_t *mode, ino_t *ino,
130 dev_t *dev, nlink_t *nlink, uid_t *uid,
131 gid_t *gid, off_t *size, time_t *atime,
132 long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
133 time_t *ctime, long int *ctime_nsec, dev_t *rdev,
134 long *blksize, long *blocks)
135{
136 struct stat buf;
137
138#if defined (OCTAVE_USE_WINDOWS_API)
139 wchar_t *wfname = u8_to_wchar (fname);
140 int status = _wstati64 (wfname, &buf);
141 free ((void *) wfname);
142#else
143 int status = stat (fname, &buf);
144#endif
145
146 assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size,
147 atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec,
148 rdev, blksize, blocks);
149
150 return status;
151}
152
153int
154octave_lstat_wrapper (const char *lname, mode_t *mode, ino_t *ino,
155 dev_t *dev, nlink_t *nlink, uid_t *uid,
156 gid_t *gid, off_t *size, time_t *atime,
157 long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
158 time_t *ctime, long int *ctime_nsec, dev_t *rdev,
159 long *blksize, long *blocks)
160{
161 struct stat buf;
162
163#if defined (OCTAVE_USE_WINDOWS_API)
164 // Windows doesn't have an lstat. Use stat instead
165 wchar_t *wlname = u8_to_wchar (lname);
166 int status = _wstati64 (wlname, &buf);
167 free ((void *) wlname);
168#else
169 int status = lstat (lname, &buf);
170#endif
171
172 assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size,
173 atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec,
174 rdev, blksize, blocks);
175
176 return status;
177}
178
179int
180octave_fstat_wrapper (int fid, mode_t *mode, ino_t *ino,
181 dev_t *dev, nlink_t *nlink, uid_t *uid,
182 gid_t *gid, off_t *size, time_t *atime,
183 long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
184 time_t *ctime, long int *ctime_nsec, dev_t *rdev,
185 long *blksize, long *blocks)
186{
187 struct stat buf;
188
189 int status = fstat (fid, &buf);
190
191 assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size,
192 atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec,
193 rdev, blksize, blocks);
194
195 return status;
196}
197
198#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
199// Disable the unused parameter warning for the following wrapper functions.
200// The <sys/stat.h> header provided by gnulib may define some of the S_IS*
201// macros to expand to a constant and ignore the parameter.
202# pragma GCC diagnostic push
203# pragma GCC diagnostic ignored "-Wunused-parameter"
204#endif
205
206bool
208{
209#if defined (S_ISBLK)
210 return S_ISBLK (mode);
211#else
212 return false;
213#endif
214}
215
216bool
218{
219#if defined (S_ISCHR)
220 return S_ISCHR (mode);
221#else
222 return false;
223#endif
224}
225
226bool
228{
229#if defined (S_ISDIR)
230 return S_ISDIR (mode);
231#else
232 return false;
233#endif
234}
235
236bool
238{
239#if defined (S_ISFIFO)
240 return S_ISFIFO (mode);
241#else
242 return false;
243#endif
244}
245
246bool
248{
249#if defined (S_ISLNK)
250 return S_ISLNK (mode);
251#else
252 return false;
253#endif
254}
255
256bool
258{
259#if defined (S_ISREG)
260 return S_ISREG (mode);
261#else
262 return false;
263#endif
264}
265
266bool
268{
269#if defined (S_ISSOCK)
270 return S_ISSOCK (mode);
271#else
272 return false;
273#endif
274}
275
276#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
277// Restore prevailing warning state for remainder of the file.
278# pragma GCC diagnostic pop
279#endif
280
281bool
283{
284#if defined (HAVE_STRUCT_STAT_ST_RDEV)
285 return true;
286#else
287 return false;
288#endif
289}
290
291bool
293{
294#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
295 return true;
296#else
297 return false;
298#endif
299}
300
301bool
303{
304#if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
305 return true;
306#else
307 return false;
308#endif
309}
int mkfifo(const std::string &nm, mode_t md)
Definition file-ops.cc:488
int mkdir(const std::string &nm, mode_t md)
Definition file-ops.cc:413
int umask(mode_t mode)
Definition file-ops.cc:721
void free(void *)
bool octave_is_lnk_wrapper(mode_t mode)
int octave_lstat_wrapper(const char *lname, mode_t *mode, ino_t *ino, dev_t *dev, nlink_t *nlink, uid_t *uid, gid_t *gid, off_t *size, time_t *atime, long int *atime_nsec, time_t *mtime, long int *mtime_nsec, time_t *ctime, long int *ctime_nsec, dev_t *rdev, long *blksize, long *blocks)
int octave_mkdir_wrapper(const char *name, mode_t mode)
bool octave_is_chr_wrapper(mode_t mode)
bool octave_is_fifo_wrapper(mode_t mode)
bool octave_have_struct_stat_st_rdev(void)
bool octave_have_struct_stat_st_blocks(void)
bool octave_is_blk_wrapper(mode_t mode)
bool octave_is_dir_wrapper(mode_t mode)
bool octave_is_reg_wrapper(mode_t mode)
int octave_stat_wrapper(const char *fname, mode_t *mode, ino_t *ino, dev_t *dev, nlink_t *nlink, uid_t *uid, gid_t *gid, off_t *size, time_t *atime, long int *atime_nsec, time_t *mtime, long int *mtime_nsec, time_t *ctime, long int *ctime_nsec, dev_t *rdev, long *blksize, long *blocks)
int octave_mkfifo_wrapper(const char *name, mode_t mode)
int octave_fstat_wrapper(int fid, mode_t *mode, ino_t *ino, dev_t *dev, nlink_t *nlink, uid_t *uid, gid_t *gid, off_t *size, time_t *atime, long int *atime_nsec, time_t *mtime, long int *mtime_nsec, time_t *ctime, long int *ctime_nsec, dev_t *rdev, long *blksize, long *blocks)
int octave_umask_wrapper(mode_t mode)
bool octave_is_sock_wrapper(mode_t mode)
bool octave_have_struct_stat_st_blksize(void)
subroutine stat(x, n, av, var, xmin, xmax)
Definition tstgmn.for:112
wchar_t * u8_to_wchar(const char *u8)