2

Update:
OSX El Capitan, Xcode 7.3

Input:

0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890001

Here is my code:

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

#define BUFFERSIZE 2048

int 
main()
{
    char buffer[BUFFERSIZE];
    printf("Enter a message: \n");
    while (fgets( buffer, BUFFERSIZE, stdin) != NULL)
    {
        printf("%s\n", buffer);
    }

    return 0;
}

compile and run it in terminal.

./test

then input one line characters and length is 1024. It doesn't work; it cannot print the buffer. When I input 1023 characters, it will print the 1023 characters. It can print more than 1024 characters which fgets reads from a local open file.

So, with standard input from a terminal, the max length is 1024, even though <syslimits.h> shows ARG_MAX is (256 * 1024).

What is wrong with my code?

I have some references:

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
beautifularea
  • 47
  • 2
  • 9
  • What operating system are you using, and exactly how are you providing the input? – jamesdlin Sep 07 '16 at 03:50
  • This code is strange. Are you sure you're calling fgets not gets? Your extra \n on the printf suggests you are calling gets. – Joshua Sep 07 '16 at 03:54
  • There is no need for `BUFFERSIZE-1` use `BUFFERSIZE` with `fgets`. Also what OS are you running on? – David C. Rankin Sep 07 '16 at 04:14
  • There is no limit on `stdin` on Linux and I doubt there is one on OSX (though I don't use OSX, so I cannot confirm). I have redirected 800K+ files to `stdin` for reading by `fgets` without issue. Your code should read up to `2048` (chars, that's `2047 + nul-byte` as you have it correctly edited). Changing `printf("%s(%zu chars [including '\\n'])\n", buffer, strlen (buffer));` and using your input, I get the full output along with `(1025 chars [including '\n'])` – David C. Rankin Sep 07 '16 at 04:28
  • Note that if it was a file that was redirected as standard input, Mac OS X has no specific limit on the length of a line. It is the terminal driver that has the limit. – Jonathan Leffler Sep 07 '16 at 04:47

1 Answers1

3

There's nothing wrong with the code per se. The problem is the terminal driver which has a limit of 1 KiB in its buffer, so you can't input more than the 1023 characters plus a newline. Most systems have a similar limit. Historically, the limit was a lot smaller, like 256 bytes.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278