configure: create some convenience AC_SUBST's for the global includes
[oweals/cde.git] / cde / lib / tt / mini_isam / isamwrite.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: isamwrite.c /main/3 1995/10/23 11:35:14 rswiston $                                                   */
28 /*
29  * Copyright (c) 1988 by Sun Microsystems, Inc.
30  */
31
32 /*
33  * isamwrite.c
34  *
35  * Description: _amwrite()
36  *      Write record to  ISAM file.
37  *      
38  *
39  */
40
41 #include "isam_impl.h"
42
43 static int _addkeys2 ();
44 extern long *ismaxlong;
45
46 /*
47  * _amwrite(isfhandle, record, reclen, curpos, 
48  *          recnum, errcode)
49  *
50  * _amwrite() writes a new record to ISAM file.
51  *
52  * Input params:
53  *      isfhandle       Handle of ISAM file
54  *      record          record
55  *      reclen          length of the record
56  *      curpos          current record position
57  *
58  * Output params:
59  *      curpos          new current position
60  *      recnum          record number
61  *      errcode         error status of the operation
62  *
63  */
64
65 int
66 _amwrite(Bytearray *isfhandle, char *record, int reclen,
67          Bytearray *curpos, Recno *recnum, struct errcode *errcode)
68 {
69     Fcb                 *fcb = NULL;
70     Recno               recnum2;
71     Crp                 *crp;
72     int                 err;
73     int                 (*rec_write)();
74
75     _isam_entryhook();
76
77     /*
78      * Get FCB corresponding to the isfhandle handle.
79      */
80     if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
81         _isam_exithook();
82         return (ISERROR);
83     }
84
85     rec_write = (fcb->varflag?_vlrec_write:_flrec_write);
86
87     /*
88      * Update information in FCB from CNTL page on the disk
89      */
90     (void)_isfcb_cntlpg_r2(fcb);
91
92     if (rec_write(fcb, record, &recnum2, reclen) == ISERROR) {
93         _isfatal_error("_amwrite() cannot write record");
94     }
95
96     /*
97      * Update all keys.
98      */
99     if ((err = _addkeys2(fcb, record, recnum2, curpos)) != ISOK) {
100         _amseterrcode(errcode, err);    
101         goto ERROR;
102     }
103     
104     fcb->nrecords++;
105     
106     *recnum = recnum2;
107     _amseterrcode(errcode, ISOK);
108
109     _issignals_mask();
110     _isdisk_commit();
111     _isdisk_sync();
112     _isdisk_inval();
113
114     /*
115      * Return new current record position.
116      */
117     crp = (Crp *) curpos->data;
118
119     crp->flag = CRP_ON;
120     crp->recno = recnum2;
121
122     /*
123      * Update CNTL Page from the FCB.
124      */
125     (void)_isfcb_cntlpg_w2(fcb);
126     _issignals_unmask();
127
128     _isam_exithook();
129     return (ISOK);
130
131  ERROR:
132     
133     _isdisk_rollback();
134     _isdisk_inval();
135
136     /*
137      * If error is not EDUPL position undefined.
138      */
139     if (errcode->iserrno != EDUPL) {
140         ((Crp *)curpos->data)->flag = CRP_UNDEF;
141     }
142
143     /*
144      * Restore FCB from CNTL page.
145      */
146     if (fcb) (void)_isfcb_cntlpg_r2(fcb);
147
148     _isam_exithook();
149     return (ISERROR);
150 }
151
152
153 /*
154  * _addkeys()
155  *
156  * Insert key entry to all indexes.
157  *
158  * Returns ISOK, or EDUPS.
159  */
160
161 int _addkeys (Fcb *fcb, char *record, Recno recnum)
162 {
163     int                         nkeys = fcb->nkeys;
164     int                i;
165     int                         err;
166
167     for (i = 0; i < nkeys; i++) {
168         if ((err = _add1key(fcb, fcb->keys + i, record, recnum, 
169                             (char*)NULL)) != ISOK)
170             return (err);
171     }
172
173     return (ISOK);
174 }      
175
176 Static int
177 _addkeys2 (Fcb *fcb, char *record, Recno recnum, Bytearray *curpos)
178 {
179     int                 nkeys = fcb->nkeys;
180     int        i;
181     int                 err;
182     Crp                 *crp;
183     int                 keyid;
184     Keydesc2            *keydesc2;
185
186     crp = (Crp *)curpos->data;
187     keyid = crp->keyid;
188
189     for (i = 0; i < nkeys; i++) {
190         keydesc2 = fcb->keys + i;
191         if ((err =_add1key(fcb, keydesc2, record, recnum,
192                               (keydesc2->k2_keyid == keyid) ?
193                               crp->key : (char *) NULL)) != ISOK)
194             return (err);
195     }
196
197     return (ISOK);
198 }