1 /* File-I/O functions for GDB, the GNU debugger.
3 Copyright (C) 2003-2015 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_to_host_openflags (int fileio_open_flags, int *open_flags_p)
85 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
88 if (fileio_open_flags & FILEIO_O_CREAT)
89 open_flags |= O_CREAT;
90 if (fileio_open_flags & FILEIO_O_EXCL)
92 if (fileio_open_flags & FILEIO_O_TRUNC)
93 open_flags |= O_TRUNC;
94 if (fileio_open_flags & FILEIO_O_APPEND)
95 open_flags |= O_APPEND;
96 if (fileio_open_flags & FILEIO_O_RDONLY)
97 open_flags |= O_RDONLY;
98 if (fileio_open_flags & FILEIO_O_WRONLY)
99 open_flags |= O_WRONLY;
100 if (fileio_open_flags & FILEIO_O_RDWR)
101 open_flags |= O_RDWR;
102 /* On systems supporting binary and text mode, always open files
105 open_flags |= O_BINARY;
108 *open_flags_p = open_flags;
115 fileio_to_host_mode (int fileio_mode, mode_t *mode_p)
119 if (fileio_mode & ~FILEIO_S_SUPPORTED)
122 if (fileio_mode & FILEIO_S_IFREG)
124 if (fileio_mode & FILEIO_S_IFDIR)
126 if (fileio_mode & FILEIO_S_IFCHR)
128 if (fileio_mode & FILEIO_S_IRUSR)
130 if (fileio_mode & FILEIO_S_IWUSR)
132 if (fileio_mode & FILEIO_S_IXUSR)
135 if (fileio_mode & FILEIO_S_IRGRP)
139 if (fileio_mode & FILEIO_S_IWGRP)
143 if (fileio_mode & FILEIO_S_IXGRP)
146 if (fileio_mode & FILEIO_S_IROTH)
149 if (fileio_mode & FILEIO_S_IWOTH)
153 if (fileio_mode & FILEIO_S_IXOTH)
161 /* Convert a host-format mode_t into a bitmask of File-I/O flags. */
164 fileio_mode_pack (mode_t mode)
169 tmode |= FILEIO_S_IFREG;
171 tmode |= FILEIO_S_IFDIR;
173 tmode |= FILEIO_S_IFCHR;
175 tmode |= FILEIO_S_IRUSR;
177 tmode |= FILEIO_S_IWUSR;
179 tmode |= FILEIO_S_IXUSR;
182 tmode |= FILEIO_S_IRGRP;
186 tmode |= FILEIO_S_IWGRP;
190 tmode |= FILEIO_S_IXGRP;
193 tmode |= FILEIO_S_IROTH;
196 tmode |= FILEIO_S_IWOTH;
200 tmode |= FILEIO_S_IXOTH;
205 /* Pack a host-format mode_t into an fio_mode_t. */
208 host_to_fileio_mode (mode_t num, fio_mode_t fnum)
210 host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4);
213 /* Pack a host-format integer into an fio_ulong_t. */
216 host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
218 host_to_bigendian (num, (char *) fnum, 8);
224 host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
228 host_to_fileio_uint ((long) st->st_dev, fst->fst_dev);
229 host_to_fileio_uint ((long) st->st_ino, fst->fst_ino);
230 host_to_fileio_mode (st->st_mode, fst->fst_mode);
231 host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink);
232 host_to_fileio_uint ((long) st->st_uid, fst->fst_uid);
233 host_to_fileio_uint ((long) st->st_gid, fst->fst_gid);
234 host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev);
235 host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size);
236 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
237 blksize = st->st_blksize;
241 host_to_fileio_ulong (blksize, fst->fst_blksize);
242 #if HAVE_STRUCT_STAT_ST_BLOCKS
243 host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
245 /* FIXME: This is correct for DJGPP, but other systems that don't
246 have st_blocks, if any, might prefer 512 instead of st_blksize.
247 (eliz, 30-12-2003) */
248 host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1)
252 host_to_fileio_time (st->st_atime, fst->fst_atime);
253 host_to_fileio_time (st->st_mtime, fst->fst_mtime);
254 host_to_fileio_time (st->st_ctime, fst->fst_ctime);