7b059951eec12bbacb33b17010044941079f9e38
[oweals/cde.git] / cde / lib / tt / mini_isam / isopen.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: isopen.c /main/3 1995/10/23 11:42:40 rswiston $                                                      */
28 #ifndef lint
29 static char sccsid[] = "@(#)isopen.c 1.12 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isopen.c
37  *
38  * Description:
39  *      Open an ISAM file. 
40  */
41
42 #include "isam_impl.h"
43 #include <netdb.h>     
44 #include <sys/file.h>
45 #include <sys/time.h>
46
47 extern char _isam_version[];
48 static char *_version_ = _isam_version;
49
50 static int _am_open();
51 /*
52  * isfd = isopen(isfname, mode)
53  *
54  * Isopen() determines on which machine the ISAM file resides, 
55  * checks if the file exists, creates a File access block,
56  * and initilizes it. It also checks permissions. Returns an ISAM file
57  * descriptor (isfd), or a value of -1 if the open failed.
58  *
59  * Errors:
60  *      EBADARG Improper mode parameter
61  *      EBADFILE ISAM file is corrupted or it is not an NetISAM file
62  *      EFLOCKED The file is exclusively locked by other process.
63  *      EFNAME  Invalid ISAM file name 
64  *      EFNAME  ISAM file does not exist
65  *      ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
66  *
67  * The following error code is "borrowed" from UNIX:
68  *      EACCES  UNIX file system protection denies access to the file:
69  *               - mode is INOUT or OUTPUT and ISAM file is on 
70  *                 a Read-Only mounted file system
71  *               - UNIX file permissions don't allow access to the file
72  */
73
74 int 
75 isopen(char *isfname, int mode)
76 {
77     Fab                 *fab;
78     Isfd                isfd;
79     enum openmode       openmode;
80
81     /*
82      * Check if the user is allowed to access the ISAM file.
83      * Use UNIX and NFS permissions.
84      */
85
86     /* Get file open mode part of the mode parameter. */
87     if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
88         _setiserrno2(EBADARG, '9', '0');
89         return (NOISFD);
90     }
91     /* Create a Fab object. */
92     fab = _fab_new(isfname,
93                    openmode,
94                    (Bool)((mode & ISLENMODE) == ISVARLEN),
95                    0,
96                    0);
97     if (fab == NULL) {
98         return (NOISFD);                     /* iserrno is set by fab_new */
99     }
100
101     /* Get an ISAM file descriptor for this fab */
102     if ((isfd = _isfd_insert(fab)) == NOISFD) {
103         /* Table of ISAM file descriptors would overflow. */
104         _fab_destroy(fab);
105         _setiserrno2(ETOOMANY, '9', '0');
106         return (NOISFD);
107     }
108     FAB_ISFDSET(fab, isfd);
109     if (_am_open(fab)) {
110         _seterr_errcode(&fab->errcode);
111         _fab_destroy(fab);
112         return (NOISFD);
113     }
114
115     isreclen = fab->maxreclen;
116
117     return ((int)isfd);                      /* Successful isopen() */
118 }
119
120 Static int _am_open(Fab *fab)
121 {
122     return (_amopen(fab->isfname, fab->openmode, &fab->varlength,
123                     &fab->minreclen, &fab->maxreclen, &fab->isfhandle,
124                     &fab->curpos, &fab->errcode));
125 }