Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/read.c


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/read.c

    r1765 r2725  
    55 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
    66 *
    7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    88 */
    9 
    109#include "libbb.h"
    1110
    12 ssize_t safe_read(int fd, void *buf, size_t count)
     11ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
    1312{
    1413    ssize_t n;
     
    2726 * A short read is returned on an end of file.
    2827 */
    29 ssize_t full_read(int fd, void *buf, size_t len)
     28ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
    3029{
    3130    ssize_t cc;
     
    3736        cc = safe_read(fd, buf, len);
    3837
    39         if (cc < 0)
    40             return cc;  /* read() returns -1 on failure. */
     38        if (cc < 0) {
     39            if (total) {
     40                /* we already have some! */
     41                /* user can do another read to know the error code */
     42                return total;
     43            }
     44            return cc; /* read() returns -1 on failure. */
     45        }
    4146        if (cc == 0)
    4247            break;
     
    4954}
    5055
    51 // Die with an error message if we can't read the entire buffer.
    52 void xread(int fd, void *buf, size_t count)
    53 {
    54     if (count) {
    55         ssize_t size = full_read(fd, buf, count);
    56         if (size != count)
    57             bb_error_msg_and_die("short read");
    58     }
    59 }
    60 
    61 // Die with an error message if we can't read one character.
    62 unsigned char xread_char(int fd)
    63 {
    64     char tmp;
    65     xread(fd, &tmp, 1);
    66     return tmp;
    67 }
    68 
    69 // Read one line a-la fgets. Works only on seekable streams
    70 char *reads(int fd, char *buffer, size_t size)
    71 {
    72     char *p;
    73 
    74     if (size < 2)
    75         return NULL;
    76     size = full_read(fd, buffer, size-1);
    77     if ((ssize_t)size <= 0)
    78         return NULL;
    79 
    80     buffer[size] = '\0';
    81     p = strchr(buffer, '\n');
    82     if (p) {
    83         off_t offset;
    84         *p++ = '\0';
    85         // avoid incorrect (unsigned) widening
    86         offset = (off_t)(p-buffer) - (off_t)size;
    87         // set fd position right after '\n'
    88         if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
    89             return NULL;
    90     }
    91     return buffer;
    92 }
    93 
    94 // Read one line a-la fgets. Reads byte-by-byte.
    95 // Useful when it is important to not read ahead.
    96 char *xmalloc_reads(int fd, char *buf)
    97 {
    98     char *p;
    99     int sz = buf ? strlen(buf) : 0;
    100 
    101     goto jump_in;
    102     while (1) {
    103         if (p - buf == sz) {
    104  jump_in:
    105             buf = xrealloc(buf, sz + 128);
    106             p = buf + sz;
    107             sz += 128;
    108         }
    109         if (safe_read(fd, p, 1) != 1) { /* EOF/error */
    110             if (p == buf) {
    111                 /* we read nothing [and buf was NULL initially] */
    112                 free(buf);
    113                 return NULL;
    114             }
    115             break;
    116         }
    117         if (*p == '\n')
    118             break;
    119         p++;
    120     }
    121     *p++ = '\0';
    122     return xrealloc(buf, p - buf);
    123 }
    124 
    125 ssize_t read_close(int fd, void *buf, size_t size)
     56ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
    12657{
    12758    /*int e;*/
     
    13364}
    13465
    135 ssize_t open_read_close(const char *filename, void *buf, size_t size)
     66ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
    13667{
    13768    int fd = open(filename, O_RDONLY);
     
    14071    return read_close(fd, buf, size);
    14172}
    142 
    143 // Read (potentially big) files in one go. File size is estimated by
    144 // lseek to end.
    145 void *xmalloc_open_read_close(const char *filename, size_t *sizep)
    146 {
    147     char *buf;
    148     size_t size = sizep ? *sizep : INT_MAX;
    149     int fd;
    150     off_t len;
    151 
    152     fd = xopen(filename, O_RDONLY);
    153     /* /proc/N/stat files report len 0 here */
    154     /* In order to make such files readable, we add small const */
    155     len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
    156     xlseek(fd, 0, SEEK_SET);
    157     if (len < size)
    158         size = len;
    159     buf = xmalloc(size + 1);
    160     size = read_close(fd, buf, size);
    161     if ((ssize_t)size < 0)
    162         bb_perror_msg_and_die("'%s'", filename);
    163     xrealloc(buf, size + 1);
    164     buf[size] = '\0';
    165     if (sizep)
    166         *sizep = size;
    167     return buf;
    168 }
Note: See TracChangeset for help on using the changeset viewer.