* Removed sys/_types.h include, all types are now defined in include files named...
[AROS.git] / compiler / clib / fcntl.c
blobc7c3609d527423b7d1f653029ea28c25d7d25a5f
1 /*
2 Copyright © 1995-2011, 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 "__errno.h"
13 #include "__fdesc.h"
15 /*****************************************************************************
17 NAME */
18 #include <fcntl.h>
20 int fcntl(
22 /* SYNOPSIS */
23 int fd,
24 int cmd,
25 ...)
27 /* FUNCTION
28 Perform operation specified in cmd on the file descriptor fd.
29 Some operations require additional arguments, in this case they
30 follow the cmd argument. The following operations are available:
32 F_DUPFD (int) - Duplicate file descriptor fd as the lowest numbered
33 file descriptor greater or equal to the operation
34 argument.
36 F_GETFD (void) - Read the file descriptor flags
38 F_SETFD (int) - Set the file descriptor flags to value given in
39 the operation argument
41 F_GETFL (void) - Read the file status flags
43 F_SETFL (int) - Set the file status flags to value given in the
44 operation argument.
46 File descriptor flags are zero or more ORed constants:
48 FD_CLOEXEC - File descriptor will be closed during execve()
50 File descriptor flags are not copied during duplication of file
51 descriptors.
53 File status flags are the flags given as mode parameter to open()
54 function call. You can change only a few file status flags in opened
55 file descriptor: O_NONBLOCK, O_APPEND and O_ASYNC. Any other file
56 status flags passed in F_SETFL argument will be ignored.
58 All duplicated file descriptors share the same set of file status
59 flags.
61 INPUTS
62 fd - File descriptor to perform operation on.
63 cmd - Operation specifier.
64 ... - Operation arguments.
66 RESULT
67 The return value of the function depends on the performed operation:
69 F_DUPFD - New duplicated file descriptor
71 F_GETFD - File descriptor flags
73 F_SETFD - 0
75 F_GETFL - File status flags
77 F_SETFL - 0 on success, -1 on error. In case of error a global errno
78 variable is set.
80 NOTES
82 EXAMPLE
84 BUGS
86 SEE ALSO
87 open()
89 INTERNALS
91 ******************************************************************************/
93 fdesc *desc = __getfdesc(fd);
95 if (!desc)
97 errno = EBADF;
98 return -1;
101 switch (cmd)
103 case F_DUPFD:
105 va_list ap;
106 int arg;
108 va_start(ap, cmd);
109 arg = va_arg(ap, int);
110 va_end(ap);
113 FIXME: FD_CLOEXEC must be off on the copy, once this flag
114 is supported (related to F_GETFD and F_SETFD).
117 return dup2(fd, __getfirstfd(arg));
119 case F_GETFD:
120 return desc->fdflags;
122 case F_SETFD:
124 va_list ap;
125 int arg;
127 va_start(ap, cmd);
128 arg = va_arg(ap, int);
129 va_end(ap);
131 desc->fdflags = arg;
132 return 0;
135 case F_GETFL:
136 return desc->fcb->flags & (O_NONBLOCK|O_APPEND|O_ASYNC);
138 case F_SETFL:
140 va_list ap;
141 int arg;
142 int oldmode = __oflags2amode(desc->fcb->flags & ~(O_NONBLOCK|O_APPEND|O_ASYNC));
144 va_start(ap, cmd);
145 arg = va_arg(ap, int);
146 va_end(ap);
148 arg &= (O_NONBLOCK|O_APPEND|O_ASYNC);
150 if (ChangeMode(CHANGE_FH, desc->fcb->fh, oldmode | __oflags2amode(arg)) == DOSTRUE)
152 desc->fcb->flags &= ~(O_NONBLOCK|O_APPEND|O_ASYNC);
153 desc->fcb->flags |= arg;
154 return 0;
157 errno = IoErr2errno(IoErr());
158 return -1;
161 default:
162 errno = EINVAL;
163 return -1;