i've been staring at this code for weeks now and i can't figure out why it segfaults. can someone have a look at it and give me some feedback? thanks!
the function opens a file located in the users ~ directory (that's about as far into linux programming as this code gets BTW). the line from the fileis formatted as follows
prog sometext morestuff xterm -e ssh blah blah
it skips the leading "prog" (intentionally) and grabs sometext, puts into an array, then gets morestuff, also puts into an array, then reads the rest into an array as it appears, spaces and all. this works well. however i get a segfault at the end of the function call. here is the functions code...
Code:
void initialize(void){
int i = 0, count = 5;
char hover[75] = { };
char icon[75] = { };
char prog[75] = { };
char path[50] = { };
char *hold;
const char *file = { "/.icewm/toolbar" }; // default path to toolbar file
struct passwd *name;
name = getpwuid( getuid() );
sprintf( path, "%s%s", (char *)name->pw_dir, (char *)file); // create ~/.icewm/toolbar path
FILE *fptr;
char *p;
fptr = fopen(path, "r" );
hold = (char *)malloc(sizeof(fptr)*sizeof(char) );
fgets(hold, 100, fptr);
do{
if ( ( p = strchr ( hold, '\n' ) ) != NULL ) *p = '\0';
printf("\ninitialized - contains: [%s]\n", hold);
count = 5;
memset(hover,0,75); //reset memory to 0
memset(icon,0,75);
memset(prog,075);
i = 0;
// the print statements in the following
// while loops are for checking memory
// contents only.
while(hold[count] != ' '){
printf("[%c] ", hold[count]);
hover[i] = hold[count];
++i;
++count;
}
i = 0;
++count;
while(hold[count] != ' '){
icon[i] = hold[count];
++i;
++count;
}
memset(hold,0,sizeof(hold)); //reset hold to 0
} while( (fgets(hold, 100, fptr)) != NULL);
fclose(fptr);
// check the values of the last iteration
// just to make sure. (this is not a feature)
printf("\n\nFinal hover: %s\nicon: %s\nprog: %s\n", hover, icon, prog);
memset(hover,0,75);
memset(icon,0,75);
memset(prog,0,75);
memset(hold,0,sizeof(hold));
getchar();
}
i = 0;
++count;
while(hold[i] != '\0'){
prog[i] = hold[count];
++i;
++count;
}
i = 0;
thanks again.
-jstn