<p>I need a function called <code>SizeOfPipe()</code> which should return the size of a pipe - I only want to know how much data is in the pipe and not actually read data off the pipe itself. </p>

<p>I thought the following code would work:</p>

<pre><code>fseek (pPipe, 0 , SEEK_END);
*pBytes = ftell (pPipe);
rewind (pPipe);
</code></pre>

<p>but <code>fseek()</code> doesn't work on file descriptors. Another option would be to read the pipe then write the data back but would like to avoid this if possible. Any suggestions?</p>

[{'answer_id': 71870, 'author': 'moonshadow', 'author_id': 11834, 'author_profile': 'https://Stackoverflow.com/users/11834', 'pm_score': 2, 'selected': False, 'text': "<p>It's not in general possible to know the amount of data you can read from a pipe just from the pipe handle alone. The data may be coming in across a network, or being dynamically generated by another process. If you need to know up front, you should arrange for the information to be sent to you - through the pipe, or out of band - by whatever process is at the other end of the pipe.</p>\n"}, {'answer_id': 71872, 'author': 'Alexey Feldgendler', 'author_id': 10682, 'author_profile': 'https://Stackoverflow.com/users/10682', 'pm_score': 2, 'selected': False, 'text': '<p>There is no generic, portable way to tell how much data is available in a pipe without reading it. At least not under POSIX specifications.</p>\n\n<p>Pipes are not seekable, and neither is it possible to put the data back into the reading end of a pipe.</p>\n\n<p>Platform-specific tricks might be possible, though. If your question is platform-specific, editing your question to say so might improve your chances to get a working answer.</p>\n'}, {'answer_id': 71873, 'author': 'Ray Hayes', 'author_id': 7093, 'author_profile': 'https://Stackoverflow.com/users/7093', 'pm_score': 0, 'selected': False, 'text': "<p>I don't think it is possible, isn't the point of a pipe to provide interprocess communication between the two ends (in one direction).  If I'm correct in that assertion, the send may not yet have finished pushing data into the pipe -- so it'd be impossible to determine the length.</p>\n\n<p>What platform are you using?</p>\n"}, {'answer_id': 71875, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 2, 'selected': False, 'text': "<p>Unfortunately the system cannot always know the size of a pipe - for example if you are piping a long-running process into another command, the source process may not have finished running yet. In this case there is no possible way (even in theory) to know how much more data is going to come out of it.</p>\n\n<p>If you want to know the amount of data <em>currently</em> available to read out of the pipe that might be possible, but it will depend on OS buffering and other factors which are hard to control. The most common approach here is just to keep reading until there's nothing left to come (if you don't get an EOF then the source process hasn't finished yet). However I don't think this is what you are looking for.</p>\n\n<p>So I'm afraid there is no general solution.</p>\n"}, {'answer_id': 71925, 'author': 'CL.', 'author_id': 11654, 'author_profile': 'https://Stackoverflow.com/users/11654', 'pm_score': 3, 'selected': False, 'text': '<p>Some UNIX implementations return the number of bytes that can be read in the <code>st_size</code> field after calling <code>fstat()</code>, but this is not portable.</p>\n'}, {'answer_id': 71950, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I do not think it's possible. Pipes present stream-oriented protocol rather than packet-oriented one. IOW, if you write to a pipe twice, once with,say, 250 bytes and once with, say, 520 bytes, there is no way to tell how many bytes you'll get from the other end in one read request. You could get 256, 256, and then the rest.</p>\n\n<p>If you need to impose packets on a pipe, you need to do it yourself by writing pre-determined (or delimited) number of bytes as packet length, and then the rest of teh packet. Use <strong>select()</strong> to find out if there is data to read, use read() to get a reasonably-sized buffer. When you have your buffer, it's your responsibility to determine the packet boundary.</p>\n"}, {'answer_id': 71951, 'author': 'João Augusto', 'author_id': 6909, 'author_profile': 'https://Stackoverflow.com/users/6909', 'pm_score': 0, 'selected': False, 'text': "<p>If you want to know the amount of data that it's expected to arrive, you could always write at the begining of every msg sent by the pipes the size of the msg.\nSo write for example 4 bytes at the start of every msg with the length of your data, and then only read the first 4 bytes.</p>\n"}, {'answer_id': 72020, 'author': 'VolkerK', 'author_id': 4833, 'author_profile': 'https://Stackoverflow.com/users/4833', 'pm_score': 4, 'selected': False, 'text': '<p>Depending on your unix implementation ioctl/FIONREAD might do the trick</p>\n\n<pre>\nerr = ioctl(pipedesc, FIONREAD, &bytesAvailable);\n</pre>\n\n<p>Unless this returns the error code for "invalid argument" (or any other error) bytesAvailable contains the amount of data available for unblocking read operations at that time.</p>\n'}, {'answer_id': 74349, 'author': 'apenwarr', 'author_id': 42219, 'author_profile': 'https://Stackoverflow.com/users/42219', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s almost never necessary to know how many bytes are in the pipe: perhaps you just want to do a non-blocking read() on the pipe, ie. to check if there are any bytes ready, and if so, read them, but never stop and <em>wait</em> for the pipe to be ready.</p>\n\n<p>You can do that in two steps.  First, use the select() system call to find out whether data is available or not.  An example is here: <a href="http://www.developerweb.net/forum/showthread.php?t=2933" rel="nofollow noreferrer">http://www.developerweb.net/forum/showthread.php?t=2933</a></p>\n\n<p>Second, if select tells you data is available, call read() once, and only once, with a large block size.  It will read <em>only</em> as many bytes are available, or up to the size of your block, whichever is smaller.  If select() returns true, read() will always return right away.</p>\n'}, {'answer_id': 74757, 'author': 'INS', 'author_id': 13136, 'author_profile': 'https://Stackoverflow.com/users/13136', 'pm_score': 0, 'selected': False, 'text': '<p>There is no portable way to tell the amount of data coming from a pipe.\nThe only thing you could do is to read and process data as it comes.</p>\n\n<p>For that you could use something like a <a href="http://en.wikipedia.org/wiki/Circular_buffer" rel="nofollow noreferrer">circular buffer</a></p>\n'}, {'answer_id': 77381, 'author': 'MSN', 'author_id': 6210, 'author_profile': 'https://Stackoverflow.com/users/6210', 'pm_score': -1, 'selected': False, 'text': "<p>On Windows you can always use <code>PeekNamedPipe</code>, but I doubt that's what you want to do anyway.</p>\n"}, {'answer_id': 731364, 'author': 'devdimi', 'author_id': 54983, 'author_profile': 'https://Stackoverflow.com/users/54983', 'pm_score': 0, 'selected': False, 'text': '<p>You can wrap it in object with buffering that can be rewinded. This would be feasible only for small amounts of data.</p>\n\n<p>One way to do this in C is to define stuct and wrap all functions operating on pipes for your struct.</p>\n'}, {'answer_id': 63974916, 'author': 'Thomas Guyot-Sionnest', 'author_id': 969196, 'author_profile': 'https://Stackoverflow.com/users/969196', 'pm_score': 0, 'selected': False, 'text': '<p>As many have answered, you cannot portably tell how many bytes there is to read, OTOH what you can do is poll the pipe for data to be read. First be sure to open the pipe with <code>O_RDWR|O_NONBLOCK</code> - it\'s mandated by POSIX that a pipe be open for both read and write to be able poll it.</p>\n<p>Whenever you want to know if there is data available, just select/poll for data to read. You can also know if the pipe is full by checking for write but see the note below, depending on the type or write it may be inaccurate.</p>\n<p>You won\'t know how much data there is but keep in mind writes up to <code>PIPE_BUF</code> bytes are guaranteed to be atomic, so if you\'re concerned about having a full message on the pipe, just make sure they fit within that or split them up.</p>\n<p><strong>Note:</strong> When you select for write, even if poll/select says you can write to the pipe a write <code>&lt;= PIPE_BUF</code> will return <code>EAGAIN</code> if there isn\'t enough room for the full write. I have no ideas how to tell if there is enough room to write... that is what I was looking for (I may end padding with <code>\\0</code>\'s to <code>PIPE_BUF</code> size... in my case it\'s just for testing anyway).</p>\n<p>I have an old example app Perl that can read one or more pipes in non-blocking mode, <a href="https://github.com/dermoth/misc-code/blob/master/nagios/OCP_Daemon/OCP_daemon" rel="nofollow noreferrer">OCP_Daemon</a>. The code is pretty close to what you would do in C using an event loop.</p>\n'}]
['https://Stackoverflow.com/questions/71820', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']