We will take a look at the randtype(1) utility's source code. It is an
amazing tool and I have
always been amazed by it. This is also the first OpenBSD port I developed.

$ randtype /etc/passwd

will type out the file as though a user were entering characters on
the keyboard.

You can see the source here.

http://gayatri-hitech.com/languageclasses/randtype.c.html

I will explain the juicy parts. If I explain everything you guys will
start deleting
my mails as it will be too long and untenable. I don't want that obviously.

Also before I start I wish to mention that the smart Debian
users(people who are good enough to use
raw Debian instead of *buntu or whatever) can obtain the source code
of a program by this command as
a user.

$ apt-get source randtype

or even

$ apt-get source wcalc

which is what I did this morning. ;)

Now let us take the juicy parts of the file randtype.c

static char chk_special(const char *str, char c)
{
    register char i;

    while ((i = *str++)) {
        if (i == '\\' && *str) {
            switch (i = *str++) {
              case 'a':
                i = '\007';
                break;
              case 'b':
                i = '\b';
                break;
              case 'f':
                i = '\f';
                break;
              case 'n':
                i = '\n';
                break;
              case 'r':
                i = '\r';
                break;
              case 't':
                i = '\t';
                break;
              case 'v':
                i = (int) 0x0B;
                break;
              case '\\':
                i = '\\';
                break;
              default:
                break;
            }
        }

        if (i == c)
            break;
    }

    return i;
}


This function is typical of C string handling code. In C, the most
painful aspect is the care with
which you have to handle characters in a string. There are no
convenient string functions like in
other languages.

You have to do everything by hand. You of course have many string
functions like strchr(), strrchr(),
strpbrk(), strtrok(), strsep() and of course strcmp(), strcasecmp().

But despite all this, you don't have functions like in Python which
make it very easy to process strings.

Anyway the above function iterates thro' the characters in a string
and breaks out of the while() loop when
the character c in the argument  matches character pointed to by i in
the string. The line

            switch (i = *str++) {

is the loop iterator command which moves the i variable to point to
the successive characters in a string.

As I have said before, the only way to master C is by looking at other
people's code. And I have made most
of my money by working and improving on top of other people's code.
(including my present work)

Anyway now let us look at one more function before we wind up for this week.

static void randsleep(float ms, unsigned int mult)
{
    static struct timeval tv;
    long i;

#ifdef HAVE_RANDOM
    i = (1 + (int) (ms * random() / (RAND_MAX + 1.0))) * mult;
#else
    i = (1 + (int) (ms * rand() / (RAND_MAX + 1.0))) * mult;
#endif

    /* FreeBSD likes one less */
    if (i > 999999L) {
        tv.tv_sec = i / 1000000;
        tv.tv_usec = i % 1000000;
    }
    else
        tv.tv_usec = i;

    /* select() is unbelievably more efficient than usleep(). */
    select(0, NULL, NULL, NULL, &tv);

    return;
}

There are two things that are illustrated in the above code.

        a) select(2) is better for timeouts and sleeping than anything else.
        b) You also need to know how to manipulate struct timeval fields.

There are other time() data structures like time_t, struct tm and so
on that you may wish to learn.

-Girish

-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com
_______________________________________________
To unsubscribe, email [email protected] with 
"unsubscribe <password> <address>"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to