150995Sbostic /*- 250995Sbostic * Copyright (c) 1990 The Regents of the University of California. 350995Sbostic * All rights reserved. 450995Sbostic * 550995Sbostic * %sccs.include.redist.c% 650995Sbostic */ 750995Sbostic 850995Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*58747Sbostic static char sccsid[] = "@(#)rec_get.c 5.8 (Berkeley) 03/19/93"; 1050995Sbostic #endif /* LIBC_SCCS and not lint */ 1150995Sbostic 1250995Sbostic #include <sys/types.h> 1356753Sbostic 1450995Sbostic #include <errno.h> 1550995Sbostic #include <stddef.h> 1650995Sbostic #include <stdio.h> 1750995Sbostic #include <stdlib.h> 1850995Sbostic #include <string.h> 1956753Sbostic #include <unistd.h> 2056753Sbostic 2157933Sbostic #include <db.h> 2251086Sbostic #include "recno.h" 2350995Sbostic 2450995Sbostic /* 2550995Sbostic * __REC_GET -- Get a record from the btree. 2650995Sbostic * 2750995Sbostic * Parameters: 2850995Sbostic * dbp: pointer to access method 2950995Sbostic * key: key to find 3050995Sbostic * data: data to return 3150995Sbostic * flag: currently unused 3250995Sbostic * 3350995Sbostic * Returns: 3450995Sbostic * RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found. 3550995Sbostic */ 3650995Sbostic int 3750995Sbostic __rec_get(dbp, key, data, flags) 3850995Sbostic const DB *dbp; 3951086Sbostic const DBT *key; 4051086Sbostic DBT *data; 4150995Sbostic u_int flags; 4250995Sbostic { 4350995Sbostic BTREE *t; 4450995Sbostic EPG *e; 4550995Sbostic recno_t nrec; 4654279Sbostic int status; 4750995Sbostic 4850995Sbostic if (flags || (nrec = *(recno_t *)key->data) == 0) { 4950995Sbostic errno = EINVAL; 5050995Sbostic return (RET_ERROR); 5150995Sbostic } 5250995Sbostic 5350995Sbostic /* 5450995Sbostic * If we haven't seen this record yet, try to find it in the 5550995Sbostic * original file. 5650995Sbostic */ 5750995Sbostic t = dbp->internal; 58*58747Sbostic if (nrec > t->bt_nrecs) { 59*58747Sbostic if (ISSET(t, BTF_EOF | BTF_RINMEM)) 60*58747Sbostic return (RET_SPECIAL); 61*58747Sbostic if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS) 6250995Sbostic return (status); 63*58747Sbostic } 6450995Sbostic 6550995Sbostic --nrec; 6651086Sbostic if ((e = __rec_search(t, nrec, SEARCH)) == NULL) 6750995Sbostic return (RET_ERROR); 6850995Sbostic 6956753Sbostic status = __rec_ret(t, e, 0, NULL, data); 7050995Sbostic mpool_put(t->bt_mp, e->page, 0); 7150995Sbostic return (status); 7250995Sbostic } 7350995Sbostic 7450995Sbostic /* 7550995Sbostic * __REC_FPIPE -- Get fixed length records from a pipe. 7650995Sbostic * 7750995Sbostic * Parameters: 7850995Sbostic * t: tree 7950995Sbostic * cnt: records to read 8050995Sbostic * 8150995Sbostic * Returns: 8250995Sbostic * RET_ERROR, RET_SUCCESS 8350995Sbostic */ 8450995Sbostic int 8550995Sbostic __rec_fpipe(t, top) 8650995Sbostic BTREE *t; 8750995Sbostic recno_t top; 8850995Sbostic { 8950995Sbostic DBT data; 9050995Sbostic recno_t nrec; 9150995Sbostic size_t len; 9250995Sbostic int ch; 9350995Sbostic char *p; 9450995Sbostic 9550995Sbostic data.data = t->bt_dbuf; 9650995Sbostic data.size = t->bt_reclen; 9750995Sbostic 9850995Sbostic if (t->bt_dbufsz < t->bt_reclen) { 9950995Sbostic if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL) 10050995Sbostic return (RET_ERROR); 10150995Sbostic t->bt_dbufsz = t->bt_reclen; 10250995Sbostic } 10350995Sbostic for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 10456753Sbostic len = t->bt_reclen; 10550995Sbostic for (p = t->bt_dbuf;; *p++ = ch) 10650995Sbostic if ((ch = getc(t->bt_rfp)) == EOF || !len--) { 10750995Sbostic if (__rec_iput(t, nrec, &data, 0) 10850995Sbostic != RET_SUCCESS) 10950995Sbostic return (RET_ERROR); 11050995Sbostic break; 11150995Sbostic } 11250995Sbostic if (ch == EOF) 11350995Sbostic break; 11450995Sbostic } 11550995Sbostic if (nrec < top) { 116*58747Sbostic SET(t, BTF_EOF); 11750995Sbostic return (RET_SPECIAL); 11850995Sbostic } 11950995Sbostic return (RET_SUCCESS); 12050995Sbostic } 12150995Sbostic 12250995Sbostic /* 12350995Sbostic * __REC_VPIPE -- Get variable length records from a pipe. 12450995Sbostic * 12550995Sbostic * Parameters: 12650995Sbostic * t: tree 12750995Sbostic * cnt: records to read 12850995Sbostic * 12950995Sbostic * Returns: 13050995Sbostic * RET_ERROR, RET_SUCCESS 13150995Sbostic */ 13250995Sbostic int 13350995Sbostic __rec_vpipe(t, top) 13450995Sbostic BTREE *t; 13550995Sbostic recno_t top; 13650995Sbostic { 13750995Sbostic DBT data; 13850995Sbostic recno_t nrec; 13957988Sbostic indx_t len; 14050995Sbostic size_t sz; 14150995Sbostic int bval, ch; 14250995Sbostic char *p; 14350995Sbostic 14450995Sbostic bval = t->bt_bval; 14550995Sbostic for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 14650995Sbostic for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) { 14750995Sbostic if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) { 14850995Sbostic data.data = t->bt_dbuf; 14950995Sbostic data.size = p - t->bt_dbuf; 15050995Sbostic if (__rec_iput(t, nrec, &data, 0) 15150995Sbostic != RET_SUCCESS) 15250995Sbostic return (RET_ERROR); 15350995Sbostic break; 15450995Sbostic } 15550995Sbostic if (sz == 0) { 15650995Sbostic len = p - t->bt_dbuf; 15758098Sbostic t->bt_dbufsz += (sz = 256); 15850995Sbostic if ((t->bt_dbuf = 15958098Sbostic realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL) 16050995Sbostic return (RET_ERROR); 16150995Sbostic p = t->bt_dbuf + len; 16250995Sbostic } 16350995Sbostic } 16450995Sbostic if (ch == EOF) 16550995Sbostic break; 16650995Sbostic } 16750995Sbostic if (nrec < top) { 168*58747Sbostic SET(t, BTF_EOF); 16950995Sbostic return (RET_SPECIAL); 17050995Sbostic } 17150995Sbostic return (RET_SUCCESS); 17250995Sbostic } 17350995Sbostic 17450995Sbostic /* 17550995Sbostic * __REC_FMAP -- Get fixed length records from a file. 17650995Sbostic * 17750995Sbostic * Parameters: 17850995Sbostic * t: tree 17950995Sbostic * cnt: records to read 18050995Sbostic * 18150995Sbostic * Returns: 18250995Sbostic * RET_ERROR, RET_SUCCESS 18350995Sbostic */ 18450995Sbostic int 18550995Sbostic __rec_fmap(t, top) 18650995Sbostic BTREE *t; 18750995Sbostic recno_t top; 18850995Sbostic { 18950995Sbostic DBT data; 19050995Sbostic recno_t nrec; 19150995Sbostic caddr_t sp, ep; 19250995Sbostic size_t len; 19350995Sbostic char *p; 19450995Sbostic 195*58747Sbostic sp = t->bt_cmap; 19650995Sbostic ep = t->bt_emap; 19750995Sbostic data.data = t->bt_dbuf; 19850995Sbostic data.size = t->bt_reclen; 19950995Sbostic 20050995Sbostic if (t->bt_dbufsz < t->bt_reclen) { 20150995Sbostic if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL) 20250995Sbostic return (RET_ERROR); 20350995Sbostic t->bt_dbufsz = t->bt_reclen; 20450995Sbostic } 20550995Sbostic for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 20650995Sbostic if (sp >= ep) { 207*58747Sbostic SET(t, BTF_EOF); 20850995Sbostic return (RET_SPECIAL); 20950995Sbostic } 21050995Sbostic len = t->bt_reclen; 21150995Sbostic for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++); 21250995Sbostic memset(p, t->bt_bval, len); 21350995Sbostic if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 21450995Sbostic return (RET_ERROR); 21550995Sbostic } 216*58747Sbostic t->bt_cmap = sp; 21750995Sbostic return (RET_SUCCESS); 21850995Sbostic } 21950995Sbostic 22050995Sbostic /* 22150995Sbostic * __REC_VMAP -- Get variable length records from a file. 22250995Sbostic * 22350995Sbostic * Parameters: 22450995Sbostic * t: tree 22550995Sbostic * cnt: records to read 22650995Sbostic * 22750995Sbostic * Returns: 22850995Sbostic * RET_ERROR, RET_SUCCESS 22950995Sbostic */ 23050995Sbostic int 23150995Sbostic __rec_vmap(t, top) 23250995Sbostic BTREE *t; 23350995Sbostic recno_t top; 23450995Sbostic { 23550995Sbostic DBT data; 23651086Sbostic caddr_t sp, ep; 23750995Sbostic recno_t nrec; 23850995Sbostic int bval; 23950995Sbostic 240*58747Sbostic sp = t->bt_cmap; 24150995Sbostic ep = t->bt_emap; 24250995Sbostic bval = t->bt_bval; 24350995Sbostic 24450995Sbostic for (nrec = t->bt_nrecs; nrec < top; ++nrec) { 24550995Sbostic if (sp >= ep) { 246*58747Sbostic SET(t, BTF_EOF); 24750995Sbostic return (RET_SPECIAL); 24850995Sbostic } 24950995Sbostic for (data.data = sp; sp < ep && *sp != bval; ++sp); 25050995Sbostic data.size = sp - (caddr_t)data.data; 25150995Sbostic if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS) 25250995Sbostic return (RET_ERROR); 25350995Sbostic ++sp; 25450995Sbostic } 255*58747Sbostic t->bt_cmap = sp; 25650995Sbostic return (RET_SUCCESS); 25750995Sbostic } 258