TwilightSparkleTheGeek
TwilightSparkleTheGeek

Reputation: 223

Where can I find the OS system limit on input lines?

this is more of a design issue question for character buffers. Typically, a lot of programs especially C programs according to C by Discovery 2nd edition deal with a lot of input and output of Strings. On chapter 5, Section 5.3, Page 255 for those that have the book the footnote on that page says:

"The size of a buffer is open to debate. Many programmers will go with an array of 80 cells since few users will type more than that. Others would go with the system limit on input lines."

Where can I find my system limit on input lines? I feel an 80 character buffer is too small a buffer. I need to be able to explain why I choose the size of my buffer in my program project, my professor will want to know a reason.

#include <stdio.h>
#include <stdlib.h>
#define DEL 127

int main(int argc, char *argv[]){

    FILE *fpin = NULL;  //pointer to access open file.
    int iochar = 0; //temporary storage for characters taken from stdin or from a file.
    int edit = 1; //used to indicate an edit corresponding to (1)(2)(3)(4)
    char *inbuffer = malloc(80); //temporary storage to store characters taken from file or stdin
    int offset = 0;//places the character taken from file or stdin in correct location in allocated memory

    if (argc == 1){

        while ((iochar = getchar()) != EOF){
            if(offset > 80){
                printf("Error: Input size to big for this program.\n");
                printf("A line from your file is bigger than 80 characters.\n");
                return 0;
            }

            if((iochar<32 && iochar != 10) || iochar == 127){
                edit = 0;
                if(iochar == DEL){
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = '?';
                }
                else{
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = (iochar+64);
                }
            }
            else if(iochar > 127 && iochar < 80)
                edit = 0;
            else if(iochar > 31 && iochar < 127)
                *(inbuffer + offset++) = iochar;

            if(iochar == '\n'){
                *(inbuffer + offset++) = '$';
                int limit_char;
                if(offset > 72)
                    limit_char = offset-72; //if line is larger than 72 print last 72 characters in inbuffer
                else
                    limit_char = 0; //if line is smaller than 72, print whatever is in allocated memory inbuffer

                /*Printing out the characters in allocated memory*/
                *(inbuffer + offset) = 0;
                printf("%s",(inbuffer+limit_char));
                putchar('\n');
                offset = 0;
                }
            }
        return edit;
        }

    if (argc == 2){
        fpin = fopen(argv[1], "r");

        while ((iochar = getc(fpin)) != EOF){
            if(offset > 80){
                printf("Error: Input size to big for this program.\n");
                printf("A line from your file is bigger than 80 characters.\n");
                return 0;
            }

            if((iochar<32 && iochar != 10) || iochar == 127){
                edit = 0;
                if(iochar == DEL){
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = '?';
                }
                else{
                    *(inbuffer + offset++) = '^';
                    *(inbuffer + offset++) = (iochar+64);
                }
            }
            else if(iochar > DEL && iochar < 80)
                edit = 0;
            else if(iochar > 31 && iochar < DEL)
                *(inbuffer + offset++) = iochar;

            if(iochar == '\n'){
                *(inbuffer + offset++) = '$';
                int limit_char; //used to limit only 72 characters to be printed
                if(offset > 72)
                    limit_char = offset-72; //if line is larger than 72 print last 72 characters
                else
                    limit_char = 0; //if line is smaller than 72

                *(inbuffer + offset) = 0;
                printf("%s",(inbuffer+limit_char));
                putchar('\n');
                offset = 0;
                }
            }
        return edit;
    }
}

Upvotes: 0

Views: 78

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137487

There is <limits.h>, which defines (on UNIX-like platforms):

  • MAX_CANON - Maximum number of bytes in a terminal canonical input line.
  • MAX_INPUT - Minimum number of bytes for which space will be available in a terminal input queue; therefore, the maximum number of bytes a portable application may require to be typed as input before reading them.

  • LINE_MAX - Unless otherwise noted, the maximum length, in bytes, of a utility's input line (either standard input or another file), when the utility is described as processing text files. The length includes room for the trailing .

fgets suggests using LINE_MAX which is 2048 on my Fedora 17 box, and in Cygwin.

See also:

Upvotes: 2

Related Questions