1)  No, the programmers declared an array of 5 character pointers, not
an array of 5 characters.  Perl 5 doesn't have pointers, so I understand
your confusion  :) They're one of the things that non-C programmers
bitch about.  (If you don't understand pointers, using them is a quick
way to a segfault).

2)  Yes, it's a list of up to five acceptable directories (or so it
appears).  The maximum size of each of these is not defined, but will be
the maximum size of a command line argument.  A pointer is an address in
memory.  Every program is passed two arguments to its main() function,
an int and an array of character pointers.  The programmers of tftpd are
setting each of the pointers in their array to one of the arguments (if
there are any arguments).  By doing this, they reduce the risk of a
buffer overflow by not copying their command line arguments into static
buffers.  They simply point their variables at the arguments already in
memory, rather than duplicate them.

3)  Syntactically, the while loop is valid.  Style is quite left up to
the programmer.  If you feel like writing code that looks like Perl, you
can  :)
(I usually code fairly close to the GNU style guides)

MSG


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?
> 
> 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.
> 
> 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.
> 
> --
> -------------------------------------
> Sam Bayne - System Administrator
> North Seattle Community College
> [EMAIL PROTECTED]     (206)527-3762
> =====================================
> 
> --
> To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
> as the Subject.


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to