The main problem is that you are copying the pointer values of result readdir 
is giving you. And the pointer points to the string value, which changes 
everytime read-dir is invoked. 

Look below for a fix:

On Tuesday 21 September 2004 11:35 am, Bruce Shaw wrote:
> I'm working on the partitions problem in HOST-RESOURCES-MIB for Solaris. 
> As it stands, the code is making all sorts of wrong decisions about which
> partition it's supposed to be using.  I'm trying to write a simple
> stand-alone program to do it correctly and when that's working, fix the HRM
> code.  My problem is that my application isn't behaving as I expect it to.
>
> Here's the code as it stands..
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <dirent.h>
> #include <unistd.h>
>
> typedef struct pentry part_entry;
>
> struct pentry {
> char *pdevname;
> char *sdname;
> char *ppath;
> };
>
> main()
> {
> FILE *fp;
// No need for *fp?
> DIR *dr;
> struct dirent *dr_entry;
> char buf[256];
> char dname[21];
>
> part_entry pentries[256];
>
> int i = 0;
> int j = 0;
>
> dr = opendir("/dev/dsk");
> while ((dr_entry = readdir(dr)) != NULL) {
>     sprintf(dname,"/dev/dsk/%s",dr_entry->d_name);
>     printf("fname -> %s\n",dname);
>     pentries[i].pdevname = dname;
// change it to:
     pentries[i].pdevname = strdup(dname);
>     printf("pentry = %s\n",pentries[i].pdevname);
>     if (readlink(dname, buf, 256) < 0)
>         printf("bad symbolic link\b");
>     else {
>         printf("%s\n",buf);
>         pentries[i].ppath = buf;
// and change it too:
pentries[i].ppath = strdup(buf);
>         printf("pentry = %s\n",pentries[i].ppath);
>         }
>     unlink(dname);
// You probably just want to clean the memory:
memset(&dname, 0x00, 21);
>     i++;
>     }
> closedir(dr);
> printf("found %d entries\n",i);
> for (j = 0; j < i; j++) {
>     printf("j= %d\n",j);
>     printf("  dev= %s\n", pentries[j].pdevname);
>     printf("  path= %s\n", pentries[j].ppath);

// These guys free the memory so you don't end up with memory leak.
        free(pentries[i].pdevname);
        free(pentries[i].ppath);
>     }
> }
>
> ***********
>
> When I execute the while loop, it reads the directory and link information
> and prints it out correctly both times.  When I get to the for loop at the
> end, it prints a zero, then the device name (/dev/dsk/c4t6d0s7) for the
> LAST partition read (not the first one), then segfaults.
>
> I tried changing the declarations for the strings inside pentry eg. char
> sdname[9] and the assignments to eg. strcpy(pentries[i].ppath,buf) but when
> I ran it, it blew away the contents of /dev/dsk so now I'm scared to do
> anything.
>
> Yes, I know I'm not null-terminating my strings but I'm obviously doing
> something else fundamentally wrong.  Yes, I've RTFM'd the C manuals but
> they just got me more confused than ever because none of the examples are
> alike and none are adequately explained.
>
> Have I mentioned recently that I hate pointers?
>
> This communication is intended for the use of the recipient to which it is
> addressed, and may contain confidential, personal and or privileged
> information. Please contact us immediately if you are not the intended
> recipient of this communication, and do not copy, distribute, or take
> action relying on it. Any communication received in error, or subsequent
> reply, should be deleted or destroyed.

-- 
Regards,

Konrad Rzeszutek,
IBM Cambridge;l 1-617-693-1718


-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
Project Admins to receive an Apple iPod Mini FREE for your judgement on
who ports your project to Linux PPC the best. Sponsored by IBM.
Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php
_______________________________________________
Net-snmp-coders mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to