Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does open() fail and errno is not set?

In my code open() fails with a return code of -1 but somehow errno is not getting set.

int fd;
int errno=0;
fd = open("/dev/tty0", O_RDWR | O_SYNC);
printf("errno is %d and fd is %d",errno,fd);

output is

errno is 0 and fd is -1

Why is errno not being set? How can i determine why open() fails?

like image 353
Jeegar Patel Avatar asked Oct 17 '25 11:10

Jeegar Patel


1 Answers

int errno=0;

The problem is you redeclared errno, thereby shadowing the global symbol (which need not even be a plain variable). The effect is that what open sets and what you're printing are different things. Instead you should include the standard errno.h.

like image 170
cnicutar Avatar answered Oct 19 '25 01:10

cnicutar