Asterisk - The Open Source Telephony Project  18.5.0
rec_get.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  * The Regents of the University of California. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  * This product includes software developed by the University of
16  * California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)rec_get.c 8.9 (Berkeley) 8/18/94";
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <sys/types.h>
39 
40 #include <errno.h>
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "../include/db.h"
48 #include "recno.h"
49 
50 /*
51  * __REC_GET -- Get a record from the btree.
52  *
53  * Parameters:
54  * dbp: pointer to access method
55  * key: key to find
56  * data: data to return
57  * flag: currently unused
58  *
59  * Returns:
60  * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
61  */
62 int
63 __rec_get(dbp, key, data, flags)
64  const DB *dbp;
65  const DBT *key;
66  DBT *data;
67  u_int flags;
68 {
69  BTREE *t;
70  EPG *e;
71  recno_t nrec;
72  int status;
73 
74  t = dbp->internal;
75 
76  /* Toss any page pinned across calls. */
77  if (t->bt_pinned != NULL) {
78  mpool_put(t->bt_mp, t->bt_pinned, 0);
79  t->bt_pinned = NULL;
80  }
81 
82  /* Get currently doesn't take any flags, and keys of 0 are illegal. */
83  if (flags || (nrec = *(recno_t *)key->data) == 0) {
84  errno = EINVAL;
85  return (RET_ERROR);
86  }
87 
88  /*
89  * If we haven't seen this record yet, try to find it in the
90  * original file.
91  */
92  if (nrec > t->bt_nrecs) {
93  if (F_ISSET(t, R_EOF | R_INMEM))
94  return (RET_SPECIAL);
95  if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
96  return (status);
97  }
98 
99  --nrec;
100  if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
101  return (RET_ERROR);
102 
103  status = __rec_ret(t, e, 0, NULL, data);
104  if (F_ISSET(t, B_DB_LOCK))
105  mpool_put(t->bt_mp, e->page, 0);
106  else
107  t->bt_pinned = e->page;
108  return (status);
109 }
110 
111 /*
112  * __REC_FPIPE -- Get fixed length records from a pipe.
113  *
114  * Parameters:
115  * t: tree
116  * cnt: records to read
117  *
118  * Returns:
119  * RET_ERROR, RET_SUCCESS
120  */
121 int
122 __rec_fpipe(t, top)
123  BTREE *t;
124  recno_t top;
125 {
126  DBT data;
127  recno_t nrec;
128  size_t len;
129  int ch;
130  u_char *p;
131 
132  if (t->bt_rdata.size < t->bt_reclen) {
133  t->bt_rdata.data = t->bt_rdata.data == NULL ?
134  malloc(t->bt_reclen) :
135  realloc(t->bt_rdata.data, t->bt_reclen);
136  if (t->bt_rdata.data == NULL)
137  return (RET_ERROR);
138  t->bt_rdata.size = t->bt_reclen;
139  }
140  data.data = t->bt_rdata.data;
141  data.size = t->bt_reclen;
142 
143  for (nrec = t->bt_nrecs; nrec < top;) {
144  len = t->bt_reclen;
145  for (p = t->bt_rdata.data;; *p++ = ch)
146  if ((ch = getc(t->bt_rfp)) == EOF || !--len) {
147  if (ch != EOF)
148  *p = ch;
149  if (len != 0)
150  memset(p, t->bt_bval, len);
151  if (__rec_iput(t,
152  nrec, &data, 0) != RET_SUCCESS)
153  return (RET_ERROR);
154  ++nrec;
155  break;
156  }
157  if (ch == EOF)
158  break;
159  }
160  if (nrec < top) {
161  F_SET(t, R_EOF);
162  return (RET_SPECIAL);
163  }
164  return (RET_SUCCESS);
165 }
166 
167 /*
168  * __REC_VPIPE -- Get variable length records from a pipe.
169  *
170  * Parameters:
171  * t: tree
172  * cnt: records to read
173  *
174  * Returns:
175  * RET_ERROR, RET_SUCCESS
176  */
177 int
178 __rec_vpipe(t, top)
179  BTREE *t;
180  recno_t top;
181 {
182  DBT data;
183  recno_t nrec;
184  indx_t len;
185  size_t sz;
186  int bval, ch;
187  u_char *p;
188 
189  bval = t->bt_bval;
190  for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
191  for (p = t->bt_rdata.data,
192  sz = t->bt_rdata.size;; *p++ = ch, --sz) {
193  if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
194  data.data = t->bt_rdata.data;
195  data.size = p - (u_char *)t->bt_rdata.data;
196  if (ch == EOF && data.size == 0)
197  break;
198  if (__rec_iput(t, nrec, &data, 0)
199  != RET_SUCCESS)
200  return (RET_ERROR);
201  break;
202  }
203  if (sz == 0) {
204  len = p - (u_char *)t->bt_rdata.data;
205  t->bt_rdata.size += (sz = 256);
206  t->bt_rdata.data = t->bt_rdata.data == NULL ?
207  malloc(t->bt_rdata.size) :
209  if (t->bt_rdata.data == NULL)
210  return (RET_ERROR);
211  p = (u_char *)t->bt_rdata.data + len;
212  }
213  }
214  if (ch == EOF)
215  break;
216  }
217  if (nrec < top) {
218  F_SET(t, R_EOF);
219  return (RET_SPECIAL);
220  }
221  return (RET_SUCCESS);
222 }
223 
224 /*
225  * __REC_FMAP -- Get fixed length records from a file.
226  *
227  * Parameters:
228  * t: tree
229  * cnt: records to read
230  *
231  * Returns:
232  * RET_ERROR, RET_SUCCESS
233  */
234 int
235 __rec_fmap(t, top)
236  BTREE *t;
237  recno_t top;
238 {
239  DBT data;
240  recno_t nrec;
241  u_char *sp, *ep, *p;
242  size_t len;
243 
244  if (t->bt_rdata.size < t->bt_reclen) {
245  t->bt_rdata.data = t->bt_rdata.data == NULL ?
246  malloc(t->bt_reclen) :
247  realloc(t->bt_rdata.data, t->bt_reclen);
248  if (t->bt_rdata.data == NULL)
249  return (RET_ERROR);
250  t->bt_rdata.size = t->bt_reclen;
251  }
252  data.data = t->bt_rdata.data;
253  data.size = t->bt_reclen;
254 
255  sp = (u_char *)t->bt_cmap;
256  ep = (u_char *)t->bt_emap;
257  for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
258  if (sp >= ep) {
259  F_SET(t, R_EOF);
260  return (RET_SPECIAL);
261  }
262  len = t->bt_reclen;
263  for (p = t->bt_rdata.data;
264  sp < ep && len > 0; *p++ = *sp++, --len);
265  if (len != 0)
266  memset(p, t->bt_bval, len);
267  if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
268  return (RET_ERROR);
269  }
270  t->bt_cmap = (caddr_t)sp;
271  return (RET_SUCCESS);
272 }
273 
274 /*
275  * __REC_VMAP -- Get variable length records from a file.
276  *
277  * Parameters:
278  * t: tree
279  * cnt: records to read
280  *
281  * Returns:
282  * RET_ERROR, RET_SUCCESS
283  */
284 int
285 __rec_vmap(t, top)
286  BTREE *t;
287  recno_t top;
288 {
289  DBT data;
290  u_char *sp, *ep;
291  recno_t nrec;
292  int bval;
293 
294  sp = (u_char *)t->bt_cmap;
295  ep = (u_char *)t->bt_emap;
296  bval = t->bt_bval;
297 
298  for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
299  if (sp >= ep) {
300  F_SET(t, R_EOF);
301  return (RET_SPECIAL);
302  }
303  for (data.data = sp; sp < ep && *sp != bval; ++sp);
304  data.size = sp - (u_char *)data.data;
305  if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
306  return (RET_ERROR);
307  ++sp;
308  }
309  t->bt_cmap = (caddr_t)sp;
310  return (RET_SUCCESS);
311 }
int __rec_fpipe(BTREE *t, recno_t top)
Definition: rec_get.c:122
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
#define RET_ERROR
Definition: db.h:51
#define realloc(a, b)
Definition: astmm.h:163
#define F_SET(p, f)
Definition: btree.h:40
#define F_ISSET(p, f)
Definition: btree.h:42
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
Definition: btree.h:254
void * internal
Definition: db.h:137
Definition: db.h:85
int __rec_fmap(BTREE *t, recno_t top)
Definition: rec_get.c:235
#define NULL
Definition: resample.c:96
u_int16_t indx_t
Definition: db.h:80
EPG * __rec_search(BTREE *t, recno_t recno, enum SRCHOP op)
Definition: rec_search.c:64
recno_t bt_nrecs
Definition: btree.h:360
Definition: btree.h:312
char * malloc()
int __rec_ret(BTREE *t, EPG *e, recno_t nrec, DBT *key, DBT *data)
Definition: rec_utils.c:62
u_char bt_bval
Definition: btree.h:362
#define RET_SUCCESS
Definition: db.h:52
PAGE * bt_pinned
Definition: btree.h:318
DBT bt_rdata
Definition: btree.h:333
PAGE * page
Definition: btree.h:255
FILE * bt_rfp
Definition: btree.h:352
MPOOL * bt_mp
Definition: btree.h:313
static DB * dbp
Definition: hsearch.c:49
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
#define R_INMEM
Definition: btree.h:381
Definition: recno.h:36
#define R_EOF
Definition: btree.h:378
size_t bt_reclen
Definition: btree.h:361
int __rec_vmap(BTREE *t, recno_t top)
Definition: rec_get.c:285
int __rec_vpipe(BTREE *t, recno_t top)
Definition: rec_get.c:178
caddr_t bt_cmap
Definition: btree.h:355
Definition: db.h:129
int __rec_get(DB *dbp, const DBT *key, DBT *data, u_int flags) const
Definition: rec_get.c:63
#define RET_SPECIAL
Definition: db.h:53
u_int32_t recno_t
Definition: db.h:82
caddr_t bt_emap
Definition: btree.h:357
int __rec_iput(BTREE *t, recno_t nrec, const DBT *data, u_int flags)
Definition: rec_put.c:190
#define B_DB_LOCK
Definition: btree.h:385
jack_status_t status
Definition: app_jack.c:146