I do not leave the function of the signal handler correctly? It does not seem to return to the program normally. Instead, it goes into a loop and where it should wait for user input, it skips and reads the length of "user input" to -1 and errors. (There will be more meaning in the code.)
void handle_SIGINT() {
int k = recent;
int count = 0;
int stop;
if (stringSize >= 10) {
stop = 10;
}
else {
stop = p;
}
printf("\nCommand History:\n");
for (count = 0; count < stop; count++) {
if (k < 0) {
k += 10;
}
printf("%s", string[abs(k)]);
k -= 1;
}
}
void setup(char inputBuffer[], char *args[],int *background)
{
int length,
i,
start,
ct;
int add = 1;
ct = 0;
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
printf("%i",length);
start = -1;
if (length == 0)
exit(0);
if (length < 0){
perror("error reading the commanddddddddd");
exit(-1);
}
}
int main(void)
{
char inputBuffer[MAX_LINE];
int background;
char *args[MAX_LINE/2+1];
FILE *inFile = fopen("pateljay.history", "r");
if (inFile != NULL) {
int count = 0;
char line[MAX_LINE];
while (fgets(line,sizeof line, inFile) != NULL) {
string[count] = strdup(line);
printf("%s", string[count]);
count++;
stringSize++;
}
p = count % 10;
recent = abs(p - 1);
}
fclose(inFile);
struct sigaction handler;
handler.sa_handler = handle_SIGINT;
sigaction(SIGINT, &handler, NULL);
while (1) {
background = 0;
printf("COMMAND->");
fflush(0);
setup(inputBuffer, args, &background);
}
}
So, when ctrl + c is entered, it should catch the signal and process it. As soon as he returns to the main one, he goes into the setup and completely skips the read function and makes the length equal to -1. This, in turn, leads to errors in the program. I think the code inside handle_SIGINT doesn't matter as it is now. Does anyone know why it skips the read function in setup?
source
share