Are there alternate implementations of GNU getline interface?

| | August 5, 2015

The experiment I am currently working uses a software base with a complicated source history and no well defined license. It would be a considerable amount of work to rationalize things and release under a fixed license.

It is also intended to run a a random unixish platform, and only some of the libc’s we support have GNU getline, but right now the code expects it.

Does anyone know of a re-implementation of the GNU getline semantics that is available under a less restrictive license?

Edit:: I ask because Google didn’t help, and I’d like to avoid writing one if possible (it might be a fun exercise, but it can’t be the best use of my time.)

To be more specific, the interface in question is:

ssize_t getline (char **lineptr, size_t *n, FILE *stream);

4 Responses to “Are there alternate implementations of GNU getline interface?”

  1. Michael Andrews on October 24, 2013 @ 12:02 AM

    If you are compiling for BSD use fgetln
    instead

  2. I’m puzzled.

    I looked at the link, read the description, and this is a fine utility.

    But, are you saying you simply can’t rewrite this function to spec? The spec seems quite clear,

    Here:

    /* This code is public domain -- Will Hartung 4/9/09 */
    #include <stdio.h>
    #include <stdlib.h>
    
    size_t getline(char **lineptr, size_t *n, FILE *stream) {
        char *bufptr = NULL;
        char *p = bufptr;
        size_t size;
        int c;
    
        if (lineptr == NULL) {
        	return -1;
        }
        if (stream == NULL) {
        	return -1;
        }
        if (n == NULL) {
        	return -1;
        }
        bufptr = *lineptr;
        size = *n;
    
        c = fgetc(stream);
        if (c == EOF) {
        	return -1;
        }
        if (bufptr == NULL) {
        	bufptr = malloc(128);
        	if (bufptr == NULL) {
        		return -1;
        	}
        	size = 128;
        }
        p = bufptr;
        while(c != EOF) {
        	if ((p - bufptr) > (size - 1)) {
        		size = size + 128;
        		bufptr = realloc(bufptr, size);
        		if (bufptr == NULL) {
        			return -1;
        		}
        	}
        	*p++ = c;
        	if (c == 'n') {
        		break;
        	}
        	c = fgetc(stream);
        }
    
        *p++ = '';
        *lineptr = bufptr;
        *n = size;
    
        return p - bufptr - 1;
    }
    
    int main(int argc, char** args) {
        char *buf = NULL; /*malloc(10);*/
        int bufSize = 0; /*10;*/
    
        printf("%dn", bufSize);
        int charsRead =  getline(&buf, &bufSize, stdin);
    
        printf("'%s'", buf);
        printf("%dn", bufSize);
        return 0;
    }
    

    15 minutes, and I haven’t written C in 10 years. It minorly breaks the getline contract in that it only checks if the lineptr is NULL, rather than NULL and n == 0. You can fix that if you like. (The other case didn’t make a whole lot of sense to me, I guess you could return -1 in that case.)

    Replace the ‘n’ with a variable to implement “getdelim”.

    Do people still write code any more?

  3. In case you are talking about readline, check out: editline

  4. Take a look at Paul Hsieh’s page on User Input. You can email the author if you want to know the exact terms.

Leave a Reply