Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / fcntl.c
1 /*
2  * Acess2 POSIX Emulation Library
3  * - By John Hodge (thePowersGang)
4  *
5  * fcntl.c
6  * - File descriptor control
7  */
8 #include <sys/types.h>  // off_t
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12 #include <acess/sys.h>
13 #include <errno.h>
14
15 // === CODE ===
16 int fcntl(int fd, int cmd, ...)
17 {
18          int    ret;
19         va_list args;
20         va_start(args, cmd);
21         
22         switch(cmd)
23         {
24         case F_GETFL: {
25                  int    a_flags = _SysFDFlags(fd, 0, 0);
26                 if( a_flags == -1 )
27                         return -1;
28                 ret = 0;
29                 if(a_flags & OPENFLAG_READ)     ret |= O_RDONLY;
30                 if(a_flags & OPENFLAG_WRITE)    ret |= O_WRONLY;
31                 if(a_flags & OPENFLAG_NONBLOCK) ret |= O_NONBLOCK;
32                 if(a_flags & OPENFLAG_APPEND)   ret |= O_APPEND;
33                 // TODO: Extra flags for F_GETFL
34                 break; }
35         case F_SETFL: {
36                 long    p_flags = va_arg(args, long);
37                  int    a_flags = 0;
38                 const int       mask = OPENFLAG_NONBLOCK|OPENFLAG_APPEND;
39                 
40                 if(p_flags & O_NONBLOCK)
41                         a_flags |= OPENFLAG_NONBLOCK;
42                 if(p_flags & O_APPEND)
43                         a_flags |= OPENFLAG_APPEND;
44                 // TODO: Extra flags for F_SETFL
45
46                 ret = _SysFDFlags(fd, mask, a_flags);
47                 _SysDebug("fcntl(%i, F_SETFL, 0x%lx) = Acess 0x%x", fd, p_flags, ret);
48                 if(ret != -1)
49                         ret = 0;
50
51                 break; }
52         default:
53                 _SysDebug("fcntl(%i) unknown or unimplimented", cmd);
54                 errno = EINVAL;
55                 ret = -1;
56                 break;
57         }
58         va_end(args);
59         return ret;
60 }

UCC git Repository :: git.ucc.asn.au