]> pd.if.org Git - pdclib/blob - platform/posix/functions/stdio/_PDCLIB_fileops.c
20138b722d4c5effd64393664cd866d229930969
[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 #include <stdio.h>
9 #include <stdint.h>
10 #include <_PDCLIB_glue.h>
11 #include <errno.h>
12 #include <unistd.h>
13
14 static bool readf( _PDCLIB_fd_t fd, void * buf, size_t length, 
15                    size_t * numBytesRead )
16 {
17     ssize_t res = read(fd.sval, buf, length);
18     if(res == -1) {
19         return false;
20     } else {
21         *numBytesRead = res;
22         return true;
23     }
24 }
25
26 static bool writef( _PDCLIB_fd_t fd, const void * buf, size_t length, 
27                    size_t * numBytesWritten )
28 {
29     ssize_t res = write(fd.sval, buf, length);
30     if(res == -1) {
31         return false;
32     } else {
33         *numBytesWritten = res;
34         return true;
35     }
36 }
37
38 /* Note: Assumes being compiled with an OFF64 programming model */
39
40 static bool seekf( _PDCLIB_fd_t fd, int_fast64_t offset, int whence,
41     int_fast64_t* newPos )
42 {
43     off_t npos = lseek( fd.sval, offset, whence );
44     if( npos == -1 ) {
45         return false;
46     } else {
47         *newPos = npos;
48         return true;
49     }
50 }
51
52 static void closef( _PDCLIB_fd_t self )
53 {
54     close( self.sval );
55 }
56
57 const _PDCLIB_fileops_t _PDCLIB_fileops = {
58     .read  = readf,
59     .write = writef,
60     .seek  = seekf,
61     .close = closef,
62 };
63
64 #endif
65
66 #ifdef TEST
67 #include <_PDCLIB_test.h>
68
69 int main( void )
70 {
71     // Tested by stdio test cases
72     return TEST_RESULTS;
73 }
74
75 #endif