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