/*************************************************************** SYS-OPEN.C This file contains Linux/i386/ILP32 implementation of __cpu_sys_open(). PART OF : CPU - library . USAGE : Internal only . Copyright (C) 2026 by Andrew V.Kosteltsev. All Rights Reserved. ***************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include static int __cpu_open_flags( __mpu_uint64_t flags, int *host_flags ) { int f = 0; if( !host_flags ) return( __MCPU_EINVAL ); if( flags & ~(__mpu_uint64_t)__CPU_O_SUPPORTED ) return( __MCPU_EINVAL ); switch( flags & __CPU_O_ACCMODE ) { case __CPU_O_RDONLY: f |= O_RDONLY; break; case __CPU_O_WRONLY: f |= O_WRONLY; break; case __CPU_O_RDWR: f |= O_RDWR; break; default: return( __MCPU_EINVAL ); } if( flags & __CPU_O_CREAT ) f |= O_CREAT; if( flags & __CPU_O_EXCL ) f |= O_EXCL; if( flags & __CPU_O_NOCTTY ) f |= O_NOCTTY; if( flags & __CPU_O_TRUNC ) f |= O_TRUNC; if( flags & __CPU_O_APPEND ) f |= O_APPEND; if( flags & __CPU_O_NONBLOCK ) f |= O_NONBLOCK; *host_flags = f; return( 0 ); } /* End of __cpu_open_flags() */ static int __cpu_open_mode( __mpu_uint64_t mode, mode_t *host_mode ) { mode_t m = 0; if( !host_mode ) return( __MCPU_EINVAL ); if( mode & ~(__mpu_uint64_t)__CPU_S_SUPPORTED ) return( __MCPU_EINVAL ); if( mode & __CPU_S_ISUID ) m |= S_ISUID; if( mode & __CPU_S_ISGID ) m |= S_ISGID; #ifdef S_ISVTX if( mode & __CPU_S_ISVTX ) m |= S_ISVTX; #endif if( mode & __CPU_S_IRUSR ) m |= S_IRUSR; if( mode & __CPU_S_IWUSR ) m |= S_IWUSR; if( mode & __CPU_S_IXUSR ) m |= S_IXUSR; if( mode & __CPU_S_IRGRP ) m |= S_IRGRP; if( mode & __CPU_S_IWGRP ) m |= S_IWGRP; if( mode & __CPU_S_IXGRP ) m |= S_IXGRP; if( mode & __CPU_S_IROTH ) m |= S_IROTH; if( mode & __CPU_S_IWOTH ) m |= S_IWOTH; if( mode & __CPU_S_IXOTH ) m |= S_IXOTH; *host_mode = m; return( 0 ); } /* End of __cpu_open_mode() */ __mpu_int64_t __cpu_sys_open( int dirfd, const char *path, __mpu_uint64_t flags, __mpu_uint64_t mode ) { mode_t host_mode = 0; int host_flags; int error; int fd; if( !path ) return( -(__mpu_int64_t)__MCPU_EFAULT ); error = __cpu_open_flags( flags, &host_flags ); if( error ) return( -(__mpu_int64_t)error ); if( flags & __CPU_O_CREAT ) { error = __cpu_open_mode( mode, &host_mode ); if( error ) return( -(__mpu_int64_t)error ); fd = openat( dirfd, path, host_flags, host_mode ); } else { if( mode != 0 ) return( -(__mpu_int64_t)__MCPU_EINVAL ); fd = openat( dirfd, path, host_flags ); } if( fd < 0 ) return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) ); return( (__mpu_int64_t)fd ); } /* End of __cpu_sys_open() */