1 /* File-I/O functions for GDB, the GNU debugger.
3 Copyright (C) 2003-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
28 host_to_fileio_error (int error
)
53 return FILEIO_ENOTDIR
;
73 return FILEIO_ENAMETOOLONG
;
75 return FILEIO_EUNKNOWN
;
81 fileio_error_to_host (fileio_error errnum
)
125 case FILEIO_ENAMETOOLONG
:
134 fileio_to_host_openflags (int fileio_open_flags
, int *open_flags_p
)
138 if (fileio_open_flags
& ~FILEIO_O_SUPPORTED
)
141 if (fileio_open_flags
& FILEIO_O_CREAT
)
142 open_flags
|= O_CREAT
;
143 if (fileio_open_flags
& FILEIO_O_EXCL
)
144 open_flags
|= O_EXCL
;
145 if (fileio_open_flags
& FILEIO_O_TRUNC
)
146 open_flags
|= O_TRUNC
;
147 if (fileio_open_flags
& FILEIO_O_APPEND
)
148 open_flags
|= O_APPEND
;
149 if (fileio_open_flags
& FILEIO_O_RDONLY
)
150 open_flags
|= O_RDONLY
;
151 if (fileio_open_flags
& FILEIO_O_WRONLY
)
152 open_flags
|= O_WRONLY
;
153 if (fileio_open_flags
& FILEIO_O_RDWR
)
154 open_flags
|= O_RDWR
;
155 /* On systems supporting binary and text mode, always open files
158 open_flags
|= O_BINARY
;
161 *open_flags_p
= open_flags
;
168 fileio_to_host_mode (int fileio_mode
, mode_t
*mode_p
)
172 if (fileio_mode
& ~FILEIO_S_SUPPORTED
)
175 if (fileio_mode
& FILEIO_S_IFREG
)
177 if (fileio_mode
& FILEIO_S_IFDIR
)
179 if (fileio_mode
& FILEIO_S_IFCHR
)
181 if (fileio_mode
& FILEIO_S_IRUSR
)
183 if (fileio_mode
& FILEIO_S_IWUSR
)
185 if (fileio_mode
& FILEIO_S_IXUSR
)
188 if (fileio_mode
& FILEIO_S_IRGRP
)
192 if (fileio_mode
& FILEIO_S_IWGRP
)
196 if (fileio_mode
& FILEIO_S_IXGRP
)
199 if (fileio_mode
& FILEIO_S_IROTH
)
202 if (fileio_mode
& FILEIO_S_IWOTH
)
206 if (fileio_mode
& FILEIO_S_IXOTH
)
214 /* Convert a host-format mode_t into a bitmask of File-I/O flags. */
217 fileio_mode_pack (mode_t mode
)
222 tmode
|= FILEIO_S_IFREG
;
224 tmode
|= FILEIO_S_IFDIR
;
226 tmode
|= FILEIO_S_IFCHR
;
228 tmode
|= FILEIO_S_IRUSR
;
230 tmode
|= FILEIO_S_IWUSR
;
232 tmode
|= FILEIO_S_IXUSR
;
235 tmode
|= FILEIO_S_IRGRP
;
239 tmode
|= FILEIO_S_IWGRP
;
243 tmode
|= FILEIO_S_IXGRP
;
246 tmode
|= FILEIO_S_IROTH
;
249 tmode
|= FILEIO_S_IWOTH
;
253 tmode
|= FILEIO_S_IXOTH
;
258 /* Pack a host-format mode_t into an fio_mode_t. */
261 host_to_fileio_mode (mode_t num
, fio_mode_t fnum
)
263 host_to_bigendian (fileio_mode_pack (num
), (char *) fnum
, 4);
266 /* Pack a host-format integer into an fio_ulong_t. */
269 host_to_fileio_ulong (LONGEST num
, fio_ulong_t fnum
)
271 host_to_bigendian (num
, (char *) fnum
, 8);
277 host_to_fileio_stat (struct stat
*st
, struct fio_stat
*fst
)
281 host_to_fileio_uint ((long) st
->st_dev
, fst
->fst_dev
);
282 host_to_fileio_uint ((long) st
->st_ino
, fst
->fst_ino
);
283 host_to_fileio_mode (st
->st_mode
, fst
->fst_mode
);
284 host_to_fileio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
285 host_to_fileio_uint ((long) st
->st_uid
, fst
->fst_uid
);
286 host_to_fileio_uint ((long) st
->st_gid
, fst
->fst_gid
);
287 host_to_fileio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
288 host_to_fileio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
289 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
290 blksize
= st
->st_blksize
;
294 host_to_fileio_ulong (blksize
, fst
->fst_blksize
);
295 #if HAVE_STRUCT_STAT_ST_BLOCKS
296 host_to_fileio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
298 /* FIXME: This is correct for DJGPP, but other systems that don't
299 have st_blocks, if any, might prefer 512 instead of st_blksize.
300 (eliz, 30-12-2003) */
301 host_to_fileio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
305 host_to_fileio_time (st
->st_atime
, fst
->fst_atime
);
306 host_to_fileio_time (st
->st_mtime
, fst
->fst_mtime
);
307 host_to_fileio_time (st
->st_ctime
, fst
->fst_ctime
);