| 1 |
/* ErrnoCatogory.cc categorizes values of errno maps onto ChanConsts.h. |
| 2 |
|
| 3 |
Copyright (C) 2008-2022 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. */ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include "ChanConsts.h" |
| 29 |
|
| 30 |
#if defined(HAVE_ERRNO_H) |
| 31 |
#include "errno.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
#if defined(HAVE_SYS_ERRNO_H) |
| 35 |
#include "sys/errno.h" |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "m2rts.h" |
| 39 |
|
| 40 |
#define EXPORT(FUNC) m2iso ## _ErrnoCategory_ ## FUNC |
| 41 |
#define M2EXPORT(FUNC) m2iso ## _M2_ErrnoCategory_ ## FUNC |
| 42 |
#define M2LIBNAME "m2iso" |
| 43 |
|
| 44 |
|
| 45 |
/* IsErrnoHard returns true if the value of errno is associated |
| 46 |
with a hard device error. */ |
| 47 |
|
| 48 |
extern "C" bool |
| 49 |
EXPORT(IsErrnoHard) (int e) |
| 50 |
{ |
| 51 |
#if defined(HAVE_ERRNO_H) || defined(HAVE_SYS_ERRNO_H) |
| 52 |
return ((e == EPERM) || (e == ENOENT) || (e == EIO) || (e == ENXIO) |
| 53 |
|| (e == EACCES) || (e == ENODEV) || (e == EINVAL) |
| 54 |
#ifdef ENOTBLK |
| 55 |
|| (e == ENOTBLK) |
| 56 |
#endif |
| 57 |
|| (e == ENFILE) || (e == EROFS) || (e == EMLINK)); |
| 58 |
#else |
| 59 |
return false; |
| 60 |
#endif |
| 61 |
} |
| 62 |
|
| 63 |
/* IsErrnoSoft returns true if the value of errno is associated |
| 64 |
with a soft device error. */ |
| 65 |
|
| 66 |
extern "C" bool |
| 67 |
EXPORT(IsErrnoSoft) (int e) |
| 68 |
{ |
| 69 |
#if defined(HAVE_ERRNO_H) || defined(HAVE_SYS_ERRNO_H) |
| 70 |
return ((e == ESRCH) || (e == EINTR) || (e == E2BIG) || (e == ENOEXEC) |
| 71 |
|| (e == EBADF) || (e == ECHILD) || (e == EAGAIN) || (e == ENOMEM) |
| 72 |
|| (e == EFAULT) || (e == EBUSY) || (e == EEXIST) || (e == EXDEV) |
| 73 |
|| (e == ENOTDIR) || (e == EISDIR) || (e == EMFILE) || (e == ENOTTY) |
| 74 |
|| (e == ETXTBSY) || (e == EFBIG) || (e == ENOSPC) || (e == EPIPE)); |
| 75 |
#else |
| 76 |
return false; |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
extern "C" bool |
| 81 |
EXPORT(UnAvailable) (int e) |
| 82 |
{ |
| 83 |
#if defined(HAVE_ERRNO_H) || defined(HAVE_SYS_ERRNO_H) |
| 84 |
return ((e == ENOENT) || (e == ESRCH) || (e == ENXIO) || (e == ECHILD) |
| 85 |
#ifdef ENOTBLK |
| 86 |
|| (e == ENOTBLK) |
| 87 |
#endif |
| 88 |
|| (e == ENODEV) || (e == ENOTDIR)); |
| 89 |
#else |
| 90 |
return false; |
| 91 |
#endif |
| 92 |
} |
| 93 |
|
| 94 |
/* GetOpenResults - maps errno onto the ISO Modula-2 enumerated type, |
| 95 |
OpenResults. */ |
| 96 |
|
| 97 |
extern "C" openResults |
| 98 |
EXPORT(GetOpenResults) (int e) |
| 99 |
{ |
| 100 |
if (e == 0) |
| 101 |
return opened; |
| 102 |
#if defined(HAVE_ERRNO_H) || defined(HAVE_SYS_ERRNO_H) |
| 103 |
switch (e) |
| 104 |
{ |
| 105 |
case EPERM: |
| 106 |
return wrongPermissions; |
| 107 |
break; |
| 108 |
case ENOENT: |
| 109 |
return noSuchFile; |
| 110 |
break; |
| 111 |
case ENXIO: |
| 112 |
return noSuchFile; |
| 113 |
break; |
| 114 |
case EACCES: |
| 115 |
return wrongPermissions; |
| 116 |
break; |
| 117 |
#ifdef ENOTBLK |
| 118 |
case ENOTBLK: |
| 119 |
return wrongFileType; |
| 120 |
break; |
| 121 |
#endif |
| 122 |
case EEXIST: |
| 123 |
return fileExists; |
| 124 |
break; |
| 125 |
case ENODEV: |
| 126 |
return noSuchFile; |
| 127 |
break; |
| 128 |
case ENOTDIR: |
| 129 |
return wrongFileType; |
| 130 |
break; |
| 131 |
case EISDIR: |
| 132 |
return wrongFileType; |
| 133 |
break; |
| 134 |
case EINVAL: |
| 135 |
return wrongFlags; |
| 136 |
break; |
| 137 |
case ENFILE: |
| 138 |
return tooManyOpen; |
| 139 |
break; |
| 140 |
case EMFILE: |
| 141 |
return tooManyOpen; |
| 142 |
break; |
| 143 |
case ENOTTY: |
| 144 |
return wrongFileType; |
| 145 |
break; |
| 146 |
case ENOSPC: |
| 147 |
return noRoomOnDevice; |
| 148 |
break; |
| 149 |
case EROFS: |
| 150 |
return wrongPermissions; |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
return otherProblem; |
| 155 |
} |
| 156 |
#else |
| 157 |
return otherProblem; |
| 158 |
#endif |
| 159 |
} |
| 160 |
|
| 161 |
/* GNU Modula-2 linking fodder. */ |
| 162 |
|
| 163 |
extern "C" void |
| 164 |
M2EXPORT(init) (int, char **, char **) |
| 165 |
{ |
| 166 |
} |
| 167 |
|
| 168 |
extern "C" void |
| 169 |
M2EXPORT(fini) (int, char **, char **) |
| 170 |
{ |
| 171 |
} |
| 172 |
|
| 173 |
extern "C" void |
| 174 |
M2EXPORT(dep) (void) |
| 175 |
{ |
| 176 |
} |
| 177 |
|
| 178 |
extern "C" void __attribute__((__constructor__)) |
| 179 |
M2EXPORT(ctor) (void) |
| 180 |
{ |
| 181 |
m2iso_M2RTS_RegisterModule ("ErrnoCategory", M2LIBNAME, |
| 182 |
M2EXPORT(init), M2EXPORT(fini), |
| 183 |
M2EXPORT(dep)); |
| 184 |
} |