Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Weirdness in comm(1)



On Fri, Nov 27, 2009 at 03:58:20PM -0500, D'Arcy J.M. Cain wrote:
> I guess I'm not as current as I should be.  I tracked it down and read
> the underlying code.  It certainly makes more sense although my way can
> be backported to supported versions.  Why don't I commit what I have,
> request pullups and then work on a new version for -current?

I think it is preferable to have the getline version in current and
just provide a compat version in netbsd-5 :) I'm using the attached code
for the wrapper rewrite in pkgsrc.

Joerg
#include <stdio.h>
#include <stdlib.h>

#include "common.h"

ssize_t
getline(char **lineptr, size_t *len, FILE *fp)
{
        char *iter, *eos;
        int ch;

        if (*len == 1) {
                free(*lineptr);
                *len = 0;
        }
        if (*len == 0) {
                *lineptr = malloc(128);
                if (*lineptr == NULL)
                        return -1;
                *len = 128;             
        }

        iter = *lineptr;
        for (;;) {
                eos = *lineptr + *len - 1;

                while (iter < eos) {
                        ch = getc_unlocked(fp);
                        if (ch == -1)
                                break;
                        *iter++ = ch;
                        if (ch == '\n') {
                                *iter = '\0';
                                return iter - *lineptr;
                        }
                }
                if (iter == *lineptr)
                        return -1;
                if (iter < eos) {
                        *iter = '\0';
                        return iter - *lineptr;
                }

                iter = realloc(*lineptr, *len * 2);
                if (iter == NULL)
                        return -1;
                *lineptr = iter;
                iter += *len - 1;
                *len *= 2;
        }
}


Home | Main Index | Thread Index | Old Index