define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / compiler / clib / fcntl.c
blobda8b0b3627d63cd07b926dc8e4ca217c17d8ce9b
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <stdarg.h>
12 #include "__errno.h"
13 #include "__open.h"
15 /*****************************************************************************
17 NAME */
18 int fcntl(
20 /* SYNOPSIS */
21 int fd,
22 int cmd,
23 ...)
25 /* FUNCTION
26 Perform operation specified in cmd on the file descriptor fd.
27 Some operations require additional arguments, in this case they
28 follow the cmd argument. The following operations are available:
30 F_DUPFD (int) - Duplicate file descriptor fd as the lowest numbered
31 file descriptor greater or equal to the operation
32 argument.
34 F_GETFD (void) - Read the file descriptor flags
36 F_SETFD (int) - Set the file descriptor flags to value given in
37 the operation argument
39 F_GETFL (void) - Read the file status flags
41 F_SETFL (int) - Set the file status flags to value given in the
42 operation argument.
44 File descriptor flags are zero or more ORed constants:
46 FD_CLOEXEC - File descriptor will be closed during execve()
48 File descriptor flags are not copied during duplication of file
49 descriptors.
51 File status flags are the flags given as mode parameter to open()
52 function call. You can change only a few file status flags in opened
53 file descriptor: O_NONBLOCK, O_APPEND and O_ASYNC. Any other file
54 status flags passed in F_SETFL argument will be ignored.
56 All duplicated file descriptors share the same set of file status
57 flags.
59 INPUTS
60 fd - File descriptor to perform operation on.
61 cmd - Operation specifier.
62 ... - Operation arguments.
64 RESULT
65 The return value of the function depends on the performed operation:
67 F_DUPFD - New duplicated file descriptor
69 F_GETFD - File descriptor flags
71 F_SETFD - 0
73 F_GETFL - File status flags
75 F_SETFL - 0 on success, -1 on error. In case of error a global errno
76 variable is set.
78 NOTES
80 EXAMPLE
82 BUGS
84 SEE ALSO
85 open()
87 INTERNALS
89 ******************************************************************************/
91 fdesc *desc = __getfdesc(fd);
93 if (!desc)
95 errno = EBADF;
96 return -1;
99 switch (cmd)
101 case F_DUPFD:
103 va_list ap;
104 int arg;
106 va_start(ap, cmd);
107 arg = va_arg(ap, int);
108 va_end(ap);
111 FIXME: FD_CLOEXEC must be off on the copy, once this flag
112 is supported (related to F_GETFD and F_SETFD).
115 return dup2(fd, __getfirstfd(arg));
117 case F_GETFD:
118 return desc->fdflags;
120 case F_SETFD:
122 va_list ap;
123 int arg;
125 va_start(ap, cmd);
126 arg = va_arg(ap, int);
127 va_end(ap);
129 desc->fdflags = arg;
130 return 0;
133 case F_GETFL:
134 return desc->fcb->flags & (O_NONBLOCK|O_APPEND|O_ASYNC);
136 case F_SETFL:
138 va_list ap;
139 int arg;
140 int oldmode = __oflags2amode(desc->fcb->flags & ~(O_NONBLOCK|O_APPEND|O_ASYNC));
142 va_start(ap, cmd);
143 arg = va_arg(ap, int);
144 va_end(ap);
146 arg &= (O_NONBLOCK|O_APPEND|O_ASYNC);
148 if (ChangeMode(CHANGE_FH, desc->fcb->fh, oldmode | __oflags2amode(arg)) == DOSTRUE)
150 desc->fcb->flags &= ~(O_NONBLOCK|O_APPEND|O_ASYNC);
151 desc->fcb->flags |= arg;
152 return 0;
155 errno = IoErr2errno(IoErr());
156 return -1;
159 default:
160 errno = EINVAL;
161 return -1;