68b425f5459fb48221a6adedbac4c1be7a41cc2d
[oweals/cde.git] / cde / lib / tt / mini_isam / isindexinfo.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: isindexinfo.c /main/3 1995/10/23 11:40:56 rswiston $                                                         */
28 #ifndef lint
29 static char sccsid[] = "@(#)isindexinfo.c 1.2 91/05/16 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isindexinfo.c
37  *
38  * Description:
39  *      Access file status information
40  */
41
42
43 #include "isam_impl.h"
44 #include <sys/time.h>
45
46 #define ZERO 0
47
48 /*
49  * err = isindexinfo(isfd, buffer, number)
50  *
51  * Return information about index.
52  * Return information about the data file (if number == 0).
53  *
54  * (this function is overloaded).
55  *
56  * Errors:
57  *      EBADARG number is out of range
58  *      ENOTOPEN isfd is not ISAM file descriptor of open ISAM file
59  */
60
61 #define dibuf ((struct dictinfo *)buffer)
62
63 int
64 isindexinfo(int isfd, struct keydesc *buffer, int number)
65 {
66     register Fab        *fab;
67     int                 ret;
68     Fcb                 *fcb = NULL;
69
70     /*
71      * Get File Access Block.
72      */
73     if ((fab = _isfd_find(isfd)) == NULL) {
74         _setiserrno2(ENOTOPEN, '9', '0');
75         return (ISERROR);
76     }
77
78     /*
79      * Call the Access Method or RPC client function, depending whether
80      * the file is local or remote.
81      */
82
83     _isam_entryhook();
84
85     /*
86      * Get FCB corresponding to the isfhandle handle.
87      */
88     if ((fcb = _openfcb(&fab->isfhandle, &fab->errcode)) == NULL) {
89         _isam_exithook();
90         return (ISERROR);
91     }
92
93     /*
94      * Update information in FCB from CNTL page on the disk
95      */
96     (void)_isfcb_cntlpg_r(fcb);
97
98     /*
99      * Validate number argument.
100      */
101     if (number < 0 || number > fcb->nkeys) {
102         _amseterrcode(&fab->errcode, EBADARG);
103         goto ERROR;
104     }
105
106     if (number == 0) {
107         
108         /*
109          * Return dictinfo structure.
110          */
111         dibuf->di_nkeys = fcb->nkeys;
112         dibuf->di_recsize = fcb->maxreclen;
113         dibuf->di_idxsize = fcb->keys[0].k2_len;
114         dibuf->di_nrecords = fcb->nrecords;
115
116         /* Set msb of di_nkeys for variable length records. */
117         if (fcb->varflag == TRUE) 
118             dibuf->di_nkeys |= DICTVARLENBIT;
119     }
120     else {
121
122         /*
123          * Return index information.
124          */
125         _iskey_itox(fcb->keys + number - 1, buffer);
126     }
127
128     _amseterrcode(&fab->errcode, ISOK);
129
130 /* XXX This fixes a core dump that occurs when isindexinfo is
131 *      called on brand new tables
132     _isdisk_commit();
133     _isdisk_sync();
134     _isdisk_inval();
135 */
136
137     _isam_exithook();
138     ret = ISOK;
139     goto CLEANUP;
140
141  ERROR:
142     /*
143      * Restore FCB from CNTL page.
144      */
145
146     _isdisk_rollback();
147     _isdisk_inval();
148
149     _isam_exithook();
150     ret = ISERROR;
151
152  CLEANUP: 
153
154     if (ret == ISOK)
155         isreclen = fab->minreclen;           /* for variable length */
156
157     _seterr_errcode(&fab->errcode);
158
159     /*
160      * This is a patch to conform with the VSX 3.0 test that checks
161      * that k_leng == 2 and k_type == 1 for index 1 if the ISAM file
162      * has no primary key. I suspect that these numbers are returned by
163      * C-ISAM and the author of VSX tests diligently checks them even 
164      * though they have no meaning.
165      */
166     if (ret == ISOK && number == 1 && buffer->k_nparts == 0) {
167         buffer->k_leng = 2;
168         buffer->k_type = INTTYPE;
169     }
170     
171     return (ret);               
172 }
173