Home > front end >  How can I find the dimension of the standard input?
How can I find the dimension of the standard input?

Time:10-05

I have a problem finding out how big is the dimension of the stdin through a pipe. I know that a lot of you will be furious at this question, but just hear me out.

Half of it already works:

$ echo "BYE" | ./my_prog

In the linux shell outputs 4 which is exactly what I want.

The problem comes out when I try to feed it some bytes, in fact the first time works while after it doesn't work anymore.

$ ./create_bytes.py -n 200 | ./my_prog
$ 200
$ ./create_bytes.py -n 200 | ./my_prog
$ 0

and I can't understand why. I'm sure the stream is always the same length.

The code I'm using is the following

int main (int argc, char *argv[]) {
    struct stat fd_s;
    if (fstat(STDIN_FILENO, &fd_s) == -1) {
        perror("fstat(fdin)");
        exit(EXIT_FAILURE);
    }
    printf("%lld\n", fdin_stat.st_size);
    ...
}

Thanks in advance

EDIT: This is the actual request: Read a stream of lines (bytes sequence that terminates with \n) from stdin in 16 bytes blocks. Every line can't be bigger than 128 bytes.

Maybe I'm just making it more difficult than it should be? I hope it can help Thanks

CodePudding user response:

If the input is a pipe, it doesn't have a size. It's a stream that in principle can go on forever. The fact that the first time you ran it it gave you a number is not something you can rely on.

If you want to read everything from stdin into memory, you need to read data in a loop, and have a buffer that you realloc() when it is full and there is still more data to be read.

If you need to read in a text file and are going to process it line by line, you can consider using the POSIX function getline(), or you might even read a whole file with getdelim() if you are sure it doesn't contain a given delimiter.

CodePudding user response:

You've run into an ill-defined corner case. POSIX specifies that fstat returns the struct stat info of a file associated with a file descriptor. But what happens when the file descriptor does not correspond to a file it not really defined. You might expect the stat call to return an error (and I'm sure there are some systems that do so), but on most systems it returns some information about the object the file descriptor refers to. What info depends on the OS and the type of the object.

On Linux with a pipe (the case you seem to be using) it will always return st_size = 0 (which implies you are using something other than Linux). I would imagine there are systems that return with st_size set to the amount of data buffered in the pipe, as that seems a useful piece of information. Your results seem consistent with that.

  • Related