Issue
While testing my code, I have found out readline makes errno 2. This is ENOENT which means No such file or directory. Does anyone know why??
#include <readline/readline.h>
#include <stdio.h>
#include <errno.h>
int main()
{
char *a = readline("shell$");
printf("%d\n", errno);
while (a)
{
printf("%s\n", a);
readline("shell$");
}
}
Do I have to set errno to 2 after readline? or Is it an error?
its in Macos
compiled with this comm -> gcc -lreadline main.c
Since I am using the Mac which is provided from a kind of institution, I don't have super privileges. Maybe could it cause the error? permission thing?
Solution
If a function does not return error value (or otherwise indicate there was an error), then, generally speaking, valuenof errno
is not set or reset by it.
So probably some operation done by readline
had an error, but it was recoverable and readline
succeeded and did not touch errno
itself.
So, before checking errno
, you have to check if the function failed (or otherwise is documented to set errno
in some specific way), usually by checking the return value.
Answered By - hyde Answer Checked By - Clifford M. (WPSolving Volunteer)