Robust IO function code of the RIO package. [Php] The following is about [php] # export destdio. h # includestring. h # includeerrno. h # includepolicypes. h # includefcntl. h # includesysstat. h # defineMAXLINE1024 * unbu [php]
The following is about
The following describes [php]
# Include
# Include
# Include
# Include
# Include
# Include
# Define MAXLINE 1024
/* Unbuffer input/output function */
Ssize_t rio_readn (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;
While (nleft> 0 ){
If (nread = read (fd, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {
Return-1;/* error */
}
} Else if (nread = 0 ){
Break;/* EOF */
} Else {/* read content */
Nleft-= nread;
Bufp + = nread;
}
}
Return (n-nleft );
}
Ssize_t rio_writen (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nwritten;
Char * bufp = usrbuf;
While (nwritten = write (fd, bufp, nleft) <= 0 ){
If (errno = EINTR ){
Nwritten = 0;
} Else {
Return-1;
}
Nleft-= nwritten;
Bufp + = nwritten;
}
Return n;
}
/*************************************** ***************************************/
# Define RIO_BUFSIZE 8192
Typedef struct {
Int rio_fd;/* To operate the file descriptor */
Int rio_cnt;/* unread bytes in internal buf */
Char * rio_bufptr;/* next unread byte int internal buf */
Char rio_buf [RIO_BUFSIZE];/* internal buf */
} Rio_t;
Void rio_readinitb (rio_t * rp, int fd)
{
Rp-> rio_fd = fd;
Rp-> rio_cnt = 0;
Rp-> rio_bufptr = rp-> rio_buf;
}
Static ssize_t rio_read (rio_t * rp, char * usrbuf, size_t n)
{
Int cnt;
While (rp-> rio_cnt <= 0) {/* Read the file content if buf is empty */
Rp-> rio_cnt = read (rp-> rio_fd, rp-> rio_buf, sizeof (rp-> rio_buf ));
If (rp-> rio_cnt <0 ){
If (errno! = EINTR ){
Return-1;
}
} Else if (rp-> rio_cnt = 0) {/* EOF */
Return 0;
} Else {/* reset buf ptr */
Rp-> rio_bufptr = rp-> rio_buf;
}
}
/* When n <rp-> rio_cnt, need copy some times */
Cnt = n;
If (rp-> rio_cnt <n) {/* one time copy end */
Cnt = rp-> rio_cnt;
}
Memcpy (usrbuf, rp-> rio_bufptr, cnt );
Rp-> rio_bufptr + = cnt;
Rp-> rio_cnt-= cnt;
Return cnt;
}
Ssize_t rio_readlineb (rio_t * rp, void * usrbuf, size_t maxlen)
{
Int n, rc;
Char c, * bufp = usrbuf;
For (n = 1; n <maxlen; n ++ ){
If (rc = rio_read (rp, & c, 1) = 1 ){
* Bufp ++ = c;
If (c = '\ n '){
Break;
}
} Else if (rc = 0 ){
If (n = 1) {/* EOF no data read */
Return 0;
} Else {/* EOF some data read */
Break;
}
} Else {/* ERROR */
Return-1;
}
}
* Bufp = 0;/* string end sign: '\ 0 '*/
Return n;
}
Ssize_t rio_readnb (rio_t * rp, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;
While (nleft> 0 ){
If (nread = rio_read (rp, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {/* errno set by read ()*/
Return-1;
}
} Else if (nread = 0) {/* EOF */
Break;
}
Nleft-= nread;
Bufp + = nread;
}
Return (n-nleft);/* return> = 0 */
}
Int main ()
{
Int n;
Rio_t rio;
Char buf [MAXLINE];
Int fd = open ("1.txt", O_RDONLY, 755 );
If (fd <= 0 ){
Printf ("error \ n ");
}
Rio_readinitb (& rio, fd );
While (n = rio_readlineb (& rio, buf, MAXLINE ))! = 0 ){
Rio_writen (1, buf, n );
}
Close (fd );
Return 0;
}
# Include
# Include
# Include
# Include
# Include
# Include
# Define MAXLINE 1024
/* Unbuffer input/output function */
Ssize_t rio_readn (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;
While (nleft> 0 ){
If (nread = read (fd, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {
Return-1;/* error */
}
} Else if (nread = 0 ){
Break;/* EOF */
} Else {/* read content */
Nleft-= nread;
Bufp + = nread;
}
}
Return (n-nleft );
}
Ssize_t rio_writen (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nwritten;
Char * bufp = usrbuf;
While (nwritten = write (fd, bufp, nleft) <= 0 ){
If (errno = EINTR ){
Nwritten = 0;
} Else {
Return-1;
}
Nleft-= nwritten;
Bufp + = nwritten;
}
Return n;
}
/*************************************** ***************************************/
# Define RIO_BUFSIZE 8192
Typedef struct {
Int rio_fd;/* To operate the file descriptor */
Int rio_cnt;/* unread bytes in internal buf */
Char * rio_bufptr;/* next unread byte int internal buf */
Char rio_buf [RIO_BUFSIZE];/* internal buf */
} Rio_t;
Void rio_readinitb (rio_t * rp, int fd)
{
Rp-> rio_fd = fd;
Rp-> rio_cnt = 0;
Rp-> rio_bufptr = rp-> rio_buf;
}
Static ssize_t rio_read (rio_t * rp, char * usrbuf, size_t n)
{
Int cnt;
While (rp-> rio_cnt <= 0) {/* Read the file content if buf is empty */
Rp-> rio_cnt = read (rp-> rio_fd, rp-> rio_buf, sizeof (rp-> rio_buf ));
If (rp-> rio_cnt <0 ){
If (errno! = EINTR ){
Return-1;
}
} Else if (rp-> rio_cnt = 0) {/* EOF */
Return 0;
} Else {/* reset buf ptr */
Rp-> rio_bufptr = rp-> rio_buf;
}
}
/* When n <rp-> rio_cnt, need copy some times */
Cnt = n;
If (rp-> rio_cnt <n) {/* one time copy end */
Cnt = rp-> rio_cnt;
}
Memcpy (usrbuf, rp-> rio_bufptr, cnt );
Rp-> rio_bufptr + = cnt;
Rp-> rio_cnt-= cnt;
Return cnt;
}
Ssize_t rio_readlineb (rio_t * rp, void * usrbuf, size_t maxlen)
{
Int n, rc;
Char c, * bufp = usrbuf;
For (n = 1; n <maxlen; n ++ ){
If (rc = rio_read (rp, & c, 1) = 1 ){
* Bufp ++ = c;
If (c = '\ n '){
Break;
}
} Else if (rc = 0 ){
If (n = 1) {/* EOF no data read */
Return 0;
} Else {/* EOF some data read */
Break;
}
} Else {/* ERROR */
Return-1;
}
}
* Bufp = 0;/* string end sign: '\ 0 '*/
Return n;
}
Ssize_t rio_readnb (rio_t * rp, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;
While (nleft> 0 ){
If (nread = rio_read (rp, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {/* errno set by read ()*/
Return-1;
}
} Else if (nread = 0) {/* EOF */
Break;
}
Nleft-= nread;
Bufp + = nread;
}
Return (n-nleft);/* return> = 0 */
}
Int main ()
{
Int n;
Rio_t rio;
Char buf [MAXLINE];
Int fd = open ("1.txt", O_RDONLY, 755 );
If (fd <= 0 ){
Printf ("error \ n ");
}
Rio_readinitb (& rio, fd );
While (n = rio_readlineb (& rio, buf, MAXLINE ))! = 0 ){
Rio_writen (1, buf, n );
}
Close (fd );
Return 0;
}
The above functional RIO package provides two different types of functions:
1. input and output functions without buffering
2. Buffered input functions
This is mainly for file I/O, because the standard I/O itself is buffered. when we perform read/write operations on files, standard I/O functions are generally used preferentially, but in network programming, only file Io functions can be used. the above functions may be used here. if you are interested, you can learn more about the above mechanism. In network communication, this mechanism is still widely used!
The following is about [php] # include stdio. h # include string. h # include errno. h # include sys/types. h # include fcntl. h # include sys/stat. h # define MAXLINE 1024/* unbu...