The following code gives me a segmentation fault when I change the keys variable to > 256. The issue seems to be with the strcpy(str[i], keyname(i)); statement. I can get output if I swap out the i with a value like 300 but if I use the counter it gives me the fault.
#include <curses.h>
#include <string.h>
int main(){
initscr();
noecho();
nonl();
keypad(stdscr, TRUE);
int length = 300;
int keys = 255;
char buf[length];
int i=0;
char str[keys+1][length];
strcpy(str[0], "keyname()\thas_key()\tinteger\n");
for(i=1;i<keys;i++){
strcpy(str[i], keyname(i));
if(has_key(i))
strcat(str[i], "\t\tTRUE");
else
strcat(str[i], "\t\tFALSE");
sprintf(buf, "\t\t%i", i);
strcat(str[i], buf);
strcat(str[i], "\n");
}
endwin();
echo();
for(i=0;i<keys-1;i++){
printf("%s", str[i]);
}
return 0;
}
The curses keyname
function returns a null pointer if there is no name for that key. (Which happens when you pass 256 and so on).
Trying to strcpy
out of a null pointer causes your segmentation fault.
To fix this you will need to take different action when keyname
returns a null pointer, e.g.
char const *name = keyname(i);
if ( name )
strcpy( str[i], name );
else
strcpy( str[i], "unknown key" );