2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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: isrewrec.c /main/3 1995/10/23 11:44:28 rswiston $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Rewrite a record in ISAM file.
40 #include "isam_impl.h"
43 static int _amrewrec(), _changekeys();
46 * err = isrewrec(isfd, recnum, record)
48 * Isrewrec() modifies a record in ISAM file. The record is identified
49 * by its record number. All indexes of the ISAM file are updated.
51 * Current record position is not changed.
52 * isrecnum is set to recnum.
54 * If the ISAM file is for variable length records, the isreclen variable
55 * must be set to indicate the actual length of the record, which must
56 * be between the minimum and maximum length, as specified in isbuild().
58 * Returns 0 if successful, or -1 of any error.
61 * EDUPL The write woul result in a duplicate on a key that
62 * does not allow duplicates.
63 * ELOCKED The file has been locked by another process.
64 * ENOTOPEN isfd does not correspond to an open ISAM file, or the
65 * ISAM file was not opened with ISINOUT mode.
66 * ENOREC Record with record number recnum does not exist.
70 isrewrec(int isfd, long recnum, char *record)
77 * Get File Access Block.
79 if ((fab = _isfd_find(isfd)) == NULL) {
80 _setiserrno2(ENOTOPEN, '9', '0');
85 * Check that the open mode was ISINOUT.
87 if (fab->openmode != OM_INOUT) {
88 _setiserrno2(ENOTOPEN, '9', '0');
93 * Determine record length. Check it against min and max record length.
95 reclen = (fab->varlength == TRUE) ? isreclen : fab->minreclen;
96 if (reclen < fab->minreclen || reclen > fab->maxreclen) {
97 _setiserrno2(EBADARG, '9', '0');
101 if ((ret = _amrewrec(&fab->isfhandle, record,
102 reclen, recnum, &fab->errcode)) == ISOK) {
103 isrecnum = recnum; /* Set isrecnum */
106 _seterr_errcode(&fab->errcode);
108 return (ret); /* Successful write */
112 * _amrewrec(isfhandle, record, reclen, recnum, errcode)
114 * _amrewrec() rewrites a record in ISAM file.
117 * isfhandle Handle of ISAM file
119 * reclen length of the record
120 * recnum record number of record to be deleted
123 * errcode error status of the operation
128 _amrewrec(Bytearray *isfhandle, char *record, int reclen, Recno recnum,
129 struct errcode *errcode)
132 char oldrecbuf[ISMAXRECLEN];
136 int (*rec_rewrite)();
141 * Get FCB corresponding to the isfhandle handle.
143 if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
148 rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
149 rec_rewrite = (fcb->varflag?_vlrec_rewrite:_flrec_rewrite);
152 * Update information in FCB from CNTL page on the disk
154 (void)_isfcb_cntlpg_r2(fcb);
157 * We must read the record first to be able to delete keys.
159 if (rec_read(fcb, oldrecbuf, recnum, &reclen2) != ISOK) {
160 _amseterrcode(errcode, ENOREC);
164 if (rec_rewrite(fcb, record, recnum, reclen) != ISOK) {
165 _amseterrcode(errcode, ENOREC);
172 if ((err = _changekeys(fcb, record, oldrecbuf, recnum)) != ISOK) {
173 _amseterrcode(errcode, err);
177 _amseterrcode(errcode, ISOK);
185 * Update control page.
187 (void)_isfcb_cntlpg_w2(fcb);
198 * Restore FCB from CNTL page.
200 (void)_isfcb_cntlpg_r2(fcb);
207 _changekeys (Fcb *fcb, char *record, char *oldrecord, Recno recnum)
209 int nkeys = fcb->nkeys;
213 for (i = 0; i < nkeys; i++) {
214 if ((err =_change1key(fcb, fcb->keys + i, record, oldrecord, recnum,