1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * -last fcntl
26  */
27 
28 #include <ast.h>
29 
30 #ifndef fcntl
31 
32 NoN(fcntl)
33 
34 #else
35 
36 #include <ls.h>
37 #include <ast_tty.h>
38 #include <error.h>
39 
40 #if F_SETFD >= _ast_F_LOCAL
41 #if _sys_filio
42 #include <sys/filio.h>
43 #endif
44 #endif
45 
46 #if _lib_fcntl
47 #undef	fcntl
48 extern int	fcntl(int, int, ...);
49 #endif
50 
51 int
52 _ast_fcntl(int fd, int op, ...)
53 {
54 	int		n;
55 	int		save_errno;
56 	struct stat	st;
57 	va_list		ap;
58 
59 	save_errno = errno;
60 	va_start(ap, op);
61 	if (op >= _ast_F_LOCAL) switch (op)
62 	{
63 #if F_DUPFD >= _ast_F_LOCAL
64 	case F_DUPFD:
65 		n = va_arg(ap, int);
66 		op = dup2(fd, n);
67 		break;
68 #endif
69 #if F_GETFL >= _ast_F_LOCAL
70 	case F_GETFL:
71 		op = fstat(fd, &st);
72 		break;
73 #endif
74 #if F_SETFD >= _ast_F_LOCAL && defined(FIOCLEX)
75 	case F_SETFD:
76 		n = va_arg(ap, int);
77 		op = ioctl(fd, n == FD_CLOEXEC ? FIOCLEX : FIONCLEX, 0);
78 		break;
79 #endif
80 	default:
81 		errno = EINVAL;
82 		op = -1;
83 		break;
84 	}
85 	else
86 #if _lib_fcntl
87 	op = fcntl(fd, op, va_arg(ap, int));
88 #else
89 	{
90 		errno = EINVAL;
91 		op = -1;
92 	}
93 #endif
94 	va_end(ap);
95 	return(op);
96 }
97 
98 #endif
99