Link to home
Start Free TrialLog in
Avatar of amankhan
amankhan

asked on

Can Anyone Explain This Program line by line if you have patience .....

Hi Guys

 This Code IS from Richard Stevens Book......




/* include readline */
#include        "unp.h"

static ssize_t
my_read(int fd, char *ptr)
{
        static int      read_cnt = 0;
        static char     *read_ptr;
        static char     read_buf[MAXLINE];

        if (read_cnt <= 0) {
again:
                if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
                        if (errno == EINTR)
                                goto again;
                        return(-1);
                } else if (read_cnt == 0)
                        return(0);
                read_ptr = read_buf;
        }

        read_cnt--;
        *ptr = *read_ptr++;
        return(1);
}

ssize_t
readline(int fd, void *vptr, size_t maxlen)
{
        int             n, rc;
        char    c, *ptr;

        ptr = vptr;
        for (n = 1; n < maxlen; n++) {
                if ( (rc = my_read(fd, &c)) == 1) {
                        *ptr++ = c;
                        if (c == '\n')
                                break;  /* newline is stored, like fgets() */
                } else if (rc == 0) {
                        if (n == 1)
                                return(0);      /* EOF, no data read */
                        else
                                break;          /* EOF, some data was read */
                } else
                        return(-1);             /* error, errno set by read() */
        }

        *ptr = 0;       /* null terminate like fgets() */
        return(n);
}
/* end readline */

ssize_t
Readline(int fd, void *ptr, size_t maxlen)
{
        ssize_t         n;

        if ( (n = readline(fd, ptr, maxlen)) < 0)
                err_sys("readline error");
        return(n);
}


help me

aman

Avatar of amankhan
amankhan

ASKER

Whats the main usage of static variables in that program....

aman
Two, er THREE  probs:

(1)  I don't see ANY static vars in the code.

(2)  I don't see a program.

(2)  Sure sounds like homework.....

When you invoke a function, any local variable (function local) is created on the fly, if this function is invoked many times, then this can make your program slower.
A static variable is like a global variable (never is destroyed until program ends) but in a local scope (the function, in this case)

So static variables can be used for 2 main purposes:
1.- To remember the previous value of the variable when function was invoked
2.- To avoid the variables to be created and destroyed every time the function is invoked

I think in your sample code static variables are used for both purposes:
- read_cnt will remember last value, so it keep count of last value the previous time my_read was invoked
- read_buf will be not created again and again when my_read is invoked, gaining some performance
 
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Narendra Kumar S S
read_cnt need not be a static variable.
read_buf is a local array, where the data read is getting stored. It's address will be given to pointer ptr, which will be used by the called function.
If read_buf is not a static variable, then once the function returns, the address stored in ptr will become invalid! To avoid this danger, read_buf is declared as static array.
The static pointer read_ptr is not at all needed. The address of read_buf can be directly assigned to ptr.

-ssnkumar
ssnkumar,

You did not read the code carefully.  read_cnt and read_ptr MUST be static in this
implementation since they maintain the current read position and number of valid
bytes remaining within read_buf[] between calls to my_read().  See my explanation above.
Note that my_read() returns a single char read from the input stream, albeit in an
awkward manner compared to fgetc().

I think that is correct.
I didn't read the code properly!:-(
Anyway, thanks for pointing out the mistake...

-ssnkumar