sourcecode

Wednesday, July 11, 2012

READ(2)

$man 2 read

NAME
       read - read from a file descriptor

SYNOPSIS
       #include

       ssize_t read(int fd, void *buf, size_t count);
 



  • size_t and ssize_t are both int

  • fd: STDIN_FILENO for standard in (keyboard, effectively 0)

  • There are three numbers: the buff size (bSize), the acceptance count size (cSize) and the actually input size (iSize).  
    1. When (iSize < cSize): if the input is terminated by Enter, then add one to the number of letters; if the input is terminated by ctrl+d, then no extra letter is appended. 
      • For example, user input: $ABC
        with an Enter, then iSize == 4, and the buffer stores ASCII (* means garbage integers):
        65 66 67 10 *  *  *
      • user input: $ABC
        with a ctrl+d, then iSzie == 3, and the buffer stores ASCII:
        65 66 67 *  *  *
    2. When (iSize > cSize), the exceeding input is ignored and resultant iSize == cSize
    3.  When (bSize < cSize), danger! The system does not perform boundary check! So one can input long enough to write out of the buffer's boundary and no warnings given. This probably won't corrupt immediately, but it still can cause fatal error potentially at any time!
    4. When a read() function is encountered, the system waits for the input. And every call of read(), the buf is overwritten.
For write(2) function, similar scenarios show. the fd can be STDOUT_FILENO for standard output (monitor, effectively 1).

/*********************END*******************/

No comments: