I’m using popen to read output from shell commands. I will use fgets to read line by line.
My question is how to choose the best buffer size for my char* buffer? I remember from a professor telling us to include <limits.h> and use LINE_MAX for such things. It works fine on my Mac, but there’s no LINE_MAX on Linux.
This mailing list archive poses the same question, but no answer to my question
http://bytes.com/topic/c/answers/843278-not-able-locate-line_max-limits-h
When
<limits.h>does not defineLINE_MAX, look at_POSIX2_LINE_MAX, which is required to be at least 2048. I usually use 4096.Also look for the (new) POSIX functions
getline()andgetdelim()– both at the same URL. These allocate memory as necessary.Program (
posix2_line_max.c)Output:
posixver.hTested on an Ubuntu 12.04 derivative; command line: