1 /* File-I/O functions for GDB, the GNU debugger.
3 Copyright (C) 2003-2024 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/>. */
27 host_to_fileio_error (int error
)
52 return FILEIO_ENOTDIR
;
72 return FILEIO_ENAMETOOLONG
;
74 return FILEIO_EUNKNOWN
;
80 fileio_error_to_host (fileio_error errnum
)
124 case FILEIO_ENAMETOOLONG
:
133 fileio_to_host_openflags (int fileio_open_flags
, int *open_flags_p
)
137 if (fileio_open_flags
& ~FILEIO_O_SUPPORTED
)
140 if (fileio_open_flags
& FILEIO_O_CREAT
)
141 open_flags
|= O_CREAT
;
142 if (fileio_open_flags
& FILEIO_O_EXCL
)
143 open_flags
|= O_EXCL
;
144 if (fileio_open_flags
& FILEIO_O_TRUNC
)
145 open_flags
|= O_TRUNC
;
146 if (fileio_open_flags
& FILEIO_O_APPEND
)
147 open_flags
|= O_APPEND
;
148 if (fileio_open_flags
& FILEIO_O_RDONLY
)
149 open_flags
|= O_RDONLY
;
150 if (fileio_open_flags
& FILEIO_O_WRONLY
)
151 open_flags
|= O_WRONLY
;
152 if (fileio_open_flags
& FILEIO_O_RDWR
)
153 open_flags
|= O_RDWR
;
154 /* On systems supporting binary and text mode, always open files
157 open_flags
|= O_BINARY
;
160 *open_flags_p
= open_flags
;
167 fileio_to_host_mode (int fileio_mode
, mode_t
*mode_p
)
171 if (fileio_mode
& ~FILEIO_S_SUPPORTED
)
174 if (fileio_mode
& FILEIO_S_IFREG
)
176 if (fileio_mode
& FILEIO_S_IFDIR
)
178 if (fileio_mode
& FILEIO_S_IFCHR
)
180 if (fileio_mode
& FILEIO_S_IRUSR
)
182 if (fileio_mode
& FILEIO_S_IWUSR
)
184 if (fileio_mode
& FILEIO_S_IXUSR
)
187 if (fileio_mode
& FILEIO_S_IRGRP
)
191 if (fileio_mode
& FILEIO_S_IWGRP
)
195 if (fileio_mode
& FILEIO_S_IXGRP
)
198 if (fileio_mode
& FILEIO_S_IROTH
)
201 if (fileio_mode
& FILEIO_S_IWOTH
)
205 if (fileio_mode
& FILEIO_S_IXOTH
)
213 /* Convert a host-format mode_t into a bitmask of File-I/O flags. */
216 fileio_mode_pack (mode_t mode
)
221 tmode
|= FILEIO_S_IFREG
;
223 tmode
|= FILEIO_S_IFDIR
;
225 tmode
|= FILEIO_S_IFCHR
;
227 tmode
|= FILEIO_S_IRUSR
;
229 tmode
|= FILEIO_S_IWUSR
;
231 tmode
|= FILEIO_S_IXUSR
;
234 tmode
|= FILEIO_S_IRGRP
;
238 tmode
|= FILEIO_S_IWGRP
;
242 tmode
|= FILEIO_S_IXGRP
;
245 tmode
|= FILEIO_S_IROTH
;
248 tmode
|= FILEIO_S_IWOTH
;
252 tmode
|= FILEIO_S_IXOTH
;
257 /* Pack a host-format mode_t into an fio_mode_t. */
260 host_to_fileio_mode (mode_t num
, fio_mode_t fnum
)
262 host_to_bigendian (fileio_mode_pack (num
), (char *) fnum
, 4);
265 /* Pack a host-format integer into an fio_ulong_t. */
268 host_to_fileio_ulong (LONGEST num
, fio_ulong_t fnum
)
270 host_to_bigendian (num
, (char *) fnum
, 8);
276 host_to_fileio_stat (struct stat
*st
, struct fio_stat
*fst
)
280 host_to_fileio_uint ((long) st
->st_dev
, fst
->fst_dev
);
281 host_to_fileio_uint ((long) st
->st_ino
, fst
->fst_ino
);
282 host_to_fileio_mode (st
->st_mode
, fst
->fst_mode
);
283 host_to_fileio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
284 host_to_fileio_uint ((long) st
->st_uid
, fst
->fst_uid
);
285 host_to_fileio_uint ((long) st
->st_gid
, fst
->fst_gid
);
286 host_to_fileio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
287 host_to_fileio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
288 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
289 blksize
= st
->st_blksize
;
293 host_to_fileio_ulong (blksize
, fst
->fst_blksize
);
294 #if HAVE_STRUCT_STAT_ST_BLOCKS
295 host_to_fileio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
297 /* FIXME: This is correct for DJGPP, but other systems that don't
298 have st_blocks, if any, might prefer 512 instead of st_blksize.
299 (eliz, 30-12-2003) */
300 host_to_fileio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
304 host_to_fileio_time (st
->st_atime
, fst
->fst_atime
);
305 host_to_fileio_time (st
->st_mtime
, fst
->fst_mtime
);
306 host_to_fileio_time (st
->st_ctime
, fst
->fst_ctime
);