multiple read() calls



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) ?
2)How does "90abcdefghijklmnopqrstuvwxyz" get printed at the command
prompt?

Kindly clarify.

Thanks
V.Subramanian
.



Relevant Pages

  • 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: multiple read() calls
    ... errno = 0; ... Shouldn't the characters remaining after the ... then read by the shell after your program returns. ... interpret these bytes as command and prints error message as there is no ...
    (comp.os.linux.development.apps)
  • 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)
  • multiple read() calls
    ... int main(int argc, char** argv) ... errno = 0; ... Shouldn't the characters remaining after the ... 2)How does "90abcdefghijklmnopqrstuvwxyz" get printed at the command ...
    (comp.unix.programmer)