1 /** 2 * Copyright (c) 2023-2024 Marcin Niestroj 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * @file 9 * 10 * fcntl.h related code common to Zephyr (top: nsos_sockets.c) and Linux 11 * (bottom: nsos_adapt.c). 12 * 13 * It is needed by both sides to share the same macro definitions/values 14 * (prefixed with NSOS_MID_), which is not possible to achieve with two separate 15 * standard libc libraries, since they use different values for the same 16 * symbols. 17 */ 18 19 /* 20 * When building for Zephyr, use Zephyr specific fcntl definitions. 21 */ 22 #ifdef __ZEPHYR__ 23 #include <zephyr/posix/fcntl.h> 24 #else 25 #include <fcntl.h> 26 #endif 27 28 #include "nsos_errno.h" 29 #include "nsos_fcntl.h" 30 31 #include <stdbool.h> 32 fl_to_nsos_mid_(int flags,bool strict)33static int fl_to_nsos_mid_(int flags, bool strict) 34 { 35 int flags_mid = 0; 36 37 #define TO_NSOS_MID(_flag) \ 38 if (flags & (_flag)) { \ 39 flags &= ~(_flag); \ 40 flags_mid |= NSOS_MID_ ## _flag; \ 41 } 42 43 TO_NSOS_MID(O_RDONLY); 44 TO_NSOS_MID(O_WRONLY); 45 TO_NSOS_MID(O_RDWR); 46 47 TO_NSOS_MID(O_APPEND); 48 TO_NSOS_MID(O_EXCL); 49 TO_NSOS_MID(O_NONBLOCK); 50 51 #undef TO_NSOS_MID 52 53 if (strict && flags != 0) { 54 return -NSOS_MID_EINVAL; 55 } 56 57 return flags_mid; 58 } 59 fl_to_nsos_mid(int flags)60int fl_to_nsos_mid(int flags) 61 { 62 return fl_to_nsos_mid_(flags, false); 63 } 64 fl_to_nsos_mid_strict(int flags)65int fl_to_nsos_mid_strict(int flags) 66 { 67 return fl_to_nsos_mid_(flags, true); 68 } 69 fl_from_nsos_mid(int flags_mid)70int fl_from_nsos_mid(int flags_mid) 71 { 72 int flags = 0; 73 74 #define FROM_NSOS_MID(_flag) \ 75 if (flags_mid & NSOS_MID_ ## _flag) { \ 76 flags_mid &= ~NSOS_MID_ ## _flag; \ 77 flags |= _flag; \ 78 } 79 80 FROM_NSOS_MID(O_RDONLY); 81 FROM_NSOS_MID(O_WRONLY); 82 FROM_NSOS_MID(O_RDWR); 83 84 FROM_NSOS_MID(O_APPEND); 85 FROM_NSOS_MID(O_EXCL); 86 FROM_NSOS_MID(O_NONBLOCK); 87 88 #undef FROM_NSOS_MID 89 90 return flags; 91 } 92