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;
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;
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;
printf("pentry = %s\n",pentries[i].ppath);
}
unlink(dname);
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);
}
}
***********
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.
