On Thu, May 04, 2000 at 05:23:37PM -0700, Sam Bayne wrote:
> So There I am, rooting around in the source to the tftpd server
> (never you mind why) when I see this:
>
> -----------------------------------------
> #define MAXARG 4
> static char *dirs[MAXARG+1];
>
> int
> main(int ac, char **av)
> {
> register struct tftphdr *tp;
> register int n = 0;
> int on = 1;
>
> ac--; av++;
> if (ac==0) dirs[0] = "/tftpboot"; /* default directory */
> while (ac-- > 0 && n < MAXARG)
> dirs[n++] = *av++;
> -----------------------------------------
>
> There are several things about this code that puzzle me.
>
> 1. It looks to me like 'dirs' is set to be a 5 character
> string by the MAXARG define, then they turn around
> and set a default of 9 characters? Didn't they just
> run right off the end of the array?
static char *dirs[MAXARG+1];
This is an array of 5 character pointers.
> 2. Or am I misreading this and it's creating a list of up to
> 5 acceptable directories? If that's so, where does it set
> the maximum size for each argument? If that's in some
> previous part of the declarations, that's fine, just
> give me an example construction to look for.
There is no length. He's assigining string pointers to string pointers.
(one is a ptr to a string constant, the others are string pointers passed
to you by the execve function *otherwise known as command line arguments*)
> 3. Stylistically, is that 'while' statement normal? It looks to
> me like a syntax mistake waiting to happen, but I'll code
> to that style if it's normal for C programmers. Coming
> from a mostly perl background that line would either be
> one line or enclosed in curly braces.
It's not something I would write, but it's acceptable. If you can't
understand what it does in under 10 seconds, then it's a safe bet you
shouldn't write it that way for your own sake, much less anyone else's.
--
Steve Borho Voice: 314-615-6349
Network Engineer
Celox Networking Inc
Fortune of the day:
I use not only all the brains I have, but all those I can borrow as well.
-- Woodrow Wilson
--
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.