sim: mcore: clean up printf warnings
[deliverable/binutils-gdb.git] / gdb / common / fileio.c
CommitLineData
7823a941 1/* File-I/O functions for GDB, the GNU debugger.
791c0056
GB
2
3 Copyright (C) 2003-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "common-defs.h"
7823a941 21#include "fileio.h"
791c0056
GB
22#include <sys/stat.h>
23
7823a941 24/* See fileio.h. */
b88bb450
GB
25
26int
7823a941 27host_to_fileio_error (int error)
b88bb450
GB
28{
29 switch (error)
30 {
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;
73 }
74 return FILEIO_EUNKNOWN;
75}
76
791c0056
GB
77/* Convert a host-format mode_t into a bitmask of File-I/O flags. */
78
79static LONGEST
7823a941 80fileio_mode_pack (mode_t mode)
791c0056
GB
81{
82 mode_t tmode = 0;
83
84 if (S_ISREG (mode))
85 tmode |= FILEIO_S_IFREG;
86 if (S_ISDIR (mode))
87 tmode |= FILEIO_S_IFDIR;
88 if (S_ISCHR (mode))
89 tmode |= FILEIO_S_IFCHR;
90 if (mode & S_IRUSR)
91 tmode |= FILEIO_S_IRUSR;
92 if (mode & S_IWUSR)
93 tmode |= FILEIO_S_IWUSR;
94 if (mode & S_IXUSR)
95 tmode |= FILEIO_S_IXUSR;
96#ifdef S_IRGRP
97 if (mode & S_IRGRP)
98 tmode |= FILEIO_S_IRGRP;
99#endif
100#ifdef S_IWRGRP
101 if (mode & S_IWGRP)
102 tmode |= FILEIO_S_IWGRP;
103#endif
104#ifdef S_IXGRP
105 if (mode & S_IXGRP)
106 tmode |= FILEIO_S_IXGRP;
107#endif
108 if (mode & S_IROTH)
109 tmode |= FILEIO_S_IROTH;
110#ifdef S_IWOTH
111 if (mode & S_IWOTH)
112 tmode |= FILEIO_S_IWOTH;
113#endif
114#ifdef S_IXOTH
115 if (mode & S_IXOTH)
116 tmode |= FILEIO_S_IXOTH;
117#endif
118 return tmode;
119}
120
121/* Pack a host-format mode_t into an fio_mode_t. */
122
123static void
7823a941 124host_to_fileio_mode (mode_t num, fio_mode_t fnum)
791c0056 125{
7823a941 126 host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4);
791c0056
GB
127}
128
129/* Pack a host-format integer into an fio_ulong_t. */
130
131static void
7823a941 132host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
791c0056 133{
7823a941 134 host_to_bigendian (num, (char *) fnum, 8);
791c0056
GB
135}
136
7823a941 137/* See fileio.h. */
791c0056
GB
138
139void
7823a941 140host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
791c0056
GB
141{
142 LONGEST blksize;
143
7823a941
GB
144 host_to_fileio_uint ((long) st->st_dev, fst->fst_dev);
145 host_to_fileio_uint ((long) st->st_ino, fst->fst_ino);
146 host_to_fileio_mode (st->st_mode, fst->fst_mode);
147 host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink);
148 host_to_fileio_uint ((long) st->st_uid, fst->fst_uid);
149 host_to_fileio_uint ((long) st->st_gid, fst->fst_gid);
150 host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev);
151 host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size);
791c0056
GB
152#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
153 blksize = st->st_blksize;
154#else
155 blksize = 512;
156#endif
7823a941 157 host_to_fileio_ulong (blksize, fst->fst_blksize);
791c0056 158#if HAVE_STRUCT_STAT_ST_BLOCKS
7823a941 159 host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
791c0056
GB
160#else
161 /* FIXME: This is correct for DJGPP, but other systems that don't
162 have st_blocks, if any, might prefer 512 instead of st_blksize.
163 (eliz, 30-12-2003) */
7823a941
GB
164 host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1)
165 / blksize,
166 fst->fst_blocks);
791c0056 167#endif
7823a941
GB
168 host_to_fileio_time (st->st_atime, fst->fst_atime);
169 host_to_fileio_time (st->st_mtime, fst->fst_mtime);
170 host_to_fileio_time (st->st_ctime, fst->fst_ctime);
791c0056 171}
This page took 0.039451 seconds and 4 git commands to generate.