I am developing a mini shell for a university practice and I run into an error that I have not been able to solve since according to what I have understood, searching in Google for my error, it is an error that can come out of several causes.
Let's see if someone sees why I get the error. For a moment I thought it could be a misuse of the fgets
[and I removed the line = fgets
... leaving only the fgets(.....);
] but it did not serve me what I thought would be a solution.
This is the error that the debugger gives me in the line of the
"line = fgets(line, MAX_LINE, stdin);"
Below I leave the code of the function that uses it with your explanation.
Program received signal SIGSEGV, Segmentation fault. __GI__IO_getline_info (fp = fp @ entry = 0x7ffff7dd4640 < _IO_2_1_stdin_ & gt ;, buf = buf @ entry = 0x0, n = 510, delim = delim @ entry = 10, extract_delim = extract_delim @ entry = 1, eof = eof @ entry = 0x0) at iogetline.c: 86 86 iogetline.c: The file or directory does not exist.
#define PROMPT "$" //declaramos el PROMPT que utilizaremos
#define MAX_LINE 512 //declaration of a variable that contains the max number of bits to read
/**
*read_line is a function that recibes a pointer as a parameter
*and returns the same pointer.
*This function prints the PROMPT,cleans the buffer and
*reads a line from the console.
*/
char *read_line (char *line) {
printf("%s", PROMPT);
if (fflush (stdout) != 0)
printf ("the buffer hasn't been cleaned correctly");
line = fgets(line, MAX_LINE, stdin);
if (line == NULL)
printf ("the line hasn't been read correctly or there was anything to read");
return line;
}
int main() {
char *line;
while(read_line(line)) {
execute_line(line);
}
}