muimaster.library: support Listview_List in List
[AROS.git] / compiler / posixc / fcntl.c
blob5d3f4f7d07033809956dd6065b98fe9fcf8c9911
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
8 #include <errno.h>
9 #include <stdarg.h>
10 #include <unistd.h>
12 #include "__fdesc.h"
14 /*****************************************************************************
16 NAME */
17 #include <fcntl.h>
19 int fcntl(
21 /* SYNOPSIS */
22 int fd,
23 int cmd,
24 ...)
26 /* FUNCTION
27 Perform operation specified in cmd on the file descriptor fd.
28 Some operations require additional arguments, in this case they
29 follow the cmd argument. The following operations are available:
31 F_DUPFD (int) - Duplicate file descriptor fd as the lowest numbered
32 file descriptor greater or equal to the operation
33 argument.
35 F_GETFD (void) - Read the file descriptor flags
37 F_SETFD (int) - Set the file descriptor flags to value given in
38 the operation argument
40 F_GETFL (void) - Read the file status flags
42 F_SETFL (int) - Set the file status flags to value given in the
43 operation argument.
45 File descriptor flags are zero or more ORed constants:
47 FD_CLOEXEC - File descriptor will be closed during execve()
49 File descriptor flags are not copied during duplication of file
50 descriptors.
52 File status flags are the flags given as mode parameter to open()
53 function call. You can change only a few file status flags in opened
54 file descriptor: O_NONBLOCK, O_APPEND and O_ASYNC. Any other file
55 status flags passed in F_SETFL argument will be ignored.
57 All duplicated file descriptors share the same set of file status
58 flags.
60 INPUTS
61 fd - File descriptor to perform operation on.
62 cmd - Operation specifier.
63 ... - Operation arguments.
65 RESULT
66 The return value of the function depends on the performed operation:
68 F_DUPFD - New duplicated file descriptor
70 F_GETFD - File descriptor flags
72 F_SETFD - 0
74 F_GETFL - File status flags
76 F_SETFL - 0 on success, -1 on error. In case of error a global errno
77 variable is set.
79 NOTES
81 EXAMPLE
83 BUGS
85 SEE ALSO
86 open()
88 INTERNALS
90 ******************************************************************************/
92 fdesc *desc = __getfdesc(fd);
94 if (!desc)
96 errno = EBADF;
97 return -1;
100 switch (cmd)
102 case F_DUPFD:
104 va_list ap;
105 int arg;
107 va_start(ap, cmd);
108 arg = va_arg(ap, int);
109 va_end(ap);
112 FIXME: FD_CLOEXEC must be off on the copy, once this flag
113 is supported (related to F_GETFD and F_SETFD).
116 return dup2(fd, __getfirstfd(arg));
118 case F_GETFD:
119 return desc->fdflags;
121 case F_SETFD:
123 va_list ap;
124 int arg;
126 va_start(ap, cmd);
127 arg = va_arg(ap, int);
128 va_end(ap);
130 desc->fdflags = arg;
131 return 0;
134 case F_GETFL:
135 return desc->fcb->flags & (O_NONBLOCK|O_APPEND|O_ASYNC);
137 case F_SETFL:
139 va_list ap;
140 int arg;
141 int oldmode = __oflags2amode(desc->fcb->flags & ~(O_NONBLOCK|O_APPEND|O_ASYNC));
143 va_start(ap, cmd);
144 arg = va_arg(ap, int);
145 va_end(ap);
147 arg &= (O_NONBLOCK|O_APPEND|O_ASYNC);
149 if (ChangeMode(CHANGE_FH, desc->fcb->handle, oldmode | __oflags2amode(arg)) == DOSTRUE)
151 desc->fcb->flags &= ~(O_NONBLOCK|O_APPEND|O_ASYNC);
152 desc->fcb->flags |= arg;
153 return 0;
156 errno = __stdc_ioerr2errno(IoErr());
157 return -1;
160 default:
161 errno = EINVAL;
162 return -1;