Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / mini_isam / isfileio.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 librararies 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: isfileio.c /main/3 1995/10/23 11:39:26 rswiston $                                                    */
28 #ifndef lint
29 static char sccsid[] = "@(#)isfileio.c 1.3 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isfileio.c
37  *
38  * Description:
39  *      I/O operations on UNIX files associated with FCB
40  */
41
42 #include "isam_impl.h"
43
44 static int _getoffset();
45 static Blkno _getblkno();
46
47
48 /*
49  * _cp_tofile(fcb, unixfd, data, pos, len)
50  *
51  * Copy data to a file associated with FCB (unixfd distinguishes among 
52  * .rec, .ind., and .var file). The data are copied from buffer data to
53  * the file at offset pos. The number of bytes to copy is in len.
54  */
55
56 void
57 _cp_tofile(fcb, unixfd, data, pos, len)
58     Fcb         *fcb;
59     int         unixfd;
60     char        *data;
61     long        pos;
62     int         len;
63 {
64     int         offset;                      /* Offset within a page */
65     Blkno       blkno;
66     Bufhdr      *bufhdr;
67     int         nbytes;
68
69     /*
70      * Data may span multiple blocks.
71      */
72     while (len > 0) {
73         blkno = _getblkno(pos);
74         offset = _getoffset(pos);
75         nbytes = (len < ISPAGESIZE - offset) ? len : ISPAGESIZE - offset;
76
77         bufhdr = _isdisk_fix(fcb, unixfd, blkno, ISFIXWRITE);
78         memcpy( bufhdr->isb_buffer + offset,data, nbytes);
79         _isdisk_unfix(bufhdr);
80
81         pos  += nbytes;
82         data += nbytes;
83         len  -= nbytes;
84     }
85 }
86
87 /*
88  * _cp_fromfile(fcb, unixfd, data, pos, len)
89  *
90  * Copy data from a file associated with FCB (unixfd distinguishes among 
91  * .rec, .ind., and .var file). The data are copied to buffer data from
92  * the file at offset pos. The number of bytes to copy is in len.
93  */
94
95 void
96 _cp_fromfile(fcb, unixfd, data, pos, len)
97     Fcb         *fcb;
98     int         unixfd;
99     char        *data;
100     long        pos;
101     int         len;
102 {
103     int         offset;                      /* Offset within a page */
104     Blkno       blkno;
105     Bufhdr      *bufhdr;
106     int         nbytes;
107
108     /*
109      * Data may span multiple blocks.
110      */
111     while (len > 0) {
112         blkno = _getblkno(pos);
113         offset = _getoffset(pos);
114         nbytes = (len < ISPAGESIZE - offset) ? len : ISPAGESIZE - offset;
115
116         bufhdr = _isdisk_fix(fcb, unixfd, blkno, ISFIXREAD);
117         memcpy( data,bufhdr->isb_buffer + offset, nbytes);
118         _isdisk_unfix(bufhdr);
119
120         pos  += nbytes;
121         data += nbytes;
122         len  -= nbytes;
123     }
124 }
125
126 /*
127  * extend_file(fcb, unixfd, oldsize)
128  *
129  * Extend UNIX file by one block.
130  */
131
132 Blkno 
133 _extend_file(fcb, unixfd, oldsize)
134     Fcb                 *fcb;
135     int                 unixfd;
136     Blkno               oldsize;
137 {
138     Bufhdr              *bufhdr;
139     char                buf[ISPAGESIZE];
140
141     /* 
142      * We must write something to the buffer, or we will get 
143      * segmentation fault when using mapped I/O.
144      */
145     _isseekpg(unixfd, oldsize);
146     _iswritepg(unixfd, buf);
147
148     bufhdr = _isdisk_fix(fcb, unixfd, oldsize, ISFIXNOREAD); 
149     _isdisk_unfix(bufhdr);
150
151     return (oldsize + 1);
152 }
153
154 /*
155  * off = _getoffset(pos)
156  *
157  * Calculate offset relative to the beginning of page
158  */
159
160 Static int
161 _getoffset(pos)
162     long                pos;
163 {
164     return ((int)(pos % ISPAGESIZE));
165 }
166
167 /*
168  * blkno = _getblkno(pos)
169  *
170  * Calculate block number of the block containing position pos.
171  */
172
173 Static Blkno
174 _getblkno(pos)
175     long                pos;
176 {
177     return ((int)(pos / ISPAGESIZE));
178 }