Fixed some embarassing errors
[reactos.git] / posix / lib / psxdll / unistd / write.c
1 /* $Id: write.c,v 1.4 2002/05/17 01:54:39 hyperion Exp $
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS POSIX+ Subsystem
6 * FILE: subsys/psx/lib/psxdll/unistd/write.c
7 * PURPOSE: Write on a file
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 15/02/2002: Created
11 * 21/03/2002: Implemented write() (KJK::Hyperion <noog@libero.it>)
12 */
13
14 #include <ddk/ntddk.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <psx/debug.h>
19 #include <psx/errno.h>
20 #include <psx/fdtable.h>
21
22 ssize_t write(int fildes, const void *buf, size_t nbyte)
23 {
24 __fildes_t fdDescriptor;
25 NTSTATUS nErrCode;
26 IO_STATUS_BLOCK isbStatus;
27
28 if(nbyte == 0)
29 return (0);
30
31 if(fcntl(fildes, F_GETALL, &fdDescriptor) == -1)
32 {
33 ERR("fcntl() failed, errno %d", errno);
34 return (0);
35 }
36
37 if((fdDescriptor.OpenFlags && O_APPEND) == O_APPEND)
38 {
39 TODO("move file pointer to the end");
40 }
41
42 INFO("handle for descriptor %d is %d", fildes, fdDescriptor.FileHandle);
43
44 nErrCode = NtWriteFile
45 (
46 fdDescriptor.FileHandle,
47 NULL,
48 NULL,
49 NULL,
50 &isbStatus,
51 (PVOID)buf,
52 nbyte,
53 NULL,
54 NULL
55 );
56
57 if(!NT_SUCCESS(nErrCode))
58 {
59 ERR("NtWriteFile() failed with status 0x%08X", nErrCode);
60 errno = __status_to_errno(nErrCode);
61 return (0);
62 }
63
64 return (isbStatus.Information);
65 }
66
67 /* EOF */
68