_WCRTLINK int _findnext( long handle, struct _finddata_t *fileinfo ) #endif #endif { #ifdef __NT__ WIN32_FIND_DATA ffb; BOOL rc; /*** Try to find another matching file ***/ rc = FIND_NEXT( (HANDLE)handle, &ffb ); if( rc == FALSE ) { return( __set_errno_nt() ); } if( !CHECK_FIND_NEXT_ATTR( (HANDLE)handle, FIND_ATTR, &ffb ) ) { return( __set_errno_dos( ERROR_FILE_NOT_FOUND ) ); } /*** Got one! ***/ #ifdef __INT64__ __F_NAME(__nt_finddatai64_cvt,__nt_wfinddatai64_cvt)( &ffb, fileinfo ); #else __F_NAME(__nt_finddata_cvt,__nt_wfinddata_cvt)( &ffb, fileinfo ); #endif #elif defined( __OS2__ ) APIRET rc; FF_BUFFER ffb; OS_UINT searchcount = 1; rc = DosFindNext( (HDIR)handle, &ffb, sizeof( ffb ), &searchcount ); if( rc != 0 ) { return( __set_errno_dos( rc ) ); } /*** Got one! ***/ #ifdef __INT64__ __F_NAME(__os2_finddatai64_cvt,__os2_wfinddatai64_cvt)( &ffb, fileinfo ); #else __F_NAME(__os2_finddata_cvt,__os2_wfinddata_cvt)( &ffb, fileinfo ); #endif #elif defined( __RDOS__ ) RDOSFINDTYPE * findbuf = (RDOSFINDTYPE*) handle; findbuf->entry++; if( __rdos_finddata_get( findbuf, fileinfo ) ) return( 0 ); else return( -1 ); #else /* DOS */ if( __F_NAME(_dos_findnext,_wdos_findnext)( (DOSFINDTYPE *)handle ) ) { return( -1 ); } #ifdef __INT64__ __F_NAME(__dos_finddatai64_cvt,__dos_wfinddatai64_cvt)( (DOSFINDTYPE *)handle, fileinfo ); #else __F_NAME(__dos_finddata_cvt,__dos_wfinddata_cvt)( (DOSFINDTYPE *)handle, fileinfo ); #endif #endif return( 0 ); }
_WCRTLINK int dup( int old_hid ) { HANDLE new_handle; int hid; HANDLE cprocess; __handle_check( old_hid, -1 ); // First try to get the required slot. // No point in creating a new handle only to not use it. JBS 99/11/01 hid = __allocPOSIXHandle( DUMMY_HANDLE ); if( hid == -1 ) { return( -1 ); } cprocess = GetCurrentProcess(); if( !DuplicateHandle( cprocess, __getOSHandle( old_hid ), cprocess, &new_handle, 0, TRUE, DUPLICATE_SAME_ACCESS ) ) { // Give back the slot we got __freePOSIXHandle( hid ); return( __set_errno_nt() ); } // Now use the slot we got __setOSHandle( hid, new_handle ); // JBS 99/11/01 __SetIOMode( hid, __GetIOMode( old_hid ) ); return( hid ); }
_WCRTLINK int __F_NAME(unlink,_wunlink)( const CHAR_TYPE *filename ) { if( __lib_DeleteFile( filename ) == FALSE ) { return( __set_errno_nt() ); } return( 0 ); }
int __close( int hid ) { int is_closed; int rc; HANDLE h; #ifdef DEFAULT_WINDOWING LPWDATA res; #endif __handle_check( hid, -1 ); is_closed = 0; rc = 0; h = __getOSHandle( hid ); #ifdef DEFAULT_WINDOWING if( _WindowsCloseWindow != 0 ) { res = _WindowsIsWindowedHandle( hid ); if( res != NULL ) { _WindowsRemoveWindowedHandle( hid ); _WindowsCloseWindow( res ); is_closed = 1; } } #endif if( !is_closed && !CloseHandle( h ) ) { rc = __set_errno_nt(); } __freePOSIXHandle( hid ); __SetIOMode_nogrow( hid, 0 ); return( rc ); }
_WCRTLINK long __lseek( int handle, long offset, int origin ) { long pos; __handle_check( handle, -1 ); #if defined( __OS2__ ) { APIRET rc; rc = DosChgFilePtr( handle, offset, origin, (PULONG)&pos ); if( rc != 0 ) { return( __set_errno_dos( rc ) ); } } #elif defined( __NT__ ) pos = SetFilePointer( __getOSHandle( handle ), offset, 0, origin ); if( pos == INVALID_SET_FILE_POINTER ) { return( __set_errno_nt() ); } #elif defined( __DOS__ ) || defined( __WINDOWS__ ) { tiny_ret_t rc; rc = TinyLSeek( handle, offset, origin, (u32_stk_ptr)&pos ); if( TINY_ERROR( rc ) ) { return( __set_errno_dos( TINY_INFO( rc ) ) ); } } #endif return( pos ); }
_WCRTLINK int fsync( int handle ) /*******************************/ { int ret = 0; __handle_check( handle, -1 ); #if defined(__DOS__) || defined(__WINDOWS__) ret = _dos_commit( handle ); #elif defined(__NT__) if( !FlushFileBuffers( __getOSHandle( handle ) ) ) { __set_errno_nt(); ret = -1; } #elif defined(__OS2__) if( DosBufReset( handle ) != 0 ) { __set_errno( EBADF ); ret = -1; } #elif defined(__NETWARE__) if( FEFlushWrite( handle ) != 0 ) { __set_errno( EBADF ); ret = -1; } #else #error Unknown target system #endif return( ret ); }
_WCRTLINK CHAR_TYPE *__F_NAME(getcwd,_wgetcwd)( CHAR_TYPE *buf, size_t size ) { CHAR_TYPE path[ _MAX_PATH ]; DWORD realsize; /*** Get the current directory ***/ realsize = __lib_GetCurrentDirectory( _MAX_PATH, path ); if( realsize == 0 ) { __set_errno_nt(); return( NULL ); } if( buf == NULL ) { buf = lib_malloc( max( size, realsize + 1 ) * CHARSIZE ); if( buf == NULL ) { __set_errno( ENOMEM ); return( NULL ); } } else { if( realsize + 1 > size ) { __set_errno( ERANGE ); return( NULL ); } } return( memcpy( buf, path, ( realsize + 1 ) * CHARSIZE ) ); }
_WCRTLINK int unlock( int hid, unsigned long offset, unsigned long nbytes ) { __handle_check( hid, -1 ); if( !UnlockFile( __getOSHandle( hid ), offset, 0L, nbytes, 0L ) ) { return( __set_errno_nt() ); } return( 0 ); }
_WCRTLINK int __F_NAME(rmdir,_wrmdir)( const CHAR_TYPE *path ) { BOOL rc; rc = __lib_RemoveDirectory( path ); if( rc == FALSE ) { return( __set_errno_nt() ); } return( 0 ); }
_WCRTLINK int __F_NAME(chdir,_wchdir)( const CHAR_TYPE *path ) { BOOL rc; rc = __lib_SetCurrentDirectory( path ); if( rc == FALSE ) { return( __set_errno_nt() ); } return( 0 ); }
static int os_write( int handle, const void *buffer, unsigned len, unsigned *amt ) /********************************************************************************/ { #ifdef DEFAULT_WINDOWING LPWDATA res; #endif #if defined(__NT__) HANDLE h; int rc; #else tiny_ret_t rc; #endif rc = 0; #ifdef DEFAULT_WINDOWING if( _WindowsStdout != 0 && (res = _WindowsIsWindowedHandle( handle )) != 0 ) { *amt = _WindowsStdout( res, buffer, len ); } else #endif { #if defined(__NT__) h = __getOSHandle( handle ); if( !WriteFile( h, (LPCVOID)buffer, (DWORD)len, (LPDWORD)amt, NULL ) ) { rc = __set_errno_nt(); } #elif defined(__OS2_286__) rc = DosWrite( handle, (PVOID)buffer, (USHORT)len, (PUSHORT)amt ); #elif defined(__OS2__) rc = DosWrite( handle, (PVOID)buffer, (ULONG)len, (PULONG)amt ); #else rc = TinyWrite( handle, buffer, len ); *amt = TINY_LINFO( rc ); if( TINY_OK( rc ) ) { rc = 0; } #endif } #if !defined(__NT__) if( TINY_ERROR( rc ) ) { rc = __set_errno_dos( TINY_INFO( rc ) ); } #endif if( *amt != len ) { rc = ENOSPC; __set_errno( rc ); } return( rc ); }
_WCRTLINK int __F_NAME(closedir,_wclosedir)( DIR_TYPE *dirp ) /***********************************************************/ { if( dirp == NULL || dirp->d_first == _DIR_CLOSED ) { return( __set_errno_dos( ERROR_INVALID_HANDLE ) ); } if( !FindClose( DIR_HANDLE_OF( dirp ) ) ) { return( __set_errno_nt() ); } dirp->d_first = _DIR_CLOSED; if( dirp->d_openpath != NULL ) free( dirp->d_openpath ); lib_free( dirp ); return( 0 ); }
static int os_write( int handle, const void *buffer, unsigned len, unsigned *amt ) /********************************************************************************/ { int rc; #ifdef DEFAULT_WINDOWING LPWDATA res; #endif #if defined(__NT__) HANDLE h; #elif defined(__OS2__) APIRET rc1; #else tiny_ret_t rc1; #endif rc = 0; #ifdef DEFAULT_WINDOWING if( _WindowsStdout != NULL && (res = _WindowsIsWindowedHandle( handle )) != NULL ) { *amt = _WindowsStdout( res, buffer, len ); } else #endif { #if defined(__NT__) h = __getOSHandle( handle ); if( !WriteFile( h, (LPCVOID)buffer, (DWORD)len, (LPDWORD)amt, NULL ) ) { return( __set_errno_nt() ); } #elif defined(__OS2__) rc1 = DosWrite( handle, (PVOID)buffer, (OS_UINT)len, (OS_PUINT)amt ); if( rc1 ) { return( __set_errno_dos( rc1 ) ); } #else rc1 = TinyWrite( handle, buffer, len ); if( TINY_ERROR( rc1 ) ) { return( __set_errno_dos( TINY_INFO( rc1 ) ) ); } *amt = TINY_LINFO( rc1 ); #endif } if( *amt != len ) { rc = ENOSPC; _RWD_errno = rc; } return( rc ); }
static DIR_TYPE *__F_NAME(___opendir,___wopendir)( const CHAR_TYPE *dirname, DIR_TYPE *dirp ) /*******************************************************************************************/ { WIN32_FIND_DATA ffb; HANDLE h; if( dirp->d_first != _DIR_CLOSED ) { FindClose( DIR_HANDLE_OF( dirp ) ); dirp->d_first = _DIR_CLOSED; } h = __lib_FindFirstFile( dirname, &ffb ); if( h == INVALID_HANDLE_VALUE ) { __set_errno_nt(); return( NULL ); } DIR_HANDLE_OF( dirp ) = h; __GetNTDirInfo( dirp, &ffb ); dirp->d_first = _DIR_ISFIRST; return( dirp ); }
_WCRTLINK CHAR_TYPE *__F_NAME(_sys_fullpath,_sys_wfullpath) ( CHAR_TYPE *buff, const CHAR_TYPE *path, size_t size ) /*********************************************************************/ { #if defined(__NT__) CHAR_TYPE *filepart; DWORD rc; if( __F_NAME(stricmp,_wcsicmp)( path, STRING( "con" ) ) == 0 ) { _WILL_FIT( 3 ); return( __F_NAME(strcpy,wcscpy)( buff, STRING( "con" ) ) ); } /*** Get the full pathname ***/ rc = __lib_GetFullPathName( path, size, buff, &filepart ); // If the buffer is too small, the return value is the size of // the buffer, in TCHARs, required to hold the path. // If the function fails, the return value is zero. To get extended error // information, call GetLastError. if( ( rc == 0 ) || ( rc > size ) ) { __set_errno_nt(); return( NULL ); } return( buff ); #elif defined(__WARP__) APIRET rc; char root[ 4 ]; /* SBCS: room for drive, ':', '\\', and null */ #ifdef __WIDECHAR__ char mbBuff[ _MAX_PATH * MB_CUR_MAX ]; char mbPath[ _MAX_PATH * MB_CUR_MAX ]; #endif if( __F_NAME(isalpha,iswalpha)( path[ 0 ] ) && ( path[ 1 ] == STRING( ':' ) ) && ( path[ 2 ] == STRING( '\\' ) ) ) { int i; i = __F_NAME(strlen,wcslen)( path ); _WILL_FIT( i ); __F_NAME(strcpy,wcscpy)( buff, path ); return( buff ); } /* * Check for x:filename.ext when drive x doesn't exist. In this * case, return x:\filename.ext, not NULL, to be consistent with * MS and with the NT version of _fullpath. */ if( __F_NAME(isalpha,iswalpha)( path[ 0 ] ) && path[ 1 ] == STRING( ':' ) ) { /*** We got this far, so path can't start with letter:\ ***/ root[ 0 ] = (char)path[ 0 ]; root[ 1 ] = ':'; root[ 2 ] = '\\'; root[ 3 ] = '\0'; rc = DosQueryPathInfo( root, FIL_QUERYFULLNAME, buff, size ); if( rc != NO_ERROR ) { /*** Drive does not exist; return x:\filename.ext ***/ _WILL_FIT( __F_NAME(strlen,wcslen)( &path[ 2 ] ) + 3 ); buff[ 0 ] = root[ 0 ]; buff[ 1 ] = STRING( ':' ); buff[ 2 ] = STRING( '\\' ); __F_NAME(strcpy,wcscpy)( &buff[ 3 ], &path[ 2 ] ); return( buff ); } } #ifdef __WIDECHAR__ if( wcstombs( mbPath, path, sizeof( mbPath ) ) == (size_t)-1 ) { return( NULL ); } rc = DosQueryPathInfo( (PSZ)mbPath, FIL_QUERYFULLNAME, mbBuff, sizeof( mbBuff ) ); #else rc = DosQueryPathInfo( (PSZ)path, FIL_QUERYFULLNAME, buff, size ); #endif if( rc != 0 ) { __set_errno_dos( rc ); return( NULL ); } #ifdef __WIDECHAR__ if( mbstowcs( buff, mbBuff, size ) != (size_t)-1 ) { return( buff ); } else { return( NULL ); } #else return( buff ); #endif #elif defined(__QNX__) || defined( __NETWARE__ ) size_t len; char temp_dir[ _MAX_PATH ]; #if defined(__NETWARE__) if( ConvertNameToFullPath( path, temp_dir ) != 0 ) { return( NULL ); } #else if( __qnx_fullpath( temp_dir, path ) == NULL ) { return( NULL ); } #endif len = strlen( temp_dir ); if( len >= size ) { __set_errno( ERANGE ); return( NULL ); } return( strcpy( buff, temp_dir ) ); #elif defined(__UNIX__) const char *p; char *q; size_t len; char curr_dir[ _MAX_PATH ]; p = path; q = buff; if( ! _IS_SLASH( p[ 0 ] ) ) { if( getcwd( curr_dir, sizeof( curr_dir ) ) == NULL ) { __set_errno( ENOENT ); return( NULL ); } len = strlen( curr_dir ); _WILL_FIT( len ); strcpy( q, curr_dir ); q += len; if( q[ -1 ] != '/' ) { _WILL_FIT( 1 ); *(q++) = '/'; } for( ;; ) { if( p[ 0 ] == '\0' ) break; if( p[ 0 ] != '.' ) { _WILL_FIT( 1 ); *(q++) = *(p++); continue; } ++p; if( _IS_SLASH( p[ 0 ] ) ) { /* ignore "./" in directory specs */ if( ! _IS_SLASH( q[ -1 ] ) ) { *q++ = '/'; } ++p; continue; } if( p[ 0 ] == '\0' ) break; if( p[ 0 ] == '.' && _IS_SLASH( p[ 1 ] ) ) { /* go up a directory for a "../" */ p += 2; if( ! _IS_SLASH( q[ -1 ] ) ) { return( NULL ); } q -= 2; for( ;; ) { if( q < buff ) { return( NULL ); } if( _IS_SLASH( *q ) ) break; --q; } ++q; *q = '\0'; continue; } _WILL_FIT( 1 ); *(q++) = '.'; } *q = '\0'; } else { len = strlen( p ); _WILL_FIT( len ); strcpy( q, p ); } return( buff ); #else const CHAR_TYPE *p; CHAR_TYPE *q; size_t len; int path_drive_idx; char curr_dir[ _MAX_PATH ]; p = path; q = buff; _WILL_FIT( 2 ); if( __F_NAME(isalpha,iswalpha)( p[ 0 ] ) && p[ 1 ] == STRING( ':' ) ) { path_drive_idx = ( __F_NAME(tolower,towlower)( p[ 0 ] ) - STRING( 'a' ) ) + 1; q[ 0 ] = p[ 0 ]; q[ 1 ] = p[ 1 ]; p += 2; } else { #if defined(__OS2__) ULONG drive_map; OS_UINT os2_drive; if( DosQCurDisk( &os2_drive, &drive_map ) ) { __set_errno( ENOENT ); return( NULL ); } path_drive_idx = os2_drive; #else path_drive_idx = TinyGetCurrDrive() + 1; #endif q[ 0 ] = STRING( 'A' ) + ( path_drive_idx - 1 ); q[ 1 ] = STRING( ':' ); } q += 2; if( ! _IS_SLASH( p[ 0 ] ) ) { #if defined(__OS2__) OS_UINT dir_len = sizeof( curr_dir ); if( DosQCurDir( path_drive_idx, curr_dir, &dir_len ) ) { __set_errno( ENOENT ); return( NULL ); } #else if( __getdcwd( curr_dir, path_drive_idx ) ) { __set_errno( ENOENT ); return( NULL ); } #endif len = strlen( curr_dir ); if( curr_dir[ 0 ] != '\\' ) { _WILL_FIT( 1 ); *(q++) = STRING( '\\' ); } _WILL_FIT( len ); #ifdef __WIDECHAR__ if( mbstowcs( q, curr_dir, len + 1 ) == (size_t)-1 ) { return( NULL ); } #else strcpy( q, curr_dir ); #endif q += len; if( q[ -1 ] != STRING( '\\' ) ) { _WILL_FIT( 1 ); *(q++) = STRING( '\\' ); } for( ;; ) { if( p[ 0 ] == NULLCHAR ) break; if( p[ 0 ] != STRING( '.' ) ) { _WILL_FIT( 1 ); *(q++) = *(p++); continue; } ++p; // at least '.' if( _IS_SLASH( p[ 0 ] ) ) { /* ignore "./" in directory specs */ if( ! _IS_SLASH( q[ -1 ] ) ) { *q++ = STRING( '\\' ); } ++p; continue; } if( p[ 0 ] == NULLCHAR ) break; if( p[ 0 ] == STRING( '.' ) ) { /* .. */ ++p; if( _IS_SLASH( p[ 0 ] ) ) { /* "../" */ ++p; } if( ! _IS_SLASH( q[ -1 ] ) ) { return( NULL ); } q -= 2; for( ;; ) { if( q < buff ) { return( NULL ); } if( _IS_SLASH( *q ) ) break; if( *q == STRING( ':' ) ) { ++q; *q = STRING( '\\' ); break; } --q; } ++q; *q = NULLCHAR; continue; } _WILL_FIT( 1 ); *(q++) = STRING( '.' ); } *q = NULLCHAR; } else { len = __F_NAME(strlen,wcslen)( p ); _WILL_FIT( len ); __F_NAME(strcpy,wcscpy)( q, p ); } /* force to all backslashes */ for( q = buff; *q != NULLCHAR; ++q ) { if( *q == STRING( '/' ) ) { *q = STRING( '\\' ); } } return( buff ); #endif }
int __F_NAME(_dospawn,_wdospawn)( int mode, CHAR_TYPE *pgmname, CHAR_TYPE *cmdline, CHAR_TYPE *envp, const CHAR_TYPE * const argv[] ) { STARTUPINFO sinfo; PROCESS_INFORMATION pinfo; DWORD rc; BOOL osrc; __F_NAME(__ccmdline,__wccmdline)( pgmname, argv, cmdline, 0 ); memset( &sinfo, 0, sizeof( sinfo ) ); sinfo.cb = sizeof( sinfo ); // set ShowWindow default value for nCmdShow parameter sinfo.dwFlags = STARTF_USESHOWWINDOW; if( mode == P_DETACH ) { sinfo.wShowWindow = SW_HIDE; } else { sinfo.wShowWindow = SW_SHOWNORMAL; } /* When passing in Unicode environments, the OS may not know which code * page to use when translating to MBCS in spawned program's startup * code. Result: Possible corruption of Unicode environment variables. */ osrc = __lib_CreateProcess( cmdline, (mode != P_DETACH), envp, &sinfo, &pinfo ); if( osrc == FALSE ) { DWORD err; err = GetLastError(); if( (err == ERROR_ACCESS_DENIED) || (err == ERROR_BAD_EXE_FORMAT) || (err == ERROR_BAD_PATHNAME) ) { err = ERROR_FILE_NOT_FOUND; } return( __set_errno_dos( err ) ); } if( mode == P_WAIT ) { if( WIN32_IS_WIN32S ) { // this is WIN32s Sleep( 1000 ); rc = STILL_ACTIVE; while( rc == STILL_ACTIVE ) { Sleep( 100 ); if( !GetExitCodeProcess( pinfo.hProcess, &rc ) ) { return( __set_errno_nt() ); } } } else { /* this is WIN32 or Windows95 */ if( WaitForSingleObject( pinfo.hProcess, INFINITE ) == 0 ) { GetExitCodeProcess( pinfo.hProcess, &rc ); } else { rc = __set_errno_nt(); } } CloseHandle( pinfo.hProcess ); } else if( mode == P_DETACH ) { /* P_DETACH should just return 0 for success */ rc = 0; } else { /* no difference between P_NOWAIT and P_NOWAITO */ rc = (int)pinfo.hProcess; /* cwait will close (free) the handle */ } CloseHandle( pinfo.hThread ); return( rc ); }
_WCRTLINK long _findfirst( const char *filespec, struct _finddata_t *fileinfo ) #endif #endif /******************************************************************************/ { #if defined( __NT__ ) WIN32_FIND_DATA ffb; HANDLE h; /*** Initialize the find ***/ h = FIND_FIRST( filespec, &ffb ); if( h == INVALID_HANDLE_VALUE ) { return( __set_errno_nt() ); } /*** Look for the first file ***/ if( !CHECK_FIND_NEXT_ATTR( h, FIND_ATTR, &ffb ) ) { FindClose( h ); return( __set_errno_dos( ERROR_FILE_NOT_FOUND ) ); } /*** Got one! ***/ #ifdef __INT64__ __F_NAME(__nt_finddatai64_cvt,__nt_wfinddatai64_cvt)( &ffb, fileinfo ); #else __F_NAME(__nt_finddata_cvt,__nt_wfinddata_cvt)( &ffb, fileinfo ); #endif return( (long)h ); #elif defined( __OS2__ ) APIRET rc; HDIR h = HDIR_CREATE; FF_BUFFER ffb; OS_UINT searchcount = 1; #ifdef __WIDECHAR__ char mbFilespec[MB_CUR_MAX * _MAX_PATH]; if( wcstombs( mbFilespec, filespec, sizeof( mbFilespec ) ) == -1 ) { mbFilespec[0] = '\0'; } #endif rc = DosFindFirst( (char*)__F_NAME(filespec,mbFilespec), &h, FIND_ATTR, &ffb, sizeof( ffb ), &searchcount, FF_LEVEL ); if( rc != 0 ) { return( -1L ); } /*** Got one! ***/ #ifdef __INT64__ __F_NAME(__os2_finddatai64_cvt,__os2_wfinddatai64_cvt)( &ffb, fileinfo ); #else __F_NAME(__os2_finddata_cvt,__os2_wfinddata_cvt)( &ffb, fileinfo ); #endif return( (long)h ); #elif defined( __RDOS__ ) RDOSFINDTYPE * findbuf; findbuf = (RDOSFINDTYPE*) lib_malloc( sizeof( RDOSFINDTYPE ) ); if( findbuf == NULL ) return( -1L ); findbuf->handle = RdosOpenDir( filespec ); findbuf->entry = 0; if( __rdos_finddata_get( findbuf, fileinfo ) ) return( (long) findbuf ); else { lib_free( findbuf ); return( -1 ); } #else /* DOS */ DOSFINDTYPE *findbuf; unsigned rc; /*** Start a new find using _dos_findfirst ***/ findbuf = (DOSFINDTYPE*) lib_malloc( sizeof( DOSFINDTYPE ) ); if( findbuf == NULL ) return( -1L ); rc = __F_NAME(_dos_findfirst,_wdos_findfirst)( filespec, FIND_ATTR, findbuf ); if( rc != 0 ) { lib_free( findbuf ); return( -1L ); } /*** Got one! ***/ #ifdef __INT64__ __F_NAME(__dos_finddatai64_cvt,__dos_wfinddatai64_cvt)( findbuf, fileinfo ); #else __F_NAME(__dos_finddata_cvt,__dos_wfinddata_cvt)( findbuf, fileinfo ); #endif return( (long)findbuf ); #endif }
_WCRTLINK int _pipe( int *phandles, unsigned psize, int textmode ) /****************************************************************/ { #if defined(__NT__) HANDLE hRead, hWrite; // HANDLE osHandle; // removed by JBS BOOL rc; SECURITY_ATTRIBUTES sa; #elif defined(__OS2__) && defined(__386__) HFILE hRead, hWrite; APIRET rc; #elif defined(__OS2__) && !defined(__386__) HFILE hRead, hWrite; USHORT rc; #endif int hReadPosix, hWritePosix; // removed by JBS - allow O_NOINHERIT // /*** Sanity check ***/ // if( textmode != 0 && textmode != _O_TEXT && textmode != _O_BINARY ) { // return( -1 ); // } /*** Create the pipes (note that psize==0 ==> use default size) ***/ #if defined(__NT__) sa.nLength = sizeof( SECURITY_ATTRIBUTES ); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = (((textmode & O_NOINHERIT)==O_NOINHERIT)?FALSE:TRUE); rc = CreatePipe( &hRead, &hWrite, &sa, psize ); if( rc == FALSE ) { return( __set_errno_nt() ); } #elif defined(__OS2__) if( psize == 0 ) psize = 4096; #ifdef __386__ rc = DosCreatePipe( &hRead, &hWrite, psize ); #else rc = DosMakePipe( &hRead, &hWrite, psize ); #endif if( rc != NO_ERROR ) { __set_errno( ENOMEM ); return( -1 ); } #endif // removed by JBS - used sa struct instead // /*** Make read handle inheritable ***/ // #ifdef __NT__ // rc = DuplicateHandle( GetCurrentProcess(), hRead, GetCurrentProcess(), // &osHandle, 0, TRUE, DUPLICATE_SAME_ACCESS ); // if( rc == FALSE ) { // CloseHandle( hRead ); // CloseHandle( hWrite ); // return( -1 ); // } // CloseHandle( hRead ); // hRead = osHandle; // #elif defined(__OS2__) // /* Handle is inheritable by default */ // #endif // // /*** Make write handle inheritable ***/ // #ifdef __NT__ // rc = DuplicateHandle( GetCurrentProcess(), hWrite, GetCurrentProcess(), // &osHandle, 0, TRUE, DUPLICATE_SAME_ACCESS ); // if( rc == FALSE ) { // CloseHandle( hRead ); // CloseHandle( hWrite ); // return( -1 ); // } // CloseHandle( hWrite ); // hWrite = osHandle; // #elif defined(__OS2__) // /* Handle is inheritable by default */ // #endif /*** Initialize the POSIX-level handles ***/ hReadPosix = _hdopen( (int)hRead, textmode|_O_RDONLY ); hWritePosix = _hdopen( (int)hWrite, textmode|_O_WRONLY ); if( hReadPosix == -1 || hWritePosix == -1 ) { if( hReadPosix != -1 ) { close( hReadPosix ); } else { #if defined(__NT__) CloseHandle( hRead ); #elif defined(__OS2__) DosClose( hRead ); #endif } if( hWritePosix != -1 ) { close( hWritePosix ); } else { #if defined(__NT__) CloseHandle( hWrite ); #elif defined(__OS2__) DosClose( hWrite ); #endif } return( -1 ); } /*** Store the new POSIX handles in return buffer ***/ phandles[0] = hReadPosix; phandles[1] = hWritePosix; 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 ); }
_WCRTLINK int write( int handle, const void *buffer, unsigned len ) #endif /**********************************************************************/ { unsigned iomode_flags; char *buf; unsigned buf_size; unsigned len_written, i, j; #if defined(__NT__) HANDLE h; LONG cur_ptr_low; LONG cur_ptr_high; DWORD rc1; #else tiny_ret_t rc1; #endif int rc2; __handle_check( handle, -1 ); iomode_flags = __GetIOMode( handle ); if( iomode_flags == 0 ) { #if defined(__WINDOWS__) || defined(__WINDOWS_386__) // How can we write to the handle if we never opened it? JBS return( _lwrite( handle, buffer, len ) ); #else __set_errno( EBADF ); return( -1 ); #endif } if( !(iomode_flags & _WRITE) ) { __set_errno( EACCES ); /* changed from EBADF to EACCES 23-feb-89 */ return( -1 ); } #if defined(__NT__) h = __getOSHandle( handle ); #endif // put a semaphore around our writes _AccessFileH( handle ); if( (iomode_flags & _APPEND) && !(iomode_flags & _ISTTY) ) { #if defined(__NT__) if( GetFileType( h ) == FILE_TYPE_DISK ) { cur_ptr_low = 0; cur_ptr_high = 0; rc1 = SetFilePointer( h, cur_ptr_low, &cur_ptr_high, FILE_END ); if( rc1 == INVALID_SET_FILE_POINTER ) { // this might be OK so if( GetLastError() != NO_ERROR ) { _ReleaseFileH( handle ); return( __set_errno_nt() ); } } } #elif defined(__OS2__) { unsigned long dummy; rc1 = DosChgFilePtr( handle, 0L, SEEK_END, &dummy ); // should we explicitly ignore ERROR_SEEK_ON_DEVICE here? } #else rc1 = TinySeek( handle, 0L, SEEK_END ); #endif #if !defined(__NT__) if( TINY_ERROR( rc1 ) ) { _ReleaseFileH( handle ); return( __set_errno_dos( TINY_INFO( rc1 ) ) ); } #endif } len_written = 0; rc2 = 0; // Pad the file with zeros if necessary if( iomode_flags & _FILEEXT ) { // turn off file extended flag __SetIOMode_nogrow( handle, iomode_flags&(~_FILEEXT) ); // It is not required to pad a file with zeroes on an NTFS file system; // unfortunately it is required on FAT (and probably FAT32). (JBS) rc2 = zero_pad( handle ); } if( rc2 == 0 ) { if( iomode_flags & _BINARY ) { /* if binary mode */ rc2 = os_write( handle, buffer, len, &len_written ); /* end of binary mode part */ } else { /* text mode */ i = stackavail(); if( i < 0x00b0 ) { __STKOVERFLOW(); /* not enough stack space */ } buf_size = 512; if( i < (512 + 48) ) { buf_size = 128; } #if defined(__AXP__) || defined(__PPC__) buf = alloca( buf_size ); #else buf = __alloca( buf_size ); #endif j = 0; for( i = 0; i < len; ) { if( ((const char*)buffer)[i] == '\n' ) { buf[j] = '\r'; ++j; if( j == buf_size ) { rc2 = os_write( handle, buf, buf_size, &j ); if( rc2 == -1 ) break; len_written += j; if( rc2 == ENOSPC ) break; len_written = i; j = 0; } } buf[j] = ((const char*)buffer)[i]; ++i; ++j; if( j == buf_size ) { rc2 = os_write( handle, buf, buf_size, &j ); if( rc2 == -1 ) break; len_written += j; if( rc2 == ENOSPC ) break; len_written = i; j = 0; } } if( j ) { rc2 = os_write( handle, buf, j, &i ); if( rc2 == ENOSPC ) { len_written += i; } else { len_written = len; } } /* end of text mode part */ } } _ReleaseFileH( handle ); if( rc2 == -1 ) { return( rc2 ); } else { return( len_written ); } }
int __F_NAME(_dospawn,_wdospawn)( int mode, CHAR_TYPE *pgmname, CHAR_TYPE *cmdline, CHAR_TYPE *envp, const CHAR_TYPE * const argv[] ) { STARTUPINFO sinfo; PROCESS_INFORMATION pinfo; DWORD rc; BOOL osrc; __F_NAME(__ccmdline,__wccmdline)( pgmname, argv, cmdline, 0 ); memset( &sinfo, 0, sizeof( sinfo ) ); sinfo.wShowWindow = SW_NORMAL; // When passing in Unicode environments, the OS may not know which code // page to use when translating to MBCS in spawned program's startup // code. Result: Possible corruption of Unicode environment variables. #ifdef __WIDECHAR__ #if defined(__AXP__) || defined(__PPC__) osrc = CreateProcessW( NULL, cmdline, NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT, envp, NULL, &sinfo, &pinfo ); #else osrc = __lib_CreateProcessW( cmdline, TRUE, envp, &pinfo ); #endif #else osrc = CreateProcessA( NULL, cmdline, NULL, NULL, TRUE, 0, envp, NULL, &sinfo, &pinfo ); #endif if( osrc == FALSE ) { DWORD err; err = GetLastError(); if( (err == ERROR_ACCESS_DENIED) || (err == ERROR_BAD_EXE_FORMAT) || (err == ERROR_BAD_PATHNAME) ) { err = ERROR_FILE_NOT_FOUND; } return( __set_errno_dos( err ) ); } if( mode == P_WAIT ) { if( WIN32_IS_WIN32S ) { // this is WIN32s Sleep( 1000 ); rc = STILL_ACTIVE; while( rc == STILL_ACTIVE ) { Sleep( 100 ); if( !GetExitCodeProcess( pinfo.hProcess, &rc ) ) { return( __set_errno_nt() ); } } } else { // this is WIN32 or Windows95 if( WaitForSingleObject( pinfo.hProcess, INFINITE ) == 0 ) { GetExitCodeProcess( pinfo.hProcess, &rc ); } else { rc = __set_errno_nt(); } } CloseHandle( pinfo.hProcess ); } else { /* no difference between P_NOWAIT and P_NOWAITO */ rc = (int)pinfo.hProcess; /* cwait will close (free) the handle */ } CloseHandle( pinfo.hThread ); return( rc ); }