LQ Newbie
Registered: Nov 2004
Location: East Windsor, NJ
Distribution: Mandriva
Posts: 18
Original Poster
Rep:
|
Towards the bottom of writeTemplateDisplay() there are 2 delwin() calls. When I was getting the segfault
the if statements that check for errors were not present. As soon as I added them the segfault went away.
However, they have never been triggered to show that delwin() failed. Now, the actual segmentation fault
occurred in the function that called this one, but I didn't make any changes to it and the segfault went away. The calling function,
writeConfigurationDisplay() would print the final fprintf(), but then segfault before execution could begin
in the calling function. Now, the really strange thing is, if I erase those error catching if statements at the
end of writeTemplateDisplay() the program will now run without problems. So I am really at a loss to understand
what caused the original segmentation fault.
int writeTemplateDisplay(FILE *log, WINDOW *wptr, int vMid, int hMid) {
DIR *templateDir;
struct dirent *dirEntry;
char filename[201], firstFile[201], buffer[150];
int len, statRes, line = 1, contentLine, delwinRes;
struct stat *statBuf;
mode_t modes;
WINDOW *fileListWin, *fileContentWin;
long int directoryPos[100];
FILE *currentFile;
// erase contents of window and redraw box
weraseAndRebox(log, wptr);
// create two subwindows, one to list template files, the other to display their content
fileListWin = subwin(wptr, LINES-4, 30, 2, 32);
scrollok(fileListWin, 1);
box(fileListWin, ACS_VLINE, ACS_HLINE);
fileContentWin = subwin(wptr, LINES-4, COLS-64, 2, 63);
scrollok(fileContentWin, 1);
box(fileContentWin, ACS_VLINE, ACS_HLINE);
touchwin(wptr);
// open the templates directory
templateDir = opendir("/etc/sinterface/templates");
if (!templateDir) {
fprintf(log, "ERROR - could not open template directory, '/etc/sinterface/templates'.\n");
fflush(log);
return(-1);
}
// save the directory location for fast indexing later.
directoryPos[line-1] = telldir(templateDir);
// read templates directory
fprintf(log, "Reading templates directory...\n");
fflush(log);
dirEntry = readdir(templateDir);
while (dirEntry) {
len = strlen(dirEntry->d_name);
if (len >= 201) {
fprintf(log, "ERROR - Encountered a filename in excess of 200 characters in the template directory.\n");
fflush(log);
return(-2);
}
sprintf(filename, "/etc/sinterface/templates/%s\0", dirEntry->d_name);
fprintf(log, "Found file/directory: '%s'\n", filename);
fflush(log);
statRes = stat(filename, statBuf);
if (statRes != 0) {
fprintf(log, "Could not stat '%s'.\n", filename);
fflush(log);
directoryPos[line-1] = telldir(templateDir);
dirEntry = readdir(templateDir);
continue;
}
modes = statBuf->st_mode;
//if (!S_ISDIR(modes) && S_ISREG(modes)) {
if (S_ISREG(modes)) {
// display the name and save into a data structure
fprintf(log, "\t'%s' is a file\n", filename);
fflush(log);
mvwprintw(fileListWin, line, 1, "%s", dirEntry->d_name);
// copy the first file to the content subwindow
if (line == 1) {
contentLine = 1;
currentFile = fopen(filename, "r");
if (currentFile) {
wattrset(fileContentWin, COLOR_PAIR(7));
while(!feof(currentFile)) {
fgets(buffer, 150, currentFile);
mvwprintw(fileContentWin, contentLine, 1, "%s", buffer);
contentLine++;
}
wattrset(fileContentWin, COLOR_PAIR(3));
} else {
mvwprintw(fileContentWin, 10, 10, "File not readable!");
}
fclose(currentFile);
}
line++;
}
directoryPos[line-1] = telldir(templateDir);
dirEntry = readdir(templateDir);
}
wrefresh(wptr);
sleep(5);
// clean up
closedir(templateDir);
werase(fileListWin);
werase(fileContentWin);
delwinRes = delwin(fileListWin);
// This was added to try to determine where the segfault was occurring
if (delwinRes == ERR) {
// These error catching statement have never been tripped
fprintf(log, "Could not delete subwindow fileListWin.\n");
fflush(log);
}
delwinRes = delwin(fileContentWin);
// This was added to try to determine where the segfault was occurring
if (delwinRes == ERR) {
// These error catching statement have never been tripped
fprintf(log, "Could not delete subwindow fileContentWin.\n");
fflush(log);
}
touchwin(wptr);
weraseAndRebox(log, wptr);
wrefresh(wptr);
return(0);
}
int writeConfigurationDisplay(FILE *log, int dispNo, WINDOW *wptr) {
int horzMid = (int)floor((double)(COLS-31)/2.);
int vertMid = (int)floor((double)(LINES-2)/2.);
switch(dispNo) {
case 1: //Templates
writeTemplateDisplay(log, wptr, vertMid, horzMid);
// Segfault occurs after the above function returns but before execution resumes in
// function that called this one.
break;
case 2:
wattrset(wptr, COLOR_PAIR(4));
mvwprintw(wptr, vertMid, horzMid-14, " ");
mvwprintw(wptr, vertMid, horzMid-14, "not yet supported");
wrefresh(wptr);
break;
case 3:
wattrset(wptr, COLOR_PAIR(4));
mvwprintw(wptr, vertMid, horzMid-14, " ");
mvwprintw(wptr, vertMid, horzMid-14, "not yet supported");
wrefresh(wptr);
break;
case 4:
wattrset(wptr, COLOR_PAIR(4));
mvwprintw(wptr, vertMid, horzMid-14, " ");
mvwprintw(wptr, vertMid, horzMid-11, "not yet supported");
wrefresh(wptr);
break;
case 5:
wattrset(wptr, COLOR_PAIR(4));
mvwprintw(wptr, vertMid, horzMid-14, " ");
mvwprintw(wptr, vertMid, horzMid-13, "not yet supported");
wrefresh(wptr);
break;
case 6: // Help
wattrset(wptr, COLOR_PAIR(4));
mvwprintw(wptr, vertMid, horzMid-14, " ");
mvwprintw(wptr, vertMid, horzMid-11, "not yet supported");
wrefresh(wptr);
break;
}
fprintf(log, "about to leave write configuration display\n");
fflush(log);
return(0);
}
|