Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/libbb/fgets_str.c

    r1765 r2725  
    66 * If you wrote this, please acknowledge your work.
    77 *
    8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    99 */
    1010
    1111#include "libbb.h"
    1212
    13 /* Read up to (and including) TERMINATING_STRING from FILE and return it.
    14  * Return NULL on EOF.  */
    15 
    16 char *xmalloc_fgets_str(FILE *file, const char *terminating_string)
     13static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off, size_t *maxsz_p)
    1714{
    1815    char *linebuf = NULL;
     
    2219    int idx = 0;
    2320    int ch;
     21    size_t maxsz = *maxsz_p;
    2422
    2523    while (1) {
    2624        ch = fgetc(file);
    2725        if (ch == EOF) {
    28             free(linebuf);
    29             return NULL;
     26            if (idx == 0)
     27                return linebuf; /* NULL */
     28            break;
    3029        }
    3130
    32         /* grow the line buffer as necessary */
    33         while (idx > linebufsz - 2) {
     31        if (idx >= linebufsz) {
    3432            linebufsz += 200;
    3533            linebuf = xrealloc(linebuf, linebufsz);
     34            if (idx >= maxsz) {
     35                linebuf[idx] = ch;
     36                idx++;
     37                break;
     38            }
    3639        }
    3740
     
    4144        /* Check for terminating string */
    4245        end_string_offset = idx - term_length;
    43         if (end_string_offset > 0
     46        if (end_string_offset >= 0
    4447         && memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0
    4548        ) {
    46             idx -= term_length;
     49            if (chop_off)
     50                idx -= term_length;
    4751            break;
    4852        }
    4953    }
     54    /* Grow/shrink *first*, then store NUL */
    5055    linebuf = xrealloc(linebuf, idx + 1);
    5156    linebuf[idx] = '\0';
     57    *maxsz_p = idx;
    5258    return linebuf;
    5359}
     60
     61/* Read up to TERMINATING_STRING from FILE and return it,
     62 * including terminating string.
     63 * Non-terminated string can be returned if EOF is reached.
     64 * Return NULL if EOF is reached immediately.  */
     65char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string)
     66{
     67    size_t maxsz = INT_MAX - 4095;
     68    return xmalloc_fgets_internal(file, terminating_string, 0, &maxsz);
     69}
     70
     71char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p)
     72{
     73    size_t maxsz;
     74
     75    if (!maxsz_p) {
     76        maxsz = INT_MAX - 4095;
     77        maxsz_p = &maxsz;
     78    }
     79    return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p);
     80}
     81
     82char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string)
     83{
     84    size_t maxsz = INT_MAX - 4095;
     85    return xmalloc_fgets_internal(file, terminating_string, 1, &maxsz);
     86}
Note: See TracChangeset for help on using the changeset viewer.