| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
#include "io.h" |
| 34 |
|
| 35 |
#include <cstdio> |
| 36 |
#include <cstdlib> |
| 37 |
#include <cerrno> |
| 38 |
#include <cstdbool> |
| 39 |
#include <cstdint> |
| 40 |
|
| 41 |
/* |
| 42 |
* The Cobol runtime support is responsible to set the file status |
| 43 |
* word appropriately to the application's expectations. This function |
| 44 |
* sets the defined file status register for the file to value of the |
| 45 |
* status parameter, except for FhErrno. For FhErrno, it sets the |
| 46 |
* file status register to a value derived from the current value of |
| 47 |
* errno. If the errno value is not accounted for, the high bit is |
| 48 |
* set to 1, and the rest to errno. |
| 49 |
*/ |
| 50 |
extern "C" |
| 51 |
file_status_t |
| 52 |
__gg__file_status_word( enum file_status_t status, |
| 53 |
int error_number) { |
| 54 |
file_status_t file_status_register; |
| 55 |
|
| 56 |
if( status != FsErrno ) { |
| 57 |
return status; |
| 58 |
} |
| 59 |
|
| 60 |
switch( error_number ) { |
| 61 |
case 0: file_status_register = FsSuccess; break; |
| 62 |
case EACCES: file_status_register = FsNoAccess; break; |
| 63 |
case EDQUOT: file_status_register = FsBoundary; break; |
| 64 |
case EEXIST: file_status_register = FsNoAccess; break; |
| 65 |
case EFAULT: file_status_register = FsNoFile; break; |
| 66 |
case EFBIG: file_status_register = FsBoundary; break; |
| 67 |
case EINTR: file_status_register = FsOsError; break; |
| 68 |
case EINVAL: file_status_register = FsWrongType; break; |
| 69 |
case EISDIR: file_status_register = FsWrongType; break; |
| 70 |
case ELOOP: file_status_register = FsOsError; break; |
| 71 |
case EMFILE: file_status_register = FsOsError; break; |
| 72 |
case ENAMETOOLONG: |
| 73 |
file_status_register = FsWrongType; break; |
| 74 |
case ENFILE: file_status_register = FsOsError; break; |
| 75 |
case ENODEV: file_status_register = FsNoFile; break; |
| 76 |
case ENOENT: file_status_register = FsNoFile; break; |
| 77 |
case ENOMEM: file_status_register = FsOsError; break; |
| 78 |
case ENOSPC: file_status_register = FsBoundary; break; |
| 79 |
case ENOTDIR: file_status_register = FsNoFile; break; |
| 80 |
case ENXIO: file_status_register = FsNoFile; break; |
| 81 |
case EOPNOTSUPP: |
| 82 |
file_status_register = FsOsError; break; |
| 83 |
case EOVERFLOW: file_status_register = FsBoundary; break; |
| 84 |
case EPERM: file_status_register = FsNoAccess; break; |
| 85 |
case EROFS: file_status_register = FsNoAccess; break; |
| 86 |
case ETXTBSY: file_status_register= FsWrongType; break; |
| 87 |
case EWOULDBLOCK: |
| 88 |
file_status_register = FsOsError; break; |
| 89 |
default: |
| 90 |
perror("What is this? "); |
| 91 |
fprintf(stderr, "__gg__file_status_word got an error_number " |
| 92 |
"%d, which it doesn't know how to handle\n", error_number); |
| 93 |
|
| 94 |
abort(); |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
return file_status_register; |
| 99 |
} |