configure: create some convenience AC_SUBST's for the global includes
[oweals/cde.git] / cde / lib / tt / mini_isam / isdelcurr.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: isdelcurr.c /main/3 1995/10/23 11:37:32 rswiston $                                                   */
28 /*
29  * Copyright (c) 1988 by Sun Microsystems, Inc.
30  */
31
32 /*
33  * isdelcurr.c
34  *
35  * Description:
36  *      Delete current record from ISAM file. 
37  */
38
39
40 #include "isam_impl.h"
41 #include <sys/time.h>
42
43 static int _amdelcurr();
44
45 /*
46  * err = isdelcurr(isfd, record)
47  *
48  * Isdelcurr() modifies the current record in ISAM file. 
49  * All indexes of the ISAM file are updated.
50  *
51  * Current record position is not changed.
52  * isrecnum is set to to the deleted record.
53  *
54  * Returns 0 if successful, or -1 of any error.
55  *
56  * Errors:
57  *      ELOCKED The record or file has been locked by another process.
58  *      ENOTOPEN isfd does not correspond to an open ISAM file, or the
59  *              ISAM file was not opened with ISINOUT mode.
60  *      ENOCURR Record with record number recnum does not exist, or it
61  *              was deleted by another process.
62  */
63
64 int 
65 isdelcurr(int isfd)
66 {
67     int                 _am_delcurr();
68     Fab *fab;
69     int                 ret;
70     int                 recnum;
71
72     /*
73      * Get File Access Block.
74      */
75     if ((fab = _isfd_find(isfd)) == NULL) {
76         _setiserrno2(ENOTOPEN, '9', '0');
77         return (ISERROR);
78     }
79
80     /*
81      * Check that the open mode was  ISINOUT.
82      */
83     if (fab->openmode != OM_INOUT) {
84         _setiserrno2(ENOTOPEN, '9', '0');
85         return (ISERROR);
86     }
87
88     if ((ret = _amdelcurr(&fab->isfhandle, &fab->curpos,
89                           &recnum, &fab->errcode)) == ISOK) {
90         isrecnum = recnum;                   /* Set isrecnum */
91     }
92
93     _seterr_errcode(&fab->errcode);
94
95     return (ret);                            /* Successful write */
96 }
97
98 /*
99  * _amdelcurr(isfhandle, curpos, recnum, errcode)
100  *
101  * _amdelcurr() deletes a record from ISAM file.
102  *
103  * Input params:
104  *      isfhandle       Handle of ISAM file
105  *      curpos          Curent record position
106  *
107  * Output params:
108  *      recnum          record number of the deleted record
109  *      errcode         error status of the operation
110  *
111  */
112
113 static int
114 _amdelcurr(Bytearray *isfhandle, Bytearray *curpos, Recno *recnum,
115            struct errcode *errcode)
116 {
117     Fcb                 *fcb = NULL;
118     Crp                 *crp;
119     char                recbuf[ISMAXRECLEN];
120     int                 reclen;
121     int                 (*rec_read)();
122     int                 (*rec_delete)();
123
124     _isam_entryhook();
125
126     /*
127      * Get FCB corresponding to the isfhandle handle.
128      */
129     if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
130         _isam_exithook();
131         return (ISERROR);
132     }
133
134     rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
135     rec_delete = (fcb->varflag?_vlrec_delete:_flrec_delete);
136
137     /*
138      * Get info from current record position structure.
139      */
140     crp = (Crp *) curpos->data;
141
142     if (crp->flag != CRP_ON) {
143         _amseterrcode(errcode, ENOCURR);
144         goto ERROR;
145     }
146
147     /*
148      * Update information in FCB from CNTL page on the disk
149      */
150     (void)_isfcb_cntlpg_r2(fcb);
151
152     /*
153      * We must read the record first to be able to delete keys.
154      */
155     if (rec_read(fcb, recbuf, crp->recno, &reclen) != ISOK) {
156         _amseterrcode(errcode, ENOCURR);
157         goto ERROR;
158     }
159
160     if (rec_delete(fcb, crp->recno) != ISOK) {
161         _amseterrcode(errcode, ENOCURR);
162         goto ERROR;
163     }
164
165     *recnum = crp->recno;
166
167     fcb->nrecords--;
168
169     /*
170      * Delete associated entries from all indexes.
171      */
172     _delkeys(fcb, recbuf, crp->recno);
173
174     _amseterrcode(errcode, ISOK);
175
176     _issignals_mask();
177     _isdisk_commit();
178     _isdisk_sync();
179     _isdisk_inval();
180
181     /*
182      * Update CNTL Page from the FCB.
183      */
184     (void)_isfcb_cntlpg_w2(fcb);
185     _issignals_unmask();
186
187     _isam_exithook();
188     return (ISOK);
189
190  ERROR:
191     _isdisk_rollback();
192     _isdisk_inval();
193
194     /*
195      * Restore FCB from CNTL page.
196      */
197     if (fcb) (void)_isfcb_cntlpg_r2(fcb);
198
199     _isam_exithook();
200     return (ISERROR);
201 }
202
203
204
205