/* * Return a pointer to a string containing information about currently open * POSIX-level file handles. If _fileinfo is non-zero, this string is * passed to child processes as an environment string. Each handle's data * is of the form "PPPPPPPP:OOOOOOOO:MMMMMMMM*", where the Ps represent the * POSIX-level file number, the Os represent the OS-level file number, and * the Ms represent the file's mode bits. The Ps, Os, and Ms are all hex * strings, without zero padding or 0x prefixes to conserve space. Returns * NULL if there is insufficient memory. The caller is responsible for * freeing the allocated memory. The returned string begins with * "C_FILE_INFO=", and so forms a valid environment string. */ _WCRTLINK CHAR_TYPE *__F_NAME(__FormPosixHandleStr,__wFormPosixHandleStr)( void ) /*******************************************************************************/ { extern unsigned __NFiles; CHAR_TYPE * p; int posixHandle, osHandle, mode; CHAR_TYPE curElem[MAX_ELEM_SIZE+1]; CHAR_TYPE buf[9]; size_t len; /*** Allocate memory for the string ***/ len = (__NFiles*MAX_ELEM_SIZE) + __F_NAME(strlen,wcslen)( STRING( "C_FILE_INFO=" ) ) + 1; p = lib_malloc( len * sizeof( CHAR_TYPE ) ); if( p == NULL ) return( NULL ); __F_NAME(strcpy,wcscpy)( p, STRING( "C_FILE_INFO=" ) ); /*** Process the open files ***/ for( posixHandle=0; posixHandle<__NFiles; posixHandle++ ) { __ChkTTYIOMode( posixHandle ); mode = __GetIOMode( posixHandle ); if( mode & _INITIALIZED ) { /* skip it if it's not open */ osHandle = _os_handle( posixHandle ); /*** Build the element string ***/ curElem[0] = NULLCHAR; __F_NAME(itoa,_itow)( posixHandle, buf, 16 ); /* POSIX handle */ __F_NAME(strcat,wcscat)( curElem, buf ); __F_NAME(strcat,wcscat)( curElem, STRING( ":" ) ); /* separator */ __F_NAME(itoa,_itow)( osHandle, buf, 16 ); /* OS handle */ __F_NAME(strcat,wcscat)( curElem, buf ); __F_NAME(strcat,wcscat)( curElem, STRING( ":" ) ); /* separator */ __F_NAME(itoa,_itow)( mode, buf, 16 ); /* file mode */ __F_NAME(strcat,wcscat)( curElem, buf ); __F_NAME(strcat,wcscat)( curElem, STRING( "*" ) ); /* terminator */ __F_NAME(strcat,wcscat)( p, curElem ); /* append it */ } } return( p ); }
_WCRTLINK int read( int handle, void *buf, unsigned len ) { unsigned read_len, total_len; unsigned reduce_idx, finish_idx; unsigned iomode_flags; char *buffer = buf; BOOL rc; HANDLE h; unsigned amount_read; int err; __file_handle *fh; __handle_check( handle, -1 ); __ChkTTYIOMode( handle ); iomode_flags = __GetIOMode( handle ); if( iomode_flags == 0 ) { __set_errno( EBADF ); return( -1 ); } if( !(iomode_flags & _READ) ) { __set_errno( EACCES ); /* changed from EBADF to EACCES 23-feb-89 */ return( -1 ); } fh = (__file_handle*) __getOSHandle( handle ); if( iomode_flags & _BINARY ) /* if binary mode */ { err=read_file(fh->name,buffer,fh->offset,len,&amount_read); fh->offset+=amount_read; total_len = amount_read; if(err) if ( amount_read == 0) return (-1); } else { total_len = 0; read_len = len; do { err=read_file(fh->name,buffer,fh->offset,len,&amount_read); fh->offset+=amount_read; if( amount_read == 0 ) break; /* EOF */ reduce_idx = 0; finish_idx = reduce_idx; for( ; reduce_idx < amount_read; ++reduce_idx ) { if( buffer[ reduce_idx ] == 0x1a ) /* EOF */ { __lseek( handle, ((long)reduce_idx - (long)amount_read)+1L, SEEK_CUR ); total_len += finish_idx; return( total_len ); } if( buffer[ reduce_idx ] != '\r' ) { buffer[ finish_idx++ ] = buffer[ reduce_idx ]; }; } total_len += finish_idx; buffer += finish_idx; read_len -= finish_idx; if( iomode_flags & _ISTTY ) { break; /* 04-feb-88, FWC */ } } while( read_len != 0 ); } return( total_len ); }
static int __F_NAME(__sopen,__wsopen)( const CHAR_TYPE *name, int mode, int shflag, va_list args ) { int rwmode; int handle; int attr; int permission; unsigned iomode_flags; tiny_ret_t rc; char dummy; #ifdef __WIDECHAR__ char mbName[MB_CUR_MAX * _MAX_PATH]; /* single-byte char */ #endif handle = -1; rc = 0; while( *name == STRING( ' ' ) ) ++name; #ifdef __WIDECHAR__ /*** If necessary, convert the wide filename to multibyte form ***/ if( wcstombs( mbName, name, sizeof( mbName ) ) == -1 ) { mbName[0] = '\0'; } #endif rwmode = mode & ( O_RDONLY | O_WRONLY | O_RDWR | O_NOINHERIT ); if( _dos_open( __F_NAME(name,mbName), rwmode | shflag, &handle ) == 0 ) { if( handle >= __NFiles ) { TinyClose( handle ); __set_errno( EMFILE ); return( -1 ); } } /* 17-apr-90 05-sep-91 */ if( (mode & (O_RDONLY | O_WRONLY | O_RDWR)) != O_RDONLY ) { if( handle != -1 ) { if( ! isatty( handle ) ) { /* if not a device */ #if 0 rc = TinyAccess( name, 0 ); /* check for existence */ if( TINY_ERROR( rc ) ) { /* file does not exist */ TinyClose( handle ); /* close whatever file we got */ handle = -1; } else if( mode & O_EXCL ) { /* must not exist */ #else /* Don't need to do the access check, since the file was opened and therefore must exist (TinyOpen can't create a file). We don't want to do the check because there are classes of items in the file system namespace that are not devices, but the TinyAccess will fail on (e.g. named pipes). */ /* must not exist if O_CREAT specified */ if( (mode & O_EXCL) && (mode & O_CREAT) ) { #endif TinyClose( handle ); __set_errno( EEXIST ); return( -1 ); } else if( mode & O_TRUNC ) { /* truncate file */ rc = TinyWrite( handle, &dummy, 0 ); if( TINY_ERROR( rc ) ) { TinyClose( handle ); return( __set_errno_dos( TINY_INFO( rc ) ) ); } } } } } if( handle == -1 ) { /* could not open */ if( (mode & O_CREAT) == 0 || _RWD_doserrno != E_nofile ) { return( -1 ); } /* creating the file */ permission = va_arg( args, int ); va_end( args ); if( permission == 0 ) permission = S_IWRITE | S_IREAD; permission &= ~_RWD_umaskval; /* 05-jan-95 */ attr = 0; if(( permission & S_IWRITE) == 0 ) attr = _A_RDONLY; #if 0 /* remove this support because it is not consistently available */ if( _RWD_osmajor >= 5 #ifdef __DOS_EXT__ && !_IsFlashTek() && !_IsRational() #endif ) { /* this function is only available in version DOS 5 and up */ /* this new way was added to handle the case of creating a */ /* new file with read-only access, but with a writeable */ /* file handle */ #ifdef __WIDECHAR__ rc = TinyCreateEx( mbName, rwmode|shflag, attr, TIO_OPEN ); #else rc = TinyCreateEx( name, rwmode|shflag, attr, TIO_OPEN ); #endif if( TINY_ERROR( rc ) ) { return( __set_errno_dos( TINY_INFO( rc ) ) ); } handle = TINY_INFO( rc ); } else #endif { /* do it the old way */ if( _dos_creat( __F_NAME(name,mbName), attr, &handle ) ) { return( -1 ); } if( handle >= __NFiles ) { TinyClose( handle ); __set_errno( EMFILE ); return( -1 ); } /* 21-nov-90 AFS: the file is created so now the file must be */ /* opened with the correct share permissions */ if( shflag != 0 ) { rc = TinyClose( handle ); if( TINY_ERROR( rc ) ) { return( __set_errno_dos( TINY_INFO( rc ) ) ); } if( _dos_open( __F_NAME(name,mbName), rwmode | shflag, &handle ) ) { return( -1 ); } /* handle does not equal -1 now */ } } } iomode_flags = __GetIOMode( handle ); iomode_flags &= ~(_READ|_WRITE|_APPEND|_BINARY); /* 11-aug-88 */ if( isatty( handle ) ) iomode_flags |= _ISTTY; rwmode &= ~O_NOINHERIT; if( rwmode == O_RDWR ) iomode_flags |= _READ | _WRITE; if( rwmode == O_RDONLY) iomode_flags |= _READ; if( rwmode == O_WRONLY) iomode_flags |= _WRITE; if( mode & O_APPEND ) iomode_flags |= _APPEND; if( mode & (O_BINARY|O_TEXT) ) { if( mode & O_BINARY ) iomode_flags |= _BINARY; } else { if( _RWD_fmode == O_BINARY ) iomode_flags |= _BINARY; } __SetIOMode( handle, iomode_flags ); #ifdef DEFAULT_WINDOWING if( _WindowsNewWindow != 0 ) { if( !__F_NAME(stricmp,wcscmp)( name, STRING( "con" ) ) ) { _WindowsNewWindow( NULL, handle, -1 ); } } #endif return( handle ); } #if 0 /* couldn't find any user; please re-enable if it's necessary */ #ifndef __WIDECHAR__ /* compile one version only */ int __set_binary( int handle ) { unsigned iomode_flags; __ChkTTYIOMode( handle ); iomode_flags = __GetIOMode( handle ); iomode_flags |= _BINARY; __SetIOMode( handle, iomode_flags ); if( iomode_flags & _ISTTY ) { tiny_ret_t rc; rc = TinyGetDeviceInfo( handle ); if( TINY_ERROR( rc ) ) { return( __set_errno_dos( TINY_INFO( rc ) ) ); } rc = TinySetDeviceInfo( handle, TINY_INFO(rc) | TIO_CTL_RAW ); if( TINY_ERROR( rc ) ) { return( __set_errno_dos( TINY_INFO( rc ) ) ); } } return( 0 ); }
_WCRTLINK int read( int handle, void *buf, unsigned len ) #endif { unsigned read_len, total_len; unsigned reduce_idx, finish_idx; unsigned iomode_flags; char *buffer = buf; #if defined(__NT__) DWORD amount_read; BOOL rc; HANDLE h; #elif defined(__WARP__) ULONG amount_read; #elif defined(__OS2_286__) USHORT amount_read; #else unsigned amount_read; #endif #if !defined(__NT__) tiny_ret_t rc; #endif #ifdef DEFAULT_WINDOWING LPWDATA res; #endif __handle_check( handle, -1 ); __ChkTTYIOMode( handle ); iomode_flags = __GetIOMode( handle ); if( iomode_flags == 0 ) { #if defined( __WINDOWS__ ) || defined( __WINDOWS_386__ ) return( _lread( handle, buffer, len ) ); #else _RWD_errno = EBADF; return( -1 ); #endif } if( !(iomode_flags & _READ) ) { _RWD_errno = EACCES; /* changed from EBADF to EACCES 23-feb-89 */ return( -1 ); } #ifdef __NT__ h = __getOSHandle( handle ); #endif if( iomode_flags & _BINARY ) { /* if binary mode */ #ifdef DEFAULT_WINDOWING if( _WindowsStdin != 0 && (res = _WindowsIsWindowedHandle( handle )) != 0 ) { total_len = _WindowsStdin( res, buffer, len ); rc = 0; } else #endif { #if defined(__NT__) rc = ReadFile( h, buffer, len, &amount_read, NULL ); total_len = amount_read; if( !rc ) { if (GetLastError() == ERROR_BROKEN_PIPE) return total_len; return( __set_errno_nt() ); } #elif defined( __OS2__ ) rc = DosRead( handle, buffer, len, &amount_read ); total_len = amount_read; #else rc = TinyRead( handle, buffer, len ); total_len = TINY_LINFO( rc ); #endif } #if !defined(__NT__) if( TINY_ERROR( rc ) ) { return( __set_errno_dos( TINY_INFO( rc ) ) ); } #endif } else { _AccessFileH( handle ); total_len = 0; read_len = len; do { #ifdef DEFAULT_WINDOWING if( _WindowsStdin != 0 && (res = _WindowsIsWindowedHandle( handle )) != 0L ) { amount_read = _WindowsStdin( res, buffer, read_len ); rc = 0; } else #endif { #if defined(__NT__) rc = ReadFile( h, buffer, read_len, &amount_read, NULL ); if( !rc ) { _ReleaseFileH( handle ); if( GetLastError() == ERROR_BROKEN_PIPE ) return total_len; return( __set_errno_nt() ); } #elif defined( __OS2__ ) rc = DosRead( handle, buffer, read_len, &amount_read ); #else rc = TinyRead( handle, buffer, read_len ); amount_read = TINY_LINFO( rc ); #endif } #if !defined(__NT__) if( TINY_ERROR( rc ) ) { _ReleaseFileH( handle ); return( __set_errno_dos( TINY_INFO( rc ) ) ); } #endif if( amount_read == 0 ) { /* EOF */ break; } reduce_idx = 0; finish_idx = reduce_idx; for( ; reduce_idx < amount_read; ++reduce_idx ) { if( buffer[ reduce_idx ] == 0x1a ) { /* EOF */ __lseek( handle, ((long)reduce_idx - (long)amount_read)+1L, SEEK_CUR ); total_len += finish_idx; _ReleaseFileH( handle ); return( total_len ); } if( buffer[ reduce_idx ] != '\r' ) { buffer[ finish_idx++ ] = buffer[ reduce_idx ]; } } total_len += finish_idx; buffer += finish_idx; read_len -= finish_idx; if( iomode_flags & _ISTTY ) { break; /* 04-feb-88, FWC */ } } while( read_len != 0 ); _ReleaseFileH( handle ); } return( total_len ); }