Path: blob/main/contrib/libarchive/libarchive_fe/line_reader.c
39481 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 "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);6667if (strcmp(pathname, "-") == 0)68lr->f = stdin;69else70lr->f = fopen(pathname, "r");71if (lr->f == NULL)72lafe_errc(1, errno, "Couldn't open %s", pathname);73lr->buff_length = 8192;74lr->line_start = lr->line_end = lr->buff_end = lr->buff = NULL;7576return (lr);77}7879static void80lafe_line_reader_find_eol(struct lafe_line_reader *lr)81{8283lr->line_end += strcspn(lr->line_end,84lr->nullSeparator ? "" : "\x0d\x0a");85*lr->line_end = '\0'; /* Noop if line_end == buff_end */86}8788const char *89lafe_line_reader_next(struct lafe_line_reader *lr)90{91size_t bytes_wanted, bytes_read, new_buff_size;92char *line_start, *p;9394for (;;) {95/* If there's a line in the buffer, return it immediately. */96while (lr->line_end < lr->buff_end) {97line_start = lr->line_start;98lr->line_start = ++lr->line_end;99lafe_line_reader_find_eol(lr);100101if (lr->nullSeparator || line_start[0] != '\0')102return (line_start);103}104105/* If we're at end-of-file, process the final data. */106if (lr->f == NULL) {107if (lr->line_start == lr->buff_end)108return (NULL); /* No more text */109line_start = lr->line_start;110lr->line_start = lr->buff_end;111return (line_start);112}113114/* Buffer only has part of a line. */115if (lr->line_start > lr->buff) {116/* Move a leftover fractional line to the beginning. */117memmove(lr->buff, lr->line_start,118lr->buff_end - lr->line_start);119lr->buff_end -= lr->line_start - lr->buff;120lr->line_end -= lr->line_start - lr->buff;121lr->line_start = lr->buff;122} else {123/* Line is too big; enlarge the buffer. */124new_buff_size = lr->buff_length * 2;125if (new_buff_size <= lr->buff_length)126lafe_errc(1, ENOMEM,127"Line too long in %s", lr->pathname);128lr->buff_length = new_buff_size;129/*130* Allocate one extra byte to allow terminating131* the buffer.132*/133p = realloc(lr->buff, new_buff_size + 1);134if (p == NULL)135lafe_errc(1, ENOMEM,136"Line too long in %s", lr->pathname);137lr->buff_end = p + (lr->buff_end - lr->buff);138lr->line_end = p + (lr->line_end - lr->buff);139lr->line_start = lr->buff = p;140}141142/* Get some more data into the buffer. */143bytes_wanted = lr->buff + lr->buff_length - lr->buff_end;144bytes_read = fread(lr->buff_end, 1, bytes_wanted, lr->f);145lr->buff_end += bytes_read;146*lr->buff_end = '\0'; /* Always terminate buffer */147lafe_line_reader_find_eol(lr);148149if (ferror(lr->f))150lafe_errc(1, errno, "Can't read %s", lr->pathname);151if (feof(lr->f)) {152if (lr->f != stdin)153fclose(lr->f);154lr->f = NULL;155}156}157}158159void160lafe_line_reader_free(struct lafe_line_reader *lr)161{162free(lr->buff);163free(lr->pathname);164free(lr);165}166167168