xref: /onnv-gate/usr/src/lib/libbc/libc/sys/sys5/fcntl.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1990-1996 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/syscall.h>
30*0Sstevel@tonic-gate #include <sys/fcntl.h>
31*0Sstevel@tonic-gate #include <sys/errno.h>
32*0Sstevel@tonic-gate #include <sys/filio.h>
33*0Sstevel@tonic-gate #include <sys/ioccom.h>
34*0Sstevel@tonic-gate #include <unistd.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /* The following is an array of fcntl commands. The numbers listed
37*0Sstevel@tonic-gate  * below are from SVR4. Array is indexed with SunOS 4.1 numbers to
38*0Sstevel@tonic-gate  * obtain the SVR4 numbers.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate int cmd_op[14] = {0, 1, 2, 3, 4, 23, 24, 14, 6, 7, 21, 20, -1, 22};
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /* SVR4/SunOS 5.0 equivalent modes */
43*0Sstevel@tonic-gate #define N_O_NDELAY	0x04
44*0Sstevel@tonic-gate #define N_O_SYNC	0x10
45*0Sstevel@tonic-gate #define N_O_NONBLOCK	0x80
46*0Sstevel@tonic-gate #define N_O_CREAT	0x100
47*0Sstevel@tonic-gate #define N_O_TRUNC	0x200
48*0Sstevel@tonic-gate #define N_O_EXCL	0x400
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	S5_FASYNC 0x1000
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /* from SVR4 stropts.h */
53*0Sstevel@tonic-gate #define	S5_S_RDNORM		0x0040
54*0Sstevel@tonic-gate #define	S5_S_WRNORM		0x0004
55*0Sstevel@tonic-gate #define	S5_S_RDBAND		0x0080
56*0Sstevel@tonic-gate #define	S5_S_BANDURG		0x0200
57*0Sstevel@tonic-gate #define	S5_I_SETSIG		(('S'<<8)|011)
58*0Sstevel@tonic-gate #define	S5_I_GETSIG		(('S'<<8)|012)
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /* Mask corresponding to the bits above in SunOS 4.x */
61*0Sstevel@tonic-gate #define FLAGS_MASK	(O_SYNC|O_NONBLOCK|O_CREAT|O_TRUNC|O_EXCL \
62*0Sstevel@tonic-gate 			|O_NDELAY|FASYNC)
63*0Sstevel@tonic-gate #define N_FLAGS_MASK	(N_O_NDELAY|N_O_SYNC|N_O_NONBLOCK|N_O_CREAT \
64*0Sstevel@tonic-gate 			|N_O_TRUNC|N_O_EXCL|S5_FASYNC)
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate struct n_flock {
67*0Sstevel@tonic-gate         short	l_type;
68*0Sstevel@tonic-gate         short	l_whence;
69*0Sstevel@tonic-gate         long	l_start;
70*0Sstevel@tonic-gate         long	l_len;          /* len == 0 means until end of file */
71*0Sstevel@tonic-gate         long	l_sysid;
72*0Sstevel@tonic-gate         long	l_pid;
73*0Sstevel@tonic-gate         long	pad[4];         /* reserve area */
74*0Sstevel@tonic-gate } ;
75*0Sstevel@tonic-gate 
fcntl(fd,cmd,arg)76*0Sstevel@tonic-gate int fcntl(fd, cmd, arg)
77*0Sstevel@tonic-gate int fd, cmd, arg;
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	return(bc_fcntl(fd, cmd, arg));
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
bc_fcntl(fd,cmd,arg)82*0Sstevel@tonic-gate int bc_fcntl(fd, cmd, arg)
83*0Sstevel@tonic-gate int fd, cmd, arg;
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	int fds, ret;
86*0Sstevel@tonic-gate 	struct flock *savarg;
87*0Sstevel@tonic-gate 	struct n_flock nfl;
88*0Sstevel@tonic-gate 	extern int errno;
89*0Sstevel@tonic-gate 	int narg, i;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if ((cmd == F_SETOWN) || (cmd == F_GETOWN)) {
92*0Sstevel@tonic-gate 		ret = _s_fcntl(fd, cmd_op[cmd], arg);
93*0Sstevel@tonic-gate 		if ((ret != -1) || (errno != EINVAL))
94*0Sstevel@tonic-gate 			return (ret);
95*0Sstevel@tonic-gate 		else {
96*0Sstevel@tonic-gate 			if (cmd == F_GETOWN) {
97*0Sstevel@tonic-gate 				if (_ioctl(fd, S5_I_GETSIG, &i) < 0) {
98*0Sstevel@tonic-gate 					if (errno == EINVAL)
99*0Sstevel@tonic-gate 						i = 0;
100*0Sstevel@tonic-gate 					else
101*0Sstevel@tonic-gate 						return (-1);
102*0Sstevel@tonic-gate 				}
103*0Sstevel@tonic-gate 				if (i & (S5_S_RDBAND|S5_S_BANDURG|
104*0Sstevel@tonic-gate 				    S5_S_RDNORM|S5_S_WRNORM))
105*0Sstevel@tonic-gate 					return (getpid());
106*0Sstevel@tonic-gate 				return (0);
107*0Sstevel@tonic-gate 			} else { /* cmd == F_SETOWN */
108*0Sstevel@tonic-gate 				i = S5_S_RDNORM|S5_S_WRNORM|S5_S_RDBAND|S5_S_BANDURG;
109*0Sstevel@tonic-gate 				return (ioctl(fd, S5_I_SETSIG, i));
110*0Sstevel@tonic-gate 			}
111*0Sstevel@tonic-gate 		}
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 	if (cmd == F_SETFL) {
114*0Sstevel@tonic-gate 		if (arg & FLAGS_MASK) {
115*0Sstevel@tonic-gate 			narg = arg & ~FLAGS_MASK;
116*0Sstevel@tonic-gate 			if (arg & FASYNC)
117*0Sstevel@tonic-gate 				narg |= S5_FASYNC;
118*0Sstevel@tonic-gate 			if (arg & O_SYNC)
119*0Sstevel@tonic-gate 				narg |= N_O_SYNC;
120*0Sstevel@tonic-gate 			if (arg & O_CREAT)
121*0Sstevel@tonic-gate 				narg |= N_O_CREAT;
122*0Sstevel@tonic-gate 			if (arg & O_TRUNC)
123*0Sstevel@tonic-gate 				narg |= N_O_TRUNC;
124*0Sstevel@tonic-gate 			if (arg & O_EXCL)
125*0Sstevel@tonic-gate 				narg |= N_O_EXCL;
126*0Sstevel@tonic-gate 			if (arg & (O_NDELAY))
127*0Sstevel@tonic-gate 				narg |= N_O_NDELAY;
128*0Sstevel@tonic-gate 			if (arg & O_NONBLOCK)
129*0Sstevel@tonic-gate 				narg |= N_O_NONBLOCK;
130*0Sstevel@tonic-gate 			arg = narg;
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate         } else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK)  {
133*0Sstevel@tonic-gate 		if (arg == 0 || arg == -1) {
134*0Sstevel@tonic-gate 			errno = EFAULT;
135*0Sstevel@tonic-gate 			return(-1);
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 		savarg = (struct flock *)arg;
138*0Sstevel@tonic-gate 		arg = (int) &nfl;
139*0Sstevel@tonic-gate 		nfl.l_type = savarg->l_type;
140*0Sstevel@tonic-gate 		nfl.l_whence = savarg->l_whence;
141*0Sstevel@tonic-gate 		nfl.l_start = savarg->l_start;
142*0Sstevel@tonic-gate 		nfl.l_len = savarg->l_len;
143*0Sstevel@tonic-gate 		nfl.l_pid = savarg->l_pid;
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	ret = _s_fcntl(fd, cmd_op[cmd], arg);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (ret != -1) {
149*0Sstevel@tonic-gate 		if (cmd == F_DUPFD) {
150*0Sstevel@tonic-gate 			if ((fds = fd_get(fd)) != -1)
151*0Sstevel@tonic-gate 				fd_add(ret, fds);
152*0Sstevel@tonic-gate        		} else if (cmd == F_GETFL) {
153*0Sstevel@tonic-gate 			if (ret & N_FLAGS_MASK) {
154*0Sstevel@tonic-gate 				narg = ret & ~N_FLAGS_MASK;
155*0Sstevel@tonic-gate 				if (ret & S5_FASYNC)
156*0Sstevel@tonic-gate 					narg |= FASYNC;
157*0Sstevel@tonic-gate 				if (ret & N_O_SYNC)
158*0Sstevel@tonic-gate 					narg |= O_SYNC;
159*0Sstevel@tonic-gate 				if (ret & N_O_NONBLOCK)
160*0Sstevel@tonic-gate 					narg |= O_NONBLOCK;
161*0Sstevel@tonic-gate 				if (ret & N_O_CREAT)
162*0Sstevel@tonic-gate 					narg |= O_CREAT;
163*0Sstevel@tonic-gate 				if (ret & N_O_TRUNC)
164*0Sstevel@tonic-gate 					narg |= O_TRUNC;
165*0Sstevel@tonic-gate 				if (ret & N_O_EXCL)
166*0Sstevel@tonic-gate 					narg |= O_EXCL;
167*0Sstevel@tonic-gate 				if (ret & (N_O_NDELAY))
168*0Sstevel@tonic-gate 					narg |= O_NDELAY;
169*0Sstevel@tonic-gate 				ret = narg;
170*0Sstevel@tonic-gate 			}
171*0Sstevel@tonic-gate 		} else if (cmd == F_SETLK || cmd == F_SETLKW ||
172*0Sstevel@tonic-gate 			cmd == F_GETLK) {
173*0Sstevel@tonic-gate 			savarg->l_type = nfl.l_type;
174*0Sstevel@tonic-gate 			savarg->l_whence = nfl.l_whence;
175*0Sstevel@tonic-gate 			savarg->l_start = nfl.l_start;
176*0Sstevel@tonic-gate 			savarg->l_len = nfl.l_len;
177*0Sstevel@tonic-gate 			savarg->l_pid = nfl.l_pid;
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate         return(ret);
181*0Sstevel@tonic-gate }
182