Path: blob/main/contrib/libarchive/libarchive_fe/line_reader.c
105377 views
/*-1* Copyright (c) 2008 Tim Kientzle2* Copyright (c) 2010 Joerg Sonnenberger3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer10* in this position and unchanged.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#include "lafe_platform.h"28#include <errno.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>3233#include "lafe_err.h"34#include "line_reader.h"3536#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__BORLANDC__)37#define strdup _strdup38#endif3940/*41* Read lines from file and do something with each one. If option_null42* is set, lines are terminated with zero bytes; otherwise, they're43* terminated with newlines.44*45* This uses a self-sizing buffer to handle arbitrarily-long lines.46*/47struct lafe_line_reader {48FILE *f;49char *buff, *buff_end, *line_start, *line_end;50char *pathname;51size_t buff_length;52int nullSeparator; /* Lines separated by null, not CR/CRLF/etc. */53};5455struct lafe_line_reader *56lafe_line_reader(const char *pathname, int nullSeparator)57{58struct lafe_line_reader *lr;5960lr = calloc(1, sizeof(*lr));61if (lr == NULL)62lafe_errc(1, ENOMEM, "Can't open %s", pathname);6364lr->nullSeparator = nullSeparator;65lr->pathname = strdup(pathname);66if (lr->pathname == NULL)67lafe_errc(1, ENOMEM, "Can't open %s", pathname);6869if (strcmp(pathname, "-") == 0)70lr->f = stdin;71else72lr->f = fopen(pathname, "r");73if (lr->f == NULL)74lafe_errc(1, errno, "Couldn't open %s", pathname);75lr->buff_length = 8192;76lr->line_start = lr->line_end = lr->buff_end = lr->buff = NULL;7778return (lr);79}8081static void82lafe_line_reader_find_eol(struct lafe_line_reader *lr)83{8485lr->line_end += strcspn(lr->line_end,86lr->nullSeparator ? "" : "\x0d\x0a");87*lr->line_end = '\0'; /* Noop if line_end == buff_end */88}8990const char *91lafe_line_reader_next(struct lafe_line_reader *lr)92{93size_t bytes_wanted, bytes_read, new_buff_size;94char *line_start, *p;9596for (;;) {97/* If there's a line in the buffer, return it immediately. */98while (lr->line_end < lr->buff_end) {99line_start = lr->line_start;100lr->line_start = ++lr->line_end;101lafe_line_reader_find_eol(lr);102103if (lr->nullSeparator || line_start[0] != '\0')104return (line_start);105}106107/* If we're at end-of-file, process the final data. */108if (lr->f == NULL) {109if (lr->line_start == lr->buff_end)110return (NULL); /* No more text */111line_start = lr->line_start;112lr->line_start = lr->buff_end;113return (line_start);114}115116/* Buffer only has part of a line. */117if (lr->line_start > lr->buff) {118/* Move a leftover fractional line to the beginning. */119memmove(lr->buff, lr->line_start,120lr->buff_end - lr->line_start);121lr->buff_end -= lr->line_start - lr->buff;122lr->line_end -= lr->line_start - lr->buff;123lr->line_start = lr->buff;124} else {125/* Line is too big; enlarge the buffer. */126new_buff_size = lr->buff_length * 2;127if (new_buff_size <= lr->buff_length)128lafe_errc(1, ENOMEM,129"Line too long in %s", lr->pathname);130lr->buff_length = new_buff_size;131/*132* Allocate one extra byte to allow terminating133* the buffer.134*/135p = realloc(lr->buff, new_buff_size + 1);136if (p == NULL)137lafe_errc(1, ENOMEM,138"Line too long in %s", lr->pathname);139lr->buff_end = p + (lr->buff_end - lr->buff);140lr->line_end = p + (lr->line_end - lr->buff);141lr->line_start = lr->buff = p;142}143144/* Get some more data into the buffer. */145bytes_wanted = lr->buff + lr->buff_length - lr->buff_end;146bytes_read = fread(lr->buff_end, 1, bytes_wanted, lr->f);147lr->buff_end += bytes_read;148*lr->buff_end = '\0'; /* Always terminate buffer */149lafe_line_reader_find_eol(lr);150151if (ferror(lr->f))152lafe_errc(1, errno, "Can't read %s", lr->pathname);153if (feof(lr->f)) {154if (lr->f != stdin)155fclose(lr->f);156lr->f = NULL;157}158}159}160161void162lafe_line_reader_free(struct lafe_line_reader *lr)163{164free(lr->buff);165free(lr->pathname);166free(lr);167}168169170