Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdbsupport / fileio.cc
blobb115e33b581e2cedc54c4c385e74cfdb17ef45d9
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/>. */
20 #include "fileio.h"
21 #include <sys/stat.h>
22 #include <fcntl.h>
24 /* See fileio.h. */
26 fileio_error
27 host_to_fileio_error (int error)
29 switch (error)
31 case EPERM:
32 return FILEIO_EPERM;
33 case ENOENT:
34 return FILEIO_ENOENT;
35 case EINTR:
36 return FILEIO_EINTR;
37 case EIO:
38 return FILEIO_EIO;
39 case EBADF:
40 return FILEIO_EBADF;
41 case EACCES:
42 return FILEIO_EACCES;
43 case EFAULT:
44 return FILEIO_EFAULT;
45 case EBUSY:
46 return FILEIO_EBUSY;
47 case EEXIST:
48 return FILEIO_EEXIST;
49 case ENODEV:
50 return FILEIO_ENODEV;
51 case ENOTDIR:
52 return FILEIO_ENOTDIR;
53 case EISDIR:
54 return FILEIO_EISDIR;
55 case EINVAL:
56 return FILEIO_EINVAL;
57 case ENFILE:
58 return FILEIO_ENFILE;
59 case EMFILE:
60 return FILEIO_EMFILE;
61 case EFBIG:
62 return FILEIO_EFBIG;
63 case ENOSPC:
64 return FILEIO_ENOSPC;
65 case ESPIPE:
66 return FILEIO_ESPIPE;
67 case EROFS:
68 return FILEIO_EROFS;
69 case ENOSYS:
70 return FILEIO_ENOSYS;
71 case ENAMETOOLONG:
72 return FILEIO_ENAMETOOLONG;
74 return FILEIO_EUNKNOWN;
77 /* See fileio.h. */
79 int
80 fileio_error_to_host (fileio_error errnum)
82 switch (errnum)
84 case FILEIO_EPERM:
85 return EPERM;
86 case FILEIO_ENOENT:
87 return ENOENT;
88 case FILEIO_EINTR:
89 return EINTR;
90 case FILEIO_EIO:
91 return EIO;
92 case FILEIO_EBADF:
93 return EBADF;
94 case FILEIO_EACCES:
95 return EACCES;
96 case FILEIO_EFAULT:
97 return EFAULT;
98 case FILEIO_EBUSY:
99 return EBUSY;
100 case FILEIO_EEXIST:
101 return EEXIST;
102 case FILEIO_ENODEV:
103 return ENODEV;
104 case FILEIO_ENOTDIR:
105 return ENOTDIR;
106 case FILEIO_EISDIR:
107 return EISDIR;
108 case FILEIO_EINVAL:
109 return EINVAL;
110 case FILEIO_ENFILE:
111 return ENFILE;
112 case FILEIO_EMFILE:
113 return EMFILE;
114 case FILEIO_EFBIG:
115 return EFBIG;
116 case FILEIO_ENOSPC:
117 return ENOSPC;
118 case FILEIO_ESPIPE:
119 return ESPIPE;
120 case FILEIO_EROFS:
121 return EROFS;
122 case FILEIO_ENOSYS:
123 return ENOSYS;
124 case FILEIO_ENAMETOOLONG:
125 return ENAMETOOLONG;
127 return -1;
130 /* See fileio.h. */
133 fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
135 int open_flags = 0;
137 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
138 return -1;
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
155 in binary mode. */
156 #ifdef O_BINARY
157 open_flags |= O_BINARY;
158 #endif
160 *open_flags_p = open_flags;
161 return 0;
164 /* See fileio.h. */
167 fileio_to_host_mode (int fileio_mode, mode_t *mode_p)
169 mode_t mode = 0;
171 if (fileio_mode & ~FILEIO_S_SUPPORTED)
172 return -1;
174 if (fileio_mode & FILEIO_S_IFREG)
175 mode |= S_IFREG;
176 if (fileio_mode & FILEIO_S_IFDIR)
177 mode |= S_IFDIR;
178 if (fileio_mode & FILEIO_S_IFCHR)
179 mode |= S_IFCHR;
180 if (fileio_mode & FILEIO_S_IRUSR)
181 mode |= S_IRUSR;
182 if (fileio_mode & FILEIO_S_IWUSR)
183 mode |= S_IWUSR;
184 if (fileio_mode & FILEIO_S_IXUSR)
185 mode |= S_IXUSR;
186 #ifdef S_IRGRP
187 if (fileio_mode & FILEIO_S_IRGRP)
188 mode |= S_IRGRP;
189 #endif
190 #ifdef S_IWGRP
191 if (fileio_mode & FILEIO_S_IWGRP)
192 mode |= S_IWGRP;
193 #endif
194 #ifdef S_IXGRP
195 if (fileio_mode & FILEIO_S_IXGRP)
196 mode |= S_IXGRP;
197 #endif
198 if (fileio_mode & FILEIO_S_IROTH)
199 mode |= S_IROTH;
200 #ifdef S_IWOTH
201 if (fileio_mode & FILEIO_S_IWOTH)
202 mode |= S_IWOTH;
203 #endif
204 #ifdef S_IXOTH
205 if (fileio_mode & FILEIO_S_IXOTH)
206 mode |= S_IXOTH;
207 #endif
209 *mode_p = mode;
210 return 0;
213 /* Convert a host-format mode_t into a bitmask of File-I/O flags. */
215 static LONGEST
216 fileio_mode_pack (mode_t mode)
218 mode_t tmode = 0;
220 if (S_ISREG (mode))
221 tmode |= FILEIO_S_IFREG;
222 if (S_ISDIR (mode))
223 tmode |= FILEIO_S_IFDIR;
224 if (S_ISCHR (mode))
225 tmode |= FILEIO_S_IFCHR;
226 if (mode & S_IRUSR)
227 tmode |= FILEIO_S_IRUSR;
228 if (mode & S_IWUSR)
229 tmode |= FILEIO_S_IWUSR;
230 if (mode & S_IXUSR)
231 tmode |= FILEIO_S_IXUSR;
232 #ifdef S_IRGRP
233 if (mode & S_IRGRP)
234 tmode |= FILEIO_S_IRGRP;
235 #endif
236 #ifdef S_IWGRP
237 if (mode & S_IWGRP)
238 tmode |= FILEIO_S_IWGRP;
239 #endif
240 #ifdef S_IXGRP
241 if (mode & S_IXGRP)
242 tmode |= FILEIO_S_IXGRP;
243 #endif
244 if (mode & S_IROTH)
245 tmode |= FILEIO_S_IROTH;
246 #ifdef S_IWOTH
247 if (mode & S_IWOTH)
248 tmode |= FILEIO_S_IWOTH;
249 #endif
250 #ifdef S_IXOTH
251 if (mode & S_IXOTH)
252 tmode |= FILEIO_S_IXOTH;
253 #endif
254 return tmode;
257 /* Pack a host-format mode_t into an fio_mode_t. */
259 static void
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. */
267 static void
268 host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
270 host_to_bigendian (num, (char *) fnum, 8);
273 /* See fileio.h. */
275 void
276 host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
278 LONGEST blksize;
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;
290 #else
291 blksize = 512;
292 #endif
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);
296 #else
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)
301 / blksize,
302 fst->fst_blocks);
303 #endif
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);