Annotation of embedaddon/sudo/compat/getline.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2009-2010 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  */
        !            16: 
        !            17: #include <config.h>
        !            18: 
        !            19: #include <sys/types.h>
        !            20: 
        !            21: #include <stdio.h>
        !            22: #ifdef STDC_HEADERS
        !            23: # include <stdlib.h>
        !            24: # include <stddef.h>
        !            25: #else
        !            26: # ifdef HAVE_STDLIB_H
        !            27: #  include <stdlib.h>
        !            28: # endif
        !            29: #endif /* STDC_HEADERS */
        !            30: #ifdef HAVE_STRING_H
        !            31: # include <string.h>
        !            32: #endif /* HAVE_STRING_H */
        !            33: #ifdef HAVE_STRINGS_H
        !            34: # include <strings.h>
        !            35: #endif /* HAVE_STRINGS_H */
        !            36: #include <limits.h>
        !            37: 
        !            38: #include "missing.h"
        !            39: 
        !            40: #ifndef LINE_MAX
        !            41: # define LINE_MAX 2048
        !            42: #endif
        !            43: 
        !            44: #ifdef HAVE_FGETLN
        !            45: ssize_t
        !            46: getline(char **bufp, size_t *bufsizep, FILE *fp)
        !            47: {
        !            48:     char *buf, *cp;
        !            49:     size_t bufsize;
        !            50:     size_t len;
        !            51: 
        !            52:     buf = fgetln(fp, &len);
        !            53:     if (buf) {
        !            54:        bufsize = *bufp ? *bufsizep : 0;
        !            55:        if (bufsize < len + 1) {
        !            56:            bufsize = len + 1;
        !            57:            cp = *bufp ? realloc(*bufp, bufsize) : malloc(bufsize);
        !            58:            if (cp == NULL)
        !            59:                return -1;
        !            60:            *bufp = cp;
        !            61:            *bufsizep = bufsize;
        !            62:        }
        !            63:        memcpy(*bufp, buf, len);
        !            64:        (*bufp)[len] = '\0';
        !            65:     }
        !            66:     return buf ? len : -1;
        !            67: }
        !            68: #else
        !            69: ssize_t
        !            70: getline(char **bufp, size_t *bufsizep, FILE *fp)
        !            71: {
        !            72:     char *buf, *cp;
        !            73:     size_t bufsize;
        !            74:     ssize_t len = 0;
        !            75: 
        !            76:     buf = *bufp;
        !            77:     bufsize = *bufsizep;
        !            78:     if (buf == NULL || bufsize == 0) {
        !            79:        bufsize = LINE_MAX;
        !            80:        cp = buf ? realloc(buf, bufsize) : malloc(bufsize);
        !            81:        if (cp == NULL)
        !            82:            return -1;
        !            83:        buf = cp;
        !            84:     }
        !            85: 
        !            86:     for (;;) {
        !            87:        if (fgets(buf + len, bufsize - len, fp) == NULL) {
        !            88:            len = -1;
        !            89:            break;
        !            90:        }
        !            91:        len = strlen(buf);
        !            92:        if (!len || buf[len - 1] == '\n' || feof(fp))
        !            93:            break;
        !            94:        bufsize *= 2;
        !            95:        cp = realloc(buf, bufsize);
        !            96:        if (cp == NULL)
        !            97:            return -1;
        !            98:        buf = cp;
        !            99:     }
        !           100:     *bufp = buf;
        !           101:     *bufsizep = bufsize;
        !           102:     return len;
        !           103: }
        !           104: #endif

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