Re: multiple read() calls



"subramanian100in@xxxxxxxxx, India" <subramanian100in@xxxxxxxxx> writes:
I am using Redhat Enterprise Linux kernel 2.6.9 with gcc 3.4.3

Consider the following program.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char** argv)
{
char arr[5] = { 0 };

ssize_t no_of_chars_read;

errno = 0;

if ((no_of_chars_read =
read(STDIN_FILENO, arr, sizeof(arr) - 1)) < 0)
{
printf("read() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

errno = 0;

if (write(STDOUT_FILENO, arr, no_of_chars_read) !=
no_of_chars_read)
{
printf("write() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

errno = 0;

if ((no_of_chars_read =
read(STDIN_FILENO, arr, sizeof(arr) - 1)) < 0)
{
printf("second read() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

errno = 0;

if (write(STDOUT_FILENO, arr, no_of_chars_read) !=
no_of_chars_read)
{
printf("write() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;

}

When I run this program as
[subbu@localhost doubts]$ ./a.out
1234567890abcdefghijklmnopqrstuvwxyz

the following output is produced:

12345678[subbu@localhost doubts]$ 90abcdefghijklmnopqrstuvwxyz
bash: 90abcdefghijklmnopqrstuvwxyz: command not found
[subbu@localhost doubts]$

My questions:
1)The second read() call does not wait for input. How does the second
read() call get input? Shouldn't the characters remaining after the
first read() call be ignored(looks like the characters remaining after
the first read() call are available in some buffer which I am unable
to understand) ?

Sure the characters are buffered. What don't you understand about it?

2)How does "90abcdefghijklmnopqrstuvwxyz" get printed at the command
prompt?

Your program only reads first 8 bytes of entire input, and the rest are
then read by the shell after your program returns. Shell then tries to
interpret these bytes as command and prints error message as there is no
such command.

-- Sergei.
.



Relevant Pages

  • Re: Cant compile this code:
    ... command interpreter shis available, and zero if it is not. ... The systemfunction returns the exit status of the shell as returned ... errno never yields a result other than zero. ... Generally speaking, if the command given to 'system' fails to execute, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Cant compile this code:
    ... | command interpreter shis available, and zero if it is not. ... A return value of 127 means the execution of the shell ... | execute the command passed to it. ... errno never yields a result other than zero. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Code fails with Segmentation Fault
    ... they way you ensure this constraint is to not read more characters ... than the size of your buffer, which you are responsible for keeping ... My file reading idiom is to use fread[or readbecause what I ... return errno; ...
    (comp.lang.c)
  • Re: How to sscanf return integer only
    ... It needs to fail if there are any characters other than integers or white ... contains any invalid characters, an error message is displayed and the ... if (errno == ERANGE) { ... string that are in the "allow" string, this must be the same as the ...
    (comp.lang.c)
  • Re: Way to unblock sys.stdin.readline() call
    ... Is there any possible way to unblock the sys.stdin.readlinecall ... something to put characters in the stdin... ... and catch IOErrors with errno = EAGAIN. ...
    (comp.lang.python)