Merge branch 'master' into cde-next
[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 /*
29  * Copyright (c) 1988 by Sun Microsystems, Inc.
30  */
31
32 /*
33  * isopen.c
34  *
35  * Description:
36  *      Open an ISAM file. 
37  */
38
39 #include "isam_impl.h"
40 #include <netdb.h>     
41 #include <sys/file.h>
42 #include <sys/time.h>
43
44 static int _am_open();
45 /*
46  * isfd = isopen(isfname, mode)
47  *
48  * Isopen() determines on which machine the ISAM file resides, 
49  * checks if the file exists, creates a File access block,
50  * and initilizes it. It also checks permissions. Returns an ISAM file
51  * descriptor (isfd), or a value of -1 if the open failed.
52  *
53  * Errors:
54  *      EBADARG Improper mode parameter
55  *      EBADFILE ISAM file is corrupted or it is not an NetISAM file
56  *      EFLOCKED The file is exclusively locked by other process.
57  *      EFNAME  Invalid ISAM file name 
58  *      EFNAME  ISAM file does not exist
59  *      ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
60  *
61  * The following error code is "borrowed" from UNIX:
62  *      EACCES  UNIX file system protection denies access to the file:
63  *               - mode is INOUT or OUTPUT and ISAM file is on 
64  *                 a Read-Only mounted file system
65  *               - UNIX file permissions don't allow access to the file
66  */
67
68 int 
69 isopen(char *isfname, int mode)
70 {
71     Fab                 *fab;
72     Isfd                isfd;
73     enum openmode       openmode;
74
75     /*
76      * Check if the user is allowed to access the ISAM file.
77      * Use UNIX and NFS permissions.
78      */
79
80     /* Get file open mode part of the mode parameter. */
81     if ((openmode = _getopenmode(mode)) == OM_BADMODE) {
82         _setiserrno2(EBADARG, '9', '0');
83         return (NOISFD);
84     }
85     /* Create a Fab object. */
86     fab = _fab_new(isfname,
87                    openmode,
88                    (Bool)((mode & ISLENMODE) == ISVARLEN),
89                    0,
90                    0);
91     if (fab == NULL) {
92         return (NOISFD);                     /* iserrno is set by fab_new */
93     }
94
95     /* Get an ISAM file descriptor for this fab */
96     if ((isfd = _isfd_insert(fab)) == NOISFD) {
97         /* Table of ISAM file descriptors would overflow. */
98         _fab_destroy(fab);
99         _setiserrno2(ETOOMANY, '9', '0');
100         return (NOISFD);
101     }
102     FAB_ISFDSET(fab, isfd);
103     if (_am_open(fab)) {
104         _seterr_errcode(&fab->errcode);
105         _fab_destroy(fab);
106         return (NOISFD);
107     }
108
109     isreclen = fab->maxreclen;
110
111     return ((int)isfd);                      /* Successful isopen() */
112 }
113
114 Static int _am_open(Fab *fab)
115 {
116     return (_amopen(fab->isfname, fab->openmode, &fab->varlength,
117                     &fab->minreclen, &fab->maxreclen, &fab->isfhandle,
118                     &fab->curpos, &fab->errcode));
119 }