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