LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2015, 12:03 PM   #1
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
stdin ioctl simply put


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.

ioctl seems it, but for sizing?
 
Old 01-12-2015, 12:18 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> can I determine the file size/length of stdin?

If that were possible, what would you expect to get as a result? The dimensions of the terminal(window)?
 
Old 01-12-2015, 12:28 PM   #3
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 01-12-2015, 12:41 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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
 
Old 01-12-2015, 02:09 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 01-12-2015, 03:04 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Are you looking for the ioctl FIONREAD? (man ioctl_list) which returns the number of bytes that can be read from a descriptor?
 
1 members found this post helpful.
Old 01-15-2015, 11:29 AM   #7
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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".
 
Old 01-15-2015, 11:57 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Could you please explain what flaw you found?
 
Old 01-15-2015, 12:10 PM   #9
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Could you please explain what flaw you found?
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.
 
Old 01-15-2015, 12:41 PM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I guess you are trying to reimplement utility 'ps'...
 
Old 01-15-2015, 12:59 PM   #11
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by multiplex22 View Post
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.

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

#define MAX_BUF 4096

int main () {
        char buf[MAX_BUF];

        if (setvbuf (stdin, buf, _IOLBF, MAX_BUF) != 0) {
                 perror ("setvbuf");
                 exit (EXIT_FAILURE);
        }

        printf ("In buffer - %ld\n", strlen (buf));

        int c;
        int read = 0;

        while ((c = fgetc (stdin)) != EOF) {
                read += printf ("%c", c);
                printf ("- %ld -", strlen (buf) - read);
        }       

        printf ("\n");

        printf ("In buffer %ld\n", strlen (buf));

        return 0;
}
Code:
$ gcc -Wall -std=c11 rebufio.c -o rebufio
$ echo "Hello World" | ./rebufio
In buffer - 0
H- 11 -e- 10 -l- 9 -l- 8 -o- 7 - - 6 -W- 5 -o- 4 -r- 3 -l- 2 -d- 1 -
- 0 -
In buffer 12
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.
 
Old 01-15-2015, 03:25 PM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by multiplex22 View Post
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).
 
Old 01-16-2015, 12:40 PM   #13
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by jpollard View Post
Are you looking for the ioctl FIONREAD? (man ioctl_list) which returns the number of bytes that can be read from a descriptor?
Perfect
 
Old 01-16-2015, 01:43 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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).
 
Old 01-16-2015, 04:23 PM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by NevemTeve View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where to put the include file which defines ioctl numbers? AustinMarton Linux - Kernel 2 08-04-2011 04:30 PM
How to pass IOCTL arguments from usespace ioctl call devkpict Linux - Kernel 1 12-07-2007 06:45 PM
simply put, how do I disable Hyperthreading? v8esprit Linux - Hardware 2 12-03-2003 07:01 PM
simply put, how do I disable Hyperthreading? v8esprit Mandriva 1 12-03-2003 06:59 PM
simply put, how do I disable Hyperthreading? v8esprit Linux - Laptop and Netbook 0 12-03-2003 05:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration