Re: [code] showing all players in pfile

From: Mike Stilson (mike@velgarian.sytes.net)
Date: 08/13/02


On Tue, Aug 13, 2002 at 01:52:15PM +0200, Peter d wrote:
>Hi!
>i used this snippet, don't remember who wrote it..
>it only shows one player.. i want it to show all stored in pfile..
>
>ACMD(do_players)
>{
> char buf[MAX_INPUT_LENGTH];
> int i, count = 0;
> *buf = 0;
>
> for (i = 0; i < top_of_p_table; i++) {
>   sprintf(buf, "%s  %-20.20s", buf, (player_table + i)->name);
>   count++;
>   if (count == 3) {
>     count = 0;
>     strcat(buf, "\r\n");
>   }
> }
> page_string(ch->desc, buf, 1);
>}

First off, there's some BIG problems with this regarding buffer
overflows and that sprintf(buf, "%s ...", buf)

Secondly, I'm guessing that since it just prints 1 player, that you have
2 players in your player_table.
It should be "<= top_of_p_table".

And for completeness sake, here's my do_players.  It's got some ugly
math for the buffer size but for some reason I like it this way more
than pre-allocating static buffers everywhere.  And it allows a little
padding in case something is a bit too large.


ACMD(do_players)
{
        char *buf = NULL;
        int i, count = 0;

        buf = (char *)malloc((top_of_p_table * 26) + (top_of_p_table / 3) * 2 + 25);

        for (i = 0; i <= top_of_p_table; i++) {
                count += sprintf(buf + count, "%-20.20s %2d%s",
                                CAP((player_table + i)->name), (player_table + i)->level, ((i+1)%3)?" ":"\r\n");

        }
        sprintf(buf + count, "\r\n%4d players listed.\r\n", i+1);
        page_string(ch->desc, buf, 1);
        free(buf);
}

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT