xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/fileio.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* File-I/O functions for GDB, the GNU debugger.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 2003-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #ifndef COMMON_FILEIO_H
218dffb485Schristos #define COMMON_FILEIO_H
228dffb485Schristos 
238dffb485Schristos #include <sys/stat.h>
248dffb485Schristos 
254b169a6bSchristos /* The following flags are defined to be independent of the host
264b169a6bSchristos    as well as the target side implementation of these constants.
274b169a6bSchristos    All constants are defined with a leading FILEIO_ in the name
284b169a6bSchristos    to allow the usage of these constants together with the
294b169a6bSchristos    corresponding implementation dependent constants in one module. */
304b169a6bSchristos 
314b169a6bSchristos /* open(2) flags */
324b169a6bSchristos #define FILEIO_O_RDONLY           0x0
334b169a6bSchristos #define FILEIO_O_WRONLY           0x1
344b169a6bSchristos #define FILEIO_O_RDWR             0x2
354b169a6bSchristos #define FILEIO_O_APPEND           0x8
364b169a6bSchristos #define FILEIO_O_CREAT          0x200
374b169a6bSchristos #define FILEIO_O_TRUNC          0x400
384b169a6bSchristos #define FILEIO_O_EXCL           0x800
394b169a6bSchristos #define FILEIO_O_SUPPORTED      (FILEIO_O_RDONLY | FILEIO_O_WRONLY| \
404b169a6bSchristos 				 FILEIO_O_RDWR   | FILEIO_O_APPEND| \
414b169a6bSchristos 				 FILEIO_O_CREAT  | FILEIO_O_TRUNC| \
424b169a6bSchristos 				 FILEIO_O_EXCL)
434b169a6bSchristos 
444b169a6bSchristos /* mode_t bits */
454b169a6bSchristos #define FILEIO_S_IFREG        0100000
464b169a6bSchristos #define FILEIO_S_IFDIR         040000
474b169a6bSchristos #define FILEIO_S_IFCHR         020000
484b169a6bSchristos #define FILEIO_S_IRUSR           0400
494b169a6bSchristos #define FILEIO_S_IWUSR           0200
504b169a6bSchristos #define FILEIO_S_IXUSR           0100
514b169a6bSchristos #define FILEIO_S_IRWXU           0700
524b169a6bSchristos #define FILEIO_S_IRGRP            040
534b169a6bSchristos #define FILEIO_S_IWGRP            020
544b169a6bSchristos #define FILEIO_S_IXGRP            010
554b169a6bSchristos #define FILEIO_S_IRWXG            070
564b169a6bSchristos #define FILEIO_S_IROTH             04
574b169a6bSchristos #define FILEIO_S_IWOTH             02
584b169a6bSchristos #define FILEIO_S_IXOTH             01
594b169a6bSchristos #define FILEIO_S_IRWXO             07
604b169a6bSchristos #define FILEIO_S_SUPPORTED         (FILEIO_S_IFREG|FILEIO_S_IFDIR|  \
614b169a6bSchristos 				    FILEIO_S_IRWXU|FILEIO_S_IRWXG|  \
624b169a6bSchristos 				    FILEIO_S_IRWXO)
634b169a6bSchristos 
644b169a6bSchristos /* lseek(2) flags */
654b169a6bSchristos #define FILEIO_SEEK_SET             0
664b169a6bSchristos #define FILEIO_SEEK_CUR             1
674b169a6bSchristos #define FILEIO_SEEK_END             2
684b169a6bSchristos 
694b169a6bSchristos /* errno values */
704b169a6bSchristos enum fileio_error
714b169a6bSchristos {
724b169a6bSchristos   FILEIO_SUCCESS      =    0,
734b169a6bSchristos   FILEIO_EPERM        =    1,
744b169a6bSchristos   FILEIO_ENOENT       =    2,
754b169a6bSchristos   FILEIO_EINTR        =    4,
764b169a6bSchristos   FILEIO_EIO          =    5,
774b169a6bSchristos   FILEIO_EBADF        =    9,
784b169a6bSchristos   FILEIO_EACCES       =   13,
794b169a6bSchristos   FILEIO_EFAULT       =   14,
804b169a6bSchristos   FILEIO_EBUSY        =   16,
814b169a6bSchristos   FILEIO_EEXIST       =   17,
824b169a6bSchristos   FILEIO_ENODEV       =   19,
834b169a6bSchristos   FILEIO_ENOTDIR      =   20,
844b169a6bSchristos   FILEIO_EISDIR       =   21,
854b169a6bSchristos   FILEIO_EINVAL       =   22,
864b169a6bSchristos   FILEIO_ENFILE       =   23,
874b169a6bSchristos   FILEIO_EMFILE       =   24,
884b169a6bSchristos   FILEIO_EFBIG        =   27,
894b169a6bSchristos   FILEIO_ENOSPC       =   28,
904b169a6bSchristos   FILEIO_ESPIPE       =   29,
914b169a6bSchristos   FILEIO_EROFS        =   30,
924b169a6bSchristos   FILEIO_ENOSYS       =   88,
934b169a6bSchristos   FILEIO_ENAMETOOLONG =   91,
944b169a6bSchristos   FILEIO_EUNKNOWN     = 9999,
954b169a6bSchristos };
964b169a6bSchristos 
974b169a6bSchristos #define FIO_INT_LEN   4
984b169a6bSchristos #define FIO_UINT_LEN  4
994b169a6bSchristos #define FIO_MODE_LEN  4
1004b169a6bSchristos #define FIO_TIME_LEN  4
1014b169a6bSchristos #define FIO_LONG_LEN  8
1024b169a6bSchristos #define FIO_ULONG_LEN 8
1034b169a6bSchristos 
1044b169a6bSchristos typedef char fio_int_t[FIO_INT_LEN];
1054b169a6bSchristos typedef char fio_uint_t[FIO_UINT_LEN];
1064b169a6bSchristos typedef char fio_mode_t[FIO_MODE_LEN];
1074b169a6bSchristos typedef char fio_time_t[FIO_TIME_LEN];
1084b169a6bSchristos typedef char fio_long_t[FIO_LONG_LEN];
1094b169a6bSchristos typedef char fio_ulong_t[FIO_ULONG_LEN];
1104b169a6bSchristos 
1114b169a6bSchristos /* Struct stat as used in protocol.  For complete independence
1124b169a6bSchristos    of host/target systems, it's defined as an array with offsets
1134b169a6bSchristos    to the members. */
1144b169a6bSchristos 
1154b169a6bSchristos struct fio_stat
1164b169a6bSchristos {
1174b169a6bSchristos   fio_uint_t  fst_dev;
1184b169a6bSchristos   fio_uint_t  fst_ino;
1194b169a6bSchristos   fio_mode_t  fst_mode;
1204b169a6bSchristos   fio_uint_t  fst_nlink;
1214b169a6bSchristos   fio_uint_t  fst_uid;
1224b169a6bSchristos   fio_uint_t  fst_gid;
1234b169a6bSchristos   fio_uint_t  fst_rdev;
1244b169a6bSchristos   fio_ulong_t fst_size;
1254b169a6bSchristos   fio_ulong_t fst_blksize;
1264b169a6bSchristos   fio_ulong_t fst_blocks;
1274b169a6bSchristos   fio_time_t  fst_atime;
1284b169a6bSchristos   fio_time_t  fst_mtime;
1294b169a6bSchristos   fio_time_t  fst_ctime;
1304b169a6bSchristos };
1314b169a6bSchristos 
1324b169a6bSchristos struct fio_timeval
1334b169a6bSchristos {
1344b169a6bSchristos   fio_time_t  ftv_sec;
1354b169a6bSchristos   fio_long_t  ftv_usec;
1364b169a6bSchristos };
1374b169a6bSchristos 
1388dffb485Schristos /* Convert a host-format errno value to a File-I/O error number.  */
1398dffb485Schristos 
1404b169a6bSchristos extern fileio_error host_to_fileio_error (int error);
1414b169a6bSchristos 
1424b169a6bSchristos /* Convert a File-I/O error number to a host-format errno value.  */
1434b169a6bSchristos 
1444b169a6bSchristos extern int fileio_error_to_host (fileio_error errnum);
1458dffb485Schristos 
1468dffb485Schristos /* Convert File-I/O open flags FFLAGS to host format, storing
1478dffb485Schristos    the result in *FLAGS.  Return 0 on success, -1 on error.  */
1488dffb485Schristos 
1498dffb485Schristos extern int fileio_to_host_openflags (int fflags, int *flags);
1508dffb485Schristos 
1518dffb485Schristos /* Convert File-I/O mode FMODE to host format, storing
1528dffb485Schristos    the result in *MODE.  Return 0 on success, -1 on error.  */
1538dffb485Schristos 
1548dffb485Schristos extern int fileio_to_host_mode (int fmode, mode_t *mode);
1558dffb485Schristos 
1568dffb485Schristos /* Pack a host-format integer into a byte buffer in big-endian
1578dffb485Schristos    format.  BYTES specifies the size of the integer to pack in
1588dffb485Schristos    bytes.  */
1598dffb485Schristos 
1608dffb485Schristos static inline void
1618dffb485Schristos host_to_bigendian (LONGEST num, char *buf, int bytes)
1628dffb485Schristos {
1638dffb485Schristos   int i;
1648dffb485Schristos 
1658dffb485Schristos   for (i = 0; i < bytes; ++i)
1668dffb485Schristos     buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
1678dffb485Schristos }
1688dffb485Schristos 
1698dffb485Schristos /* Pack a host-format integer into an fio_uint_t.  */
1708dffb485Schristos 
1718dffb485Schristos static inline void
1728dffb485Schristos host_to_fileio_uint (long num, fio_uint_t fnum)
1738dffb485Schristos {
1748dffb485Schristos   host_to_bigendian ((LONGEST) num, (char *) fnum, 4);
1758dffb485Schristos }
1768dffb485Schristos 
1778dffb485Schristos /* Pack a host-format time_t into an fio_time_t.  */
1788dffb485Schristos 
1798dffb485Schristos static inline void
1808dffb485Schristos host_to_fileio_time (time_t num, fio_time_t fnum)
1818dffb485Schristos {
1828dffb485Schristos   host_to_bigendian ((LONGEST) num, (char *) fnum, 4);
1838dffb485Schristos }
1848dffb485Schristos 
1858dffb485Schristos /* Pack a host-format struct stat into a struct fio_stat.  */
1868dffb485Schristos 
1878dffb485Schristos extern void host_to_fileio_stat (struct stat *st, struct fio_stat *fst);
1888dffb485Schristos 
1898dffb485Schristos #endif /* COMMON_FILEIO_H */
190