1 /* @(#)fileluopen.c 1.19 10/11/06 Copyright 1986, 1995-2010 J. Schilling */
2 /*
3 * Copyright (c) 1986, 1995-2010 J. Schilling
4 */
5 /*
6 * The contents of this file are subject to the terms of the
7 * Common Development and Distribution License, Version 1.0 only
8 * (the "License"). You may not use this file except in compliance
9 * with the License.
10 *
11 * See the file CDDL.Schily.txt in this distribution for details.
12 * A copy of the CDDL is also available via the Internet at
13 * http://www.opensource.org/licenses/cddl1.txt
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file CDDL.Schily.txt from this distribution.
17 */
18
19 #include "schilyio.h"
20
21 /*
22 * Note that because of a definition in schilyio.h we are using
23 * fseeko()/ftello() instead of fseek()/ftell() if available.
24 */
25
26 #ifndef O_NDELAY /* This is undefined on BeOS :-( */
27 #define O_NDELAY 0
28 #endif
29 #ifndef O_CREAT
30 #define O_CREAT 0
31 #endif
32 #ifndef O_TRUNC
33 #define O_TRUNC 0
34 #endif
35 #ifndef O_EXCL
36 #define O_EXCL 0
37 #endif
38
39 /*
40 * fileluopen - open a stream for lun
41 */
42 EXPORT FILE *
fileluopen(f,mode)43 fileluopen(f, mode)
44 int f;
45 const char *mode;
46 {
47 int omode = 0;
48 int flag = 0;
49
50 if (!_cvmod(mode, &omode, &flag))
51 return ((FILE *)NULL);
52
53 if (omode & (O_NDELAY|O_CREAT|O_TRUNC|O_EXCL)) {
54 raisecond(_badmode, 0L);
55 return ((FILE *)NULL);
56 }
57
58 #ifdef F_GETFD
59 if (fcntl(f, F_GETFD, 0) < 0) {
60 raisecond(_badfile, 0L);
61 return ((FILE *)NULL);
62 }
63 #endif
64
65 #ifdef O_APPEND
66 if (omode & O_APPEND)
67 lseek(f, (off_t)0, SEEK_END);
68 #endif
69
70 return (_fcons((FILE *)0, f, flag));
71 }
72