]> pd.if.org Git - pdclib/blob - platform/posix/functions/stdio/_PDCLIB_fileops.c
2b75c415f7247357c15c2aaab2d67d89972d1bc5
[pdclib] / platform / posix / functions / stdio / _PDCLIB_fileops.c
1 /* _PDCLIB_fileops
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #ifndef REGTEST
8 #define _FILE_OFFSET_BITS 64
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <_PDCLIB_glue.h>
12 #include <errno.h>
13 #include <unistd.h>
14 typedef int64_t off_t;
15
16 static bool readf( _PDCLIB_fd_t fd, void * buf, size_t length,
17                    size_t * numBytesRead )
18 {
19     ssize_t res = read(fd.sval, buf, length);
20     if(res == -1) {
21         return false;
22     } else {
23         *numBytesRead = res;
24         return true;
25     }
26 }
27
28 static bool writef( _PDCLIB_fd_t fd, const void * buf, size_t length,
29                    size_t * numBytesWritten )
30 {
31     ssize_t res = write(fd.sval, buf, length);
32     if(res == -1) {
33         return false;
34     } else {
35         *numBytesWritten = res;
36         return true;
37     }
38 }
39
40 /* Note: Assumes being compiled with an OFF64 programming model */
41
42 static bool seekf( _PDCLIB_fd_t fd, int_fast64_t offset, int whence,
43     int_fast64_t* newPos )
44 {
45     off_t npos = lseek( fd.sval, offset, whence );
46     if( npos == -1 ) {
47         return false;
48     } else {
49         *newPos = npos;
50         return true;
51     }
52 }
53
54 static void closef( _PDCLIB_fd_t self )
55 {
56     close( self.sval );
57 }
58
59 const _PDCLIB_fileops_t _PDCLIB_fileops = {
60     .read  = readf,
61     .write = writef,
62     .seek  = seekf,
63     .close = closef,
64 };
65
66 #endif
67
68 #ifdef TEST
69 #include <_PDCLIB_test.h>
70
71 int main( void )
72 {
73     // Tested by stdio test cases
74     return TEST_RESULTS;
75 }
76
77 #endif