File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libpdel / io / count_fp.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 23:25:53 2012 UTC (13 years, 1 month ago) by misho
Branches: libpdel, MAIN
CVS tags: v0_5_3, HEAD
libpdel

    1: 
    2: /*
    3:  * Copyright (c) 2001-2002 Packet Design, LLC.
    4:  * All rights reserved.
    5:  * 
    6:  * Subject to the following obligations and disclaimer of warranty,
    7:  * use and redistribution of this software, in source or object code
    8:  * forms, with or without modifications are expressly permitted by
    9:  * Packet Design; provided, however, that:
   10:  * 
   11:  *    (i)  Any and all reproductions of the source or object code
   12:  *         must include the copyright notice above and the following
   13:  *         disclaimer of warranties; and
   14:  *    (ii) No rights are granted, in any manner or form, to use
   15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
   16:  *         on advertising, endorsements, or otherwise except as such
   17:  *         appears in the above copyright notice or in the software.
   18:  * 
   19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
   20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
   21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
   22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
   23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
   24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
   25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
   26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
   27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
   28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
   29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
   30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
   31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
   32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
   33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
   35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
   36:  * THE POSSIBILITY OF SUCH DAMAGE.
   37:  *
   38:  * Author: Archie Cobbs <archie@freebsd.org>
   39:  */
   40: 
   41: #include <sys/types.h>
   42: 
   43: #include <stdio.h>
   44: #include <stdlib.h>
   45: #include <stdarg.h>
   46: #include <string.h>
   47: 
   48: #include "structs/structs.h"
   49: #include "structs/type/array.h"
   50: 
   51: #include "util/typed_mem.h"
   52: #include "io/count_fp.h"
   53: 
   54: #define MEM_TYPE		"count_fp"
   55: 
   56: /* Our state */
   57: struct count_info {
   58: 	FILE		*fp;
   59: 	off_t		remain;
   60: 	int		closeit;
   61: };
   62: 
   63: /*
   64:  * Internal functions
   65:  */
   66: static int	count_read(void *cookie, char *buf, int len);
   67: static int	count_close(void *cookie);
   68: 
   69: /*
   70:  * Create a FILE * that reads a limited amount from another FILE *.
   71:  */
   72: FILE *
   73: count_fopen(FILE *fp, off_t count, int closeit)
   74: {
   75: 	struct count_info *c;
   76: 
   77: 	/* Create info */
   78: 	if ((c = MALLOC(MEM_TYPE, sizeof(*c))) == NULL)
   79: 		return (NULL);
   80: 	memset(c, 0, sizeof(*c));
   81: 	c->fp = fp;
   82: 	c->remain = count;
   83: 	c->closeit = closeit;
   84: 
   85: 	/* Create new FILE * */
   86: 	if ((fp = funopen(c, count_read, NULL, NULL, count_close)) == NULL) {
   87: 		FREE(MEM_TYPE, c);
   88: 		return (NULL);
   89: 	}
   90: 
   91: 	/* Set to non-buffered so we don't read to much from underlying fp */
   92: 	setbuf(fp, NULL);
   93: 
   94: 	/* Done */
   95: 	return (fp);
   96: }
   97: 
   98: /*
   99:  * Read from a count_fp.
  100:  */
  101: static int
  102: count_read(void *cookie, char *buf, int len)
  103: {
  104: 	struct count_info *const c = cookie;
  105: 	int ret;
  106: 
  107: 	if (c->remain == 0)
  108: 		return (0);
  109: 	if (len > c->remain)
  110: 		len = c->remain;
  111: 	if ((ret = fread(buf, 1, len, c->fp)) != len) {
  112: 		if (ferror(c->fp))
  113: 			return (-1);
  114: 	}
  115: 	c->remain -= ret;
  116: 	return (ret);
  117: }
  118: 
  119: /*
  120:  * Close a count_fp.
  121:  */
  122: static int
  123: count_close(void *cookie)
  124: {
  125: 	struct count_info *const c = cookie;
  126: 
  127: 	if (c->closeit)
  128: 		fclose(c->fp);
  129: 	FREE(MEM_TYPE, c);
  130: 	return (0);
  131: }
  132: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>