ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im trying to read stdin with read and it is a success. I found I could use ioctl to flush stdin (STDIN_FILENO) to an even greater surprise.
Big question: can I determine the file size/length of stdin?
Ive tried lstat w ttyname even, but returns 0 bytes though the termios buffer is not depleted yet. Tried feof too for comparisons, but itll stay in an infinite loop. Using lseek is also bad.
Not at all. The length of remaining characters unread. TIOCGWINSZ would only return the rows and columns. And I already understand how to wait for a read signal.
It has an unread filled buffer of oodles of bytes somewhere. I simply want to allocate a proper string to receive precise data.
I don't think that would be either possible or useful. Just read diligently until you get an EWOULDBLOCK. (Or use select/poll/SIGIO).
The size of the buffer can be dynamically increased, details are described here: http://www.joelonsoftware.com/articl...000000319.html
I don't think you can easily, if not at all. You can either read what's in stdin now, or not. You can limit your read in byte count, but that doesn't respond by telling you how much is in there in the first place.
When I say "easily" I don't know but I bet if you browse down the /proc file system for a given terminal or other process, you may be able to discern the count of bytes in stdin, but you know ... you've then opened and viewed yet another file when all you needed to do was to read stdin.
Note also that you can use select() to determine if there is anything in that file descriptor in the first place if your intentions are to determine if there is any data.
Thanks and very good replies. This is mostly what i want to confirm. However the kernel stores the data it is probably a static buffer somewhere. Yet another flaw you encounter with strict text data systems. Strict meaning "protected".
Simple. I need human text data from the proc folders versus the old way of asm interrupt calls to fetch fast binary data. I find this inefficient but I will suffice with free.
Thanks and very good replies. This is mostly what i want to confirm. However the kernel stores the data it is probably a static buffer somewhere. Yet another flaw you encounter with strict text data systems. Strict meaning "protected".
If by data you mean the pipe buffer that feeds libc's stdin buffer - it's a circular buffer. It's not a static buffer.
You can mess with setvbuf within a program (it doesn't affect the buffer libc uses for stdin in other programs) and doing so gives some interesting results.
I don't know if this says anything about libc's stdin buffer - but I find two things notable.
First there is nothing in the buffer until fgetc is called. Second, string size in the buffer doesn't change as it's read which means there is an internal pointer being kept. This makes sense when you consider a function such as ungetc.
Simple. I need human text data from the proc folders versus the old way of asm interrupt calls to fetch fast binary data. I find this inefficient but I will suffice with free.
Umm.... efficiency isn't the issue. None of the data from /proc can be synchronized, thus it is irrelevant about any "efficiency" involved. What counted was standardization of access to the data. Using open/close/read/write eliminates all the unique ioctl function parameters that do the same thing. This eliminated a lot of code (the ioctls) in favor of other already existing data retrieval (file search, open/close/read/write).
Not really. The amount can change even between ioctl(FIONREAD) and read/recv (remember: time-sharing, interrupts, asynchron events and stuff like those). Also it can change during read/recv.
Normally you use some message delimiter (eg CR and/or LF as end-of-line), or a length-field (eg HTTP:Content-Length).
Not really. The amount can change even between ioctl(FIONREAD) and read/recv (remember: time-sharing, interrupts, asynchron events and stuff like those). Also it can change during read/recv.
Actually, the only thing that can happen is that it gets larger.
Quote:
Normally you use some message delimiter (eg CR and/or LF as end-of-line), or a length-field (eg HTTP:Content-Length).
FIONREAD IS the length. You cannot trust "length-field" within the packet, nor can you trust delimiters... that is an old security failure as the length within the packet can be invalid, and delimiters could be missing. You can only test for "sanity" by verifying that it is less than the actual length of the message...
The only way to get that is by FIONREAD, or requesting exactly the messege length expected, and expect to block if it is short, or handling EWOULDBLOCK errors.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.