#include #include // for open #include // for perror #include // for strerror #include // for getpid using namespace std; int main() { int fd; int errorSave; cout << endl; cout << "Just so you know, EACCES is " << EACCES << endl; cout << "Just so you know, EBADF is " << EBADF << endl; cout << endl; // the right way. cout << endl << "Executing " << endl; cout << "fd = open(\"/etc/passwd\", O_RDWR);" << endl; fd = open("/etc/passwd", O_RDWR); if (-1 == fd) { // save it, perror might change it if there is a problem. errorSave = errno; perror("\tUnable to open /etc/passwd"); cout << "\terrno = " << errno << " errorSave = " << errorSave << endl; // don't mess with these two strings // if you need them, save them as they might change later. cout << "\tThe error string is " << strerror(errorSave) << endl; cout << "\tThe error name is " << strerrorname_np(errorSave) << endl; // the error has been handled, reset it. errno = 0; } cout << endl << endl; // careful, any system call could reset the errno variable. cout << "EXECUTING: fd = open(\"/etc/passwd\", O_RDWR);" << endl; fd = open("/etc/passwd", O_RDWR); if (-1 == fd) { errorSave = errno; cout << "\terrno = " << errno << " errorSave = " << errorSave << endl; // force another system error. close(999); cout << "\terrno = " << errno << " errorSave = " << errorSave << endl; perror("Error from opening /etc/passwd"); errno = 0; } cout << endl << endl; cout << "EXECUTING: if (-1 == fd = open(\"/etc/passwd\", O_RDWR) ) {" << endl; // you will see this, but my compiler flags complained so I added () // if (-1 == fd = open("/etc/passwd", O_RDWR) ) { if (-1 == (fd = open("/etc/passwd", O_RDWR)) ) { errorSave = errno; perror("\tERROR: opening /etc/passwd failed"); } return 0; }