Index: stable/10/lib/libstand/bzipfs.c =================================================================== --- stable/10/lib/libstand/bzipfs.c (revision 321070) +++ stable/10/lib/libstand/bzipfs.c (revision 321071) @@ -1,390 +1,388 @@ /* * Copyright (c) 1998 Michael Smith. * Copyright (c) 2000 Maxim Sobolev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #ifndef REGRESSION #include "stand.h" #else #include #include #include #include #include struct open_file { int f_flags; /* see F_* below */ void *f_fsdata; /* file system specific data */ }; #define F_READ 0x0001 /* file opened for reading */ #define EOFFSET (ELAST+8) /* relative seek not supported */ static inline u_int min(u_int a, u_int b) { return(a < b ? a : b); } #define panic(x, y) abort() #endif #include #include #include #define BZ_BUFSIZE 2048 /* XXX larger? */ struct bz_file { int bzf_rawfd; bz_stream bzf_bzstream; char bzf_buf[BZ_BUFSIZE]; int bzf_endseen; }; static int bzf_fill(struct bz_file *z); static int bzf_open(const char *path, struct open_file *f); static int bzf_close(struct open_file *f); static int bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid); static off_t bzf_seek(struct open_file *f, off_t offset, int where); static int bzf_stat(struct open_file *f, struct stat *sb); #ifndef REGRESSION struct fs_ops bzipfs_fsops = { "bzip", bzf_open, bzf_close, bzf_read, null_write, bzf_seek, bzf_stat, null_readdir }; #endif static int bzf_fill(struct bz_file *bzf) { int result; int req; req = BZ_BUFSIZE - bzf->bzf_bzstream.avail_in; result = 0; /* If we need more */ if (req > 0) { /* move old data to bottom of buffer */ if (req < BZ_BUFSIZE) bcopy(bzf->bzf_buf + req, bzf->bzf_buf, BZ_BUFSIZE - req); /* read to fill buffer and update availibility data */ result = read(bzf->bzf_rawfd, bzf->bzf_buf + bzf->bzf_bzstream.avail_in, req); bzf->bzf_bzstream.next_in = bzf->bzf_buf; if (result >= 0) bzf->bzf_bzstream.avail_in += result; } return(result); } /* * Adapted from get_byte/check_header in libz * * Returns 0 if the header is OK, nonzero if not. */ static int get_byte(struct bz_file *bzf) { if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1)) return(-1); bzf->bzf_bzstream.avail_in--; return(*(bzf->bzf_bzstream.next_in)++); } static int bz_magic[3] = {'B', 'Z', 'h'}; /* bzip2 magic header */ static int check_header(struct bz_file *bzf) { unsigned int len; int c; /* Check the bzip2 magic header */ for (len = 0; len < 3; len++) { c = get_byte(bzf); if (c != bz_magic[len]) { return(1); } } /* Check that the block size is valid */ c = get_byte(bzf); if (c < '1' || c > '9') return(1); /* Put back bytes that we've took from the input stream */ bzf->bzf_bzstream.next_in -= 4; bzf->bzf_bzstream.avail_in += 4; return(0); } static int bzf_open(const char *fname, struct open_file *f) { static char *bzfname; int rawfd; struct bz_file *bzf; char *cp; int error; struct stat sb; /* Have to be in "just read it" mode */ if (f->f_flags != F_READ) return(EPERM); /* If the name already ends in .gz or .bz2, ignore it */ if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz") || !strcmp(cp, ".bz2") || !strcmp(cp, ".split"))) return(ENOENT); /* Construct new name */ bzfname = malloc(strlen(fname) + 5); if (bzfname == NULL) return(ENOMEM); sprintf(bzfname, "%s.bz2", fname); /* Try to open the compressed datafile */ rawfd = open(bzfname, O_RDONLY); free(bzfname); if (rawfd == -1) return(ENOENT); if (fstat(rawfd, &sb) < 0) { printf("bzf_open: stat failed\n"); close(rawfd); return(ENOENT); } if (!S_ISREG(sb.st_mode)) { printf("bzf_open: not a file\n"); close(rawfd); return(EISDIR); /* best guess */ } /* Allocate a bz_file structure, populate it */ bzf = malloc(sizeof(struct bz_file)); if (bzf == NULL) return(ENOMEM); bzero(bzf, sizeof(struct bz_file)); bzf->bzf_rawfd = rawfd; /* Verify that the file is bzipped */ if (check_header(bzf)) { close(bzf->bzf_rawfd); free(bzf); return(EFTYPE); } /* Initialise the inflation engine */ if ((error = BZ2_bzDecompressInit(&(bzf->bzf_bzstream), 0, 1)) != BZ_OK) { printf("bzf_open: BZ2_bzDecompressInit returned %d\n", error); close(bzf->bzf_rawfd); free(bzf); return(EIO); } /* Looks OK, we'll take it */ f->f_fsdata = bzf; return(0); } static int bzf_close(struct open_file *f) { struct bz_file *bzf = (struct bz_file *)f->f_fsdata; BZ2_bzDecompressEnd(&(bzf->bzf_bzstream)); close(bzf->bzf_rawfd); free(bzf); return(0); } static int bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid) { struct bz_file *bzf = (struct bz_file *)f->f_fsdata; int error; bzf->bzf_bzstream.next_out = buf; /* where and how much */ bzf->bzf_bzstream.avail_out = size; while (bzf->bzf_bzstream.avail_out && bzf->bzf_endseen == 0) { if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1)) { printf("bzf_read: fill error\n"); return(EIO); } if (bzf->bzf_bzstream.avail_in == 0) { /* oops, unexpected EOF */ printf("bzf_read: unexpected EOF\n"); if (bzf->bzf_bzstream.avail_out == size) return(EIO); break; } error = BZ2_bzDecompress(&bzf->bzf_bzstream); /* decompression pass */ if (error == BZ_STREAM_END) { /* EOF, all done */ bzf->bzf_endseen = 1; break; } if (error != BZ_OK) { /* argh, decompression error */ printf("bzf_read: BZ2_bzDecompress returned %d\n", error); return(EIO); } } if (resid != NULL) *resid = bzf->bzf_bzstream.avail_out; return(0); } static int bzf_rewind(struct open_file *f) { struct bz_file *bzf = (struct bz_file *)f->f_fsdata; struct bz_file *bzf_tmp; /* * Since bzip2 does not have an equivalent inflateReset function a crude * one needs to be provided. The functions all called in such a way that * at any time an error occurs a roll back can be done (effectively making * this rewind 'atomic', either the reset occurs successfully or not at all, * with no 'undefined' state happening). */ /* Allocate a bz_file structure, populate it */ bzf_tmp = malloc(sizeof(struct bz_file)); if (bzf_tmp == NULL) return(-1); bzero(bzf_tmp, sizeof(struct bz_file)); bzf_tmp->bzf_rawfd = bzf->bzf_rawfd; /* Initialise the inflation engine */ if (BZ2_bzDecompressInit(&(bzf_tmp->bzf_bzstream), 0, 1) != BZ_OK) { free(bzf_tmp); return(-1); } /* Seek back to the beginning of the file */ if (lseek(bzf->bzf_rawfd, 0, SEEK_SET) == -1) { BZ2_bzDecompressEnd(&(bzf_tmp->bzf_bzstream)); free(bzf_tmp); return(-1); } /* Free old bz_file data */ BZ2_bzDecompressEnd(&(bzf->bzf_bzstream)); free(bzf); /* Use the new bz_file data */ f->f_fsdata = bzf_tmp; return(0); } static off_t bzf_seek(struct open_file *f, off_t offset, int where) { struct bz_file *bzf = (struct bz_file *)f->f_fsdata; off_t target; char discard[16]; switch (where) { case SEEK_SET: target = offset; break; case SEEK_CUR: target = offset + bzf->bzf_bzstream.total_out_lo32; break; - case SEEK_END: - target = -1; default: errno = EINVAL; return(-1); } /* Can we get there from here? */ if (target < bzf->bzf_bzstream.total_out_lo32 && bzf_rewind(f) != 0) { errno = EOFFSET; return -1; } /* if bzf_rewind was called then bzf has changed */ bzf = (struct bz_file *)f->f_fsdata; /* skip forwards if required */ while (target > bzf->bzf_bzstream.total_out_lo32) { errno = bzf_read(f, discard, min(sizeof(discard), target - bzf->bzf_bzstream.total_out_lo32), NULL); if (errno) return(-1); } /* This is where we are (be honest if we overshot) */ return(bzf->bzf_bzstream.total_out_lo32); } static int bzf_stat(struct open_file *f, struct stat *sb) { struct bz_file *bzf = (struct bz_file *)f->f_fsdata; int result; /* stat as normal, but indicate that size is unknown */ if ((result = fstat(bzf->bzf_rawfd, sb)) == 0) sb->st_size = -1; return(result); } void bz_internal_error(int errorcode) { panic("bzipfs: critical error %d in bzip2 library occured\n", errorcode); } #ifdef REGRESSION /* Small test case, open and decompress test.bz2 */ int main() { struct open_file f; char buf[1024]; size_t resid; int err; memset(&f, '\0', sizeof(f)); f.f_flags = F_READ; err = bzf_open("test", &f); if (err != 0) exit(1); do { err = bzf_read(&f, buf, sizeof(buf), &resid); } while (err == 0 && resid != sizeof(buf)); if (err != 0) exit(2); exit(0); } #endif Index: stable/10/lib/libstand/gzipfs.c =================================================================== --- stable/10/lib/libstand/gzipfs.c (revision 321070) +++ stable/10/lib/libstand/gzipfs.c (revision 321071) @@ -1,339 +1,337 @@ /* * Copyright (c) 1998 Michael Smith. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "stand.h" #include #include #include #define Z_BUFSIZE 2048 /* XXX larger? */ struct z_file { int zf_rawfd; off_t zf_dataoffset; z_stream zf_zstream; char zf_buf[Z_BUFSIZE]; int zf_endseen; }; static int zf_fill(struct z_file *z); static int zf_open(const char *path, struct open_file *f); static int zf_close(struct open_file *f); static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid); static off_t zf_seek(struct open_file *f, off_t offset, int where); static int zf_stat(struct open_file *f, struct stat *sb); struct fs_ops gzipfs_fsops = { "zip", zf_open, zf_close, zf_read, null_write, zf_seek, zf_stat, null_readdir }; static int zf_fill(struct z_file *zf) { int result; int req; req = Z_BUFSIZE - zf->zf_zstream.avail_in; result = 0; /* If we need more */ if (req > 0) { /* move old data to bottom of buffer */ if (req < Z_BUFSIZE) bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req); /* read to fill buffer and update availibility data */ result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req); zf->zf_zstream.next_in = zf->zf_buf; if (result >= 0) zf->zf_zstream.avail_in += result; } return(result); } /* * Adapted from get_byte/check_header in libz * * Returns 0 if the header is OK, nonzero if not. */ static int get_byte(struct z_file *zf, off_t *curoffp) { if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) return(-1); zf->zf_zstream.avail_in--; ++*curoffp; return(*(zf->zf_zstream.next_in)++); } static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ /* gzip flag byte */ #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ #define COMMENT 0x10 /* bit 4 set: file comment present */ #define RESERVED 0xE0 /* bits 5..7: reserved */ static int check_header(struct z_file *zf) { int method; /* method byte */ int flags; /* flags byte */ uInt len; int c; zf->zf_dataoffset = 0; /* Check the gzip magic header */ for (len = 0; len < 2; len++) { c = get_byte(zf, &zf->zf_dataoffset); if (c != gz_magic[len]) { return(1); } } method = get_byte(zf, &zf->zf_dataoffset); flags = get_byte(zf, &zf->zf_dataoffset); if (method != Z_DEFLATED || (flags & RESERVED) != 0) { return(1); } /* Discard time, xflags and OS code: */ for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset); if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ len = (uInt)get_byte(zf, &zf->zf_dataoffset); len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8; /* len is garbage if EOF but the loop below will quit anyway */ while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ; } if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ; } if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ; } if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset); } /* if there's data left, we're in business */ return((c == -1) ? 1 : 0); } static int zf_open(const char *fname, struct open_file *f) { static char *zfname; int rawfd; struct z_file *zf; char *cp; int error; struct stat sb; /* Have to be in "just read it" mode */ if (f->f_flags != F_READ) return(EPERM); /* If the name already ends in .gz or .bz2, ignore it */ if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz") || !strcmp(cp, ".bz2") || !strcmp(cp, ".split"))) return(ENOENT); /* Construct new name */ zfname = malloc(strlen(fname) + 4); if (zfname == NULL) return(ENOMEM); sprintf(zfname, "%s.gz", fname); /* Try to open the compressed datafile */ rawfd = open(zfname, O_RDONLY); free(zfname); if (rawfd == -1) return(ENOENT); if (fstat(rawfd, &sb) < 0) { printf("zf_open: stat failed\n"); close(rawfd); return(ENOENT); } if (!S_ISREG(sb.st_mode)) { printf("zf_open: not a file\n"); close(rawfd); return(EISDIR); /* best guess */ } /* Allocate a z_file structure, populate it */ zf = malloc(sizeof(struct z_file)); if (zf == NULL) return(ENOMEM); bzero(zf, sizeof(struct z_file)); zf->zf_rawfd = rawfd; /* Verify that the file is gzipped */ if (check_header(zf)) { close(zf->zf_rawfd); free(zf); return(EFTYPE); } /* Initialise the inflation engine */ if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) { printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg); close(zf->zf_rawfd); free(zf); return(EIO); } /* Looks OK, we'll take it */ f->f_fsdata = zf; return(0); } static int zf_close(struct open_file *f) { struct z_file *zf = (struct z_file *)f->f_fsdata; inflateEnd(&(zf->zf_zstream)); close(zf->zf_rawfd); free(zf); return(0); } static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid) { struct z_file *zf = (struct z_file *)f->f_fsdata; int error; zf->zf_zstream.next_out = buf; /* where and how much */ zf->zf_zstream.avail_out = size; while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) { if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) { printf("zf_read: fill error\n"); return(EIO); } if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */ printf("zf_read: unexpected EOF\n"); if (zf->zf_zstream.avail_out == size) return(EIO); break; } error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */ if (error == Z_STREAM_END) { /* EOF, all done */ zf->zf_endseen = 1; break; } if (error != Z_OK) { /* argh, decompression error */ printf("inflate: %s\n", zf->zf_zstream.msg); return(EIO); } } if (resid != NULL) *resid = zf->zf_zstream.avail_out; return(0); } static int zf_rewind(struct open_file *f) { struct z_file *zf = (struct z_file *)f->f_fsdata; if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1) return(-1); zf->zf_zstream.avail_in = 0; zf->zf_zstream.next_in = NULL; zf->zf_endseen = 0; (void)inflateReset(&zf->zf_zstream); return(0); } static off_t zf_seek(struct open_file *f, off_t offset, int where) { struct z_file *zf = (struct z_file *)f->f_fsdata; off_t target; char discard[16]; switch (where) { case SEEK_SET: target = offset; break; case SEEK_CUR: target = offset + zf->zf_zstream.total_out; break; - case SEEK_END: - target = -1; default: errno = EINVAL; return(-1); } /* rewind if required */ if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0) return(-1); /* skip forwards if required */ while (target > zf->zf_zstream.total_out) { errno = zf_read(f, discard, min(sizeof(discard), target - zf->zf_zstream.total_out), NULL); if (errno) return(-1); } /* This is where we are (be honest if we overshot) */ return(zf->zf_zstream.total_out); } static int zf_stat(struct open_file *f, struct stat *sb) { struct z_file *zf = (struct z_file *)f->f_fsdata; int result; /* stat as normal, but indicate that size is unknown */ if ((result = fstat(zf->zf_rawfd, sb)) == 0) sb->st_size = -1; return(result); } Index: stable/10/lib/libstand/lseek.c =================================================================== --- stable/10/lib/libstand/lseek.c (revision 321070) +++ stable/10/lib/libstand/lseek.c (revision 321071) @@ -1,142 +1,141 @@ /* $NetBSD: lseek.c,v 1.4 1997/01/22 00:38:10 cgd Exp $ */ /*- * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * The Mach Operating System project at Carnegie-Mellon University. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)lseek.c 8.1 (Berkeley) 6/11/93 * * * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University * All Rights Reserved. * * Author: Alessandro Forin * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie the * rights to redistribute these changes. */ #include __FBSDID("$FreeBSD$"); #include "stand.h" off_t lseek(int fd, off_t offset, int where) { off_t bufpos, filepos, target; struct open_file *f = &files[fd]; if ((unsigned)fd >= SOPEN_MAX || f->f_flags == 0) { errno = EBADF; return (-1); } if (f->f_flags & F_RAW) { /* * On RAW devices, update internal offset. */ switch (where) { case SEEK_SET: f->f_offset = offset; break; case SEEK_CUR: f->f_offset += offset; break; - case SEEK_END: default: errno = EOFFSET; return (-1); } return (f->f_offset); } /* * If there is some unconsumed data in the readahead buffer and it * contains the desired offset, simply adjust the buffer offset and * length. We don't bother with SEEK_END here, since the code to * handle it would fail in the same cases where the non-readahead * code fails (namely, for streams which cannot seek backward and whose * size isn't known in advance). */ if (f->f_ralen != 0 && where != SEEK_END) { if ((filepos = (f->f_ops->fo_seek)(f, (off_t)0, SEEK_CUR)) == -1) return (-1); bufpos = filepos - f->f_ralen; switch (where) { case SEEK_SET: target = offset; break; case SEEK_CUR: target = bufpos + offset; break; default: errno = EINVAL; return (-1); } if (bufpos <= target && target < filepos) { f->f_raoffset += target - bufpos; f->f_ralen -= target - bufpos; return (target); } } /* * If this is a relative seek, we need to correct the offset for * bytes that we have already read but the caller doesn't know * about. */ if (where == SEEK_CUR) offset -= f->f_ralen; /* * Invalidate the readahead buffer. */ f->f_ralen = 0; return (f->f_ops->fo_seek)(f, offset, where); } Index: stable/10 =================================================================== --- stable/10 (revision 321070) +++ stable/10 (revision 321071) Property changes on: stable/10 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r320468