Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:39:26+00:00 2026-06-07T05:39:26+00:00

In usual case open() return the new file descriptor, or -1 if an error

  • 0

In usual case open() return the new file descriptor, or -1 if an error occurred and in that case, errno is set appropriately.

I am not understanding why this mechanism of errno is used here? whats the purpose of here? why just we can not map all error with some negative return no?

like

fd = open("/dev/tty0", O_RDWR | O_SYNC);
if(fd == -1)
  printf("this is EACCES error");
else if (fd == -2)
    printf("this is EPERM error");

Is there any benifit of errno mechanism.? if yes then i would like to know/understand then in other things i can also use this mechanism.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:39:28+00:00Added an answer on June 7, 2026 at 5:39 am

    Since fopen returns a FILE* you can’t expect it to return an error code in that pointer: the only “special” value for pointers is 0.

    As you observe, for open this restriction doesn’t hold. In fact systems as linux do exactly what you propose in their lower levels. The system call under the hood returns the negative error code if things go wrong. That (negated) code is then plug into errno by a shallow user space wrapper which then returns the -1 to indicate the error to the application.

    The reason that this is done so is purely historical. In good old times there was no threading and errno was still just a simple global variable. At that time the chosen strategy incurred not much overhead and probably seemed an acceptable way to communicate between OS and application. Since such interfaces basically can’t be changed without breaking a lot of code, we will be stuck with errno as a pseudo variable that is thread local.

    This is not ideal, but the overhead is not as bad as it sounds, since these are clearly error indications that should occur only exceptionally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The usual case. I have a simple app that will allow people to upload
The usual way to resolve lnk involve using WShell.WshShortcut or IShellLink that way :
as usual I'm working with my messed up database and I found that there
This is the usual way for declare a Java array: int[] arr = new
In Cython, the usual raise keyword emits C code that contains a reference to
When seeing about the whole finalizer/IDisposable issue, it is usual to see that, at
In the usual AccountController in my MVC3 app, if returnUrl is set (in my
I have a question on xquery grouping. From what I understand, the usual case
The usual clone idiom makes use of covariant return types: struct Base { virtual
In my main method, I start the main form as usual: Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.