implemented read and write system calls

svn path=/trunk/; revision=2755
This commit is contained in:
KJK::Hyperion 2002-03-21 22:46:30 +00:00
parent 9e44e1b4a3
commit 75deecc48d
2 changed files with 135 additions and 20 deletions

View file

@ -1,4 +1,4 @@
/* $Id:
/* $Id: read.c,v 1.2 2002/03/21 22:46:30 hyperion Exp $
*/
/*
* COPYRIGHT: See COPYING in the top level directory
@ -11,14 +11,13 @@
*/
#include <ddk/ntddk.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <psx/debug.h>
#include <psx/errno.h>
ssize_t read(int fildes, void *buf, size_t nbyte)
{
}
ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
ssize_t __read(int fildes, void *buf, size_t nbyte, off_t *offset)
{
HANDLE hFile;
NTSTATUS nErrCode;
@ -28,22 +27,91 @@ ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
if(fcntl(fildes, F_GETFH, &hFile) == -1)
return (-1);
/* read data from file */
nErrCode = NtReadFile
(
hFile,
NULL,
NULL,
NULL,
&isbStatus,
buf,
nbyte,
(PLARGE_INTEGER)&off_t,
NULL
);
if(offset != NULL)
{
/* NT always moves the file pointer, while Unix pread() must not: we have to
duplicate the handle and work on the duplicate */ /* FIXME? better save
the file position and restore it later? */
HANDLE hDupFile;
/* duplicate the handle */
nErrCode = NtDuplicateObject
(
NtCurrentProcess(),
hFile,
NtCurrentProcess(),
&hDupFile,
0,
0,
DUPLICATE_SAME_ACCESS
);
/* failure */
if(!NT_SUCCESS(nErrCode))
{
errno = __status_to_errno(nErrCode);
return (0);
}
/* read data from file at the specified offset */
nErrCode = NtReadFile
(
hDupFile,
NULL,
NULL,
NULL,
&isbStatus,
buf,
nbyte,
(PLARGE_INTEGER)offset,
NULL
);
NtClose(hDupFile);
/* failure */
if(!NT_SUCCESS(nErrCode))
{
errno = __status_to_errno(nErrCode);
return (0);
}
}
else
{
/* read data from file at the current offset */
nErrCode = NtReadFile
(
hFile,
NULL,
NULL,
NULL,
&isbStatus,
buf,
nbyte,
NULL,
NULL
);
/* failure */
if(!NT_SUCCESS(nErrCode))
{
errno = __status_to_errno(nErrCode);
return (0);
}
}
return ((ssize_t)isbStatus.Information);
}
ssize_t read(int fildes, void *buf, size_t nbyte)
{
return (__read(fildes, buf, nbyte, NULL));
}
ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
{
return (__read(fildes, buf, nbyte, &offset));
}
/* EOF */

View file

@ -1,4 +1,4 @@
/* $Id: write.c,v 1.2 2002/02/20 09:17:58 hyperion Exp $
/* $Id: write.c,v 1.3 2002/03/21 22:46:30 hyperion Exp $
*/
/*
* COPYRIGHT: See COPYING in the top level directory
@ -8,8 +8,55 @@
* PROGRAMMER: KJK::Hyperion <noog@libero.it>
* UPDATE HISTORY:
* 15/02/2002: Created
* 21/03/2002: Implemented write() (KJK::Hyperion <noog@libero.it>)
*/
#include <ddk/ntddk.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <psx/debug.h>
#include <psx/errno.h>
#include <psx/fdtable.h>
ssize_t write(int fildes, const void *buf, size_t nbyte)
{
__fildes_t fdDescriptor;
NTSTATUS nErrCode;
IO_STATUS_BLOCK isbStatus;
if(nbyte == 0)
return (0);
if(fcntl(fildes, F_GETALL, &fdDescriptor) == -1)
return (0);
if((fdDescriptor.OpenFlags && O_APPEND) == O_APPEND)
{
TODO("move file pointer to the end");
}
nErrCode = NtWriteFile
(
fdDescriptor.FileHandle,
NULL,
NULL,
NULL,
&isbStatus,
(PVOID)buf,
nbyte,
NULL,
NULL
);
if(!NT_SUCCESS(nErrCode))
{
errno = __status_to_errno(nErrCode);
return (0);
}
return (isbStatus.Information);
}
/* EOF */