summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/sys/4.2/fcntl.c
blob: 8e19913010d2cea4ce58eabaf340fbd2bfbda34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1990-1996 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#ident	"%Z%%M%	%I%	%E% SMI"

#include <unistd.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/filio.h>
#include <sys/ioccom.h>
#include <sys/syscall.h>


/* The following is an array of fcntl commands. The numbers listed
 * below are from SVR4. Array is indexed with SunOS 4.1 numbers to
 * obtain the SVR4 numbers.
 */
int cmd_op[14] = {0, 1, 2, 3, 4, 23, 24, 14, 6, 7, 21, 20, -1, 22};

/* SVR4/SunOS 5.0 equivalent modes */
#define N_O_NDELAY      0x04
#define N_O_SYNC	0x10
#define N_O_NONBLOCK    0x80
#define N_O_CREAT       0x100
#define N_O_TRUNC       0x200
#define N_O_EXCL	0x400

#define	S5_FASYNC 0x1000

/* from SVR4 stropts.h */
#define	S5_S_RDNORM		0x0040
#define	S5_S_WRNORM		0x0004
#define	S5_S_RDBAND		0x0080
#define	S5_S_BANDURG		0x0200
#define	S5_I_SETSIG		(('S'<<8)|011)
#define	S5_I_GETSIG		(('S'<<8)|012)

/* Mask corresponding to the bits above in SunOS 4.x */
#define FLAGS_MASK      (O_SYNC|O_NONBLOCK|O_CREAT|O_TRUNC|O_EXCL \
			|O_NDELAY|FNBIO|FASYNC)
#define N_FLAGS_MASK    (N_O_NDELAY|N_O_SYNC|N_O_NONBLOCK|N_O_CREAT \
			|N_O_TRUNC|N_O_EXCL|S5_FASYNC)

struct n_flock {
	short	l_type;
	short	l_whence;
	long	l_start;
	long	l_len;	  /* len == 0 means until end of file */
	long	l_sysid;
	long	l_pid;
	long	pad[4];	 /* reserve area */
} ;


int fcntl(fd, cmd, arg)
int fd, cmd, arg;
{
	return(bc_fcntl(fd, cmd, arg));
}


int bc_fcntl(fd, cmd, arg)
int fd, cmd, arg;
{
	int fds, ret;
	struct flock *savarg;
	struct n_flock nfl;
	extern int errno;
	int i, narg;

	if ((cmd == F_SETOWN) || (cmd == F_GETOWN)) {
		ret = _s_fcntl(fd, cmd_op[cmd], arg);
		if ((ret != -1) || (errno != EINVAL))
			return (ret);
		else {
			if (cmd == F_GETOWN) {
				if (_ioctl(fd, S5_I_GETSIG, &i) < 0) {
					if (errno == EINVAL)
						i = 0;
					else
						return (-1);
				}
				if (i & (S5_S_RDBAND|S5_S_BANDURG|
				    S5_S_RDNORM|S5_S_WRNORM))
					return (getpid());
				return (0);
			} else { /* cmd == F_SETOWN */
				i = S5_S_RDNORM|S5_S_WRNORM|S5_S_RDBAND|S5_S_BANDURG;
				return (ioctl(fd, S5_I_SETSIG, i));
			}
		}
	}
	if (cmd == F_SETFL) {
		if (arg & FLAGS_MASK) {
			narg = arg & ~FLAGS_MASK;
			if (arg & FASYNC)
				narg |= S5_FASYNC;
			if (arg & O_SYNC)
				narg |= N_O_SYNC;
			if (arg & O_CREAT)
				narg |= N_O_CREAT;
			if (arg & O_TRUNC)
				narg |= N_O_TRUNC;
			if (arg & O_EXCL)
				narg |= N_O_EXCL;
			if (arg & (O_NDELAY)) 
				narg |= N_O_NDELAY;
			if (arg & O_NONBLOCK) 
				narg |= N_O_NONBLOCK;
			if (arg & FNBIO)
				narg |= N_O_NDELAY;
			arg = narg;
		}
	} else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK)  {
		if (arg == 0 || arg == -1) {
			errno = EFAULT;
			return(-1);
		}
		savarg = (struct flock *)arg;
		arg = (int) &nfl;
		nfl.l_type = savarg->l_type;
		nfl.l_whence = savarg->l_whence;
		nfl.l_start = savarg->l_start;
		nfl.l_len = savarg->l_len;
		nfl.l_pid = savarg->l_pid;
	}			

	ret = _s_fcntl(fd, cmd_op[cmd], arg);

	if (ret != -1) {
		if (cmd == F_DUPFD) {
			if ((fds = fd_get(fd)) != -1) 
				fd_add(ret, fds);
		} else if (cmd == F_GETFL) {
			if (ret & N_FLAGS_MASK) {
				narg = ret & ~N_FLAGS_MASK;
				if (ret & S5_FASYNC)
					narg |= FASYNC;
				if (ret & N_O_SYNC)
					narg |= O_SYNC;
				if (ret & N_O_NONBLOCK)
					narg |= O_NONBLOCK;
				if (ret & N_O_CREAT)
					narg |= O_CREAT;
				if (ret & N_O_TRUNC)
					narg |= O_TRUNC;
				if (ret & N_O_EXCL)
					narg |= O_EXCL;
				if (ret & (N_O_NDELAY))
					narg |= O_NDELAY;
				ret = narg;
			}
		} else if (cmd == F_SETLK || cmd == F_SETLKW ||
			cmd == F_GETLK) {
			savarg->l_type = nfl.l_type;
			savarg->l_whence = nfl.l_whence;
			savarg->l_start = nfl.l_start;
			savarg->l_len = nfl.l_len;
			savarg->l_pid = nfl.l_pid;
			arg = (int) savarg;
		}
	}
	return(ret);
}