The getline(3) manpage states: ssize_t getline(char **lineptr, size_t *n, FILE *stream); [...] If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the user program. (The value in *n is ignored.) Based on this description (particularly the part that says the value *n is ignored), you should be able to do something like: char *line = NULL; getline(&line, NULL, stream); However this always fails, because the code for getline does the following: if (!lineptr || !n || !stream) { errno = EINVAL; return -1; } if (!*lineptr) { *n = MIN_CHUNK; *lineptr = malloc (*n); if (!*lineptr) { errno = ENOMEM; return -1; } *lineptr[0] = '\0'; } (thus the !n check always causes -1 to be returned). Probably the manpage should just be modified to say something like "(The value in *n is ignored on entry, but *n must still point to a valid int, and its value is updated on exit to reflect the size of the newly malloc'd buffer.)" This is pretty confusing behavior otherwise -- I had to read the source for getline() to know what was going on. Thanks!
glibc does not come with man pages.
*** This bug has been marked as a duplicate of bug 1481 ***
(In reply to Luke Hutchison from comment #0) > The getline(3) manpage states: > > ssize_t getline(char **lineptr, size_t *n, FILE *stream); > [...] > If *lineptr is NULL, then getline() will allocate a buffer for > storing the line, which should be > freed by the user program. (The value in *n is ignored.) > > Based on this description (particularly the part that says the value *n is > ignored), you should be able to do something like: > > char *line = NULL; > getline(&line, NULL, stream); > > However this always fails, because the code for getline does the following: > > if (!lineptr || !n || !stream) > { > errno = EINVAL; > return -1; > } > > if (!*lineptr) > { > *n = MIN_CHUNK; > *lineptr = malloc (*n); > if (!*lineptr) > { > errno = ENOMEM; > return -1; > } > *lineptr[0] = '\0'; > } > > (thus the !n check always causes -1 to be returned). > > Probably the manpage should just be modified to say something like "(The > value > in *n is ignored on entry, but *n must still point to a valid int, and its > value > is updated on exit to reflect the size of the newly malloc'd buffer.)" > > This is pretty confusing behavior otherwise -- I had to read the source for > getline() to know what was going on. > > Thanks! getline(3) text changed to read: If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line, which should be freed by the user program.