-2

what is errno and EEXIST mean?

#include<errno.h>
    if(errno != EEXIST) {
        perror(sha1_dir);
        exit(1);
    }

I can't understand the condition of if statement. is that saying "if errno doesn't exist"?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
woowoo
  • 117
  • 1
  • 6
  • 4
    It is literally saying "if `errno` is not equal to `EEXIST`". – mkrieger1 Feb 02 '21 at 23:36
  • so what is errno? – woowoo Feb 02 '21 at 23:38
  • 2
    Right now it means nothing. `errno` would possibly be set by some function call (it is a variable that should be scoped thread-local). Check with the function setting `errno` to discover what `EEXIST` means in context of that function. – Christian Gibbons Feb 02 '21 at 23:38
  • 8
    You may find this page helpful: https://en.cppreference.com/w/c/error/errno – Brian61354270 Feb 02 '21 at 23:39
  • 2
    What `errno` is depends on the code above that line which you didn't show. – mkrieger1 Feb 02 '21 at 23:39
  • For a general idea of what EEXIST means, you can check '/usr/include/asm/errno-base.h' and find this: `#define EEXIST 17 /* File exists */` – Christian Gibbons Feb 02 '21 at 23:41
  • 5
    It's saying, "if the previous file-access function, which failed, failed for a reason *other* than that the file did not exist, print a relevant error message and exit." So if the file existed but we couldn't read it because of something like "Permission denied", that's an error. But if the reason we couldn't read the file was because it wasn't there at all, we're okay, so proceed. This is a common pattern when reading the file is optional, like a configuration file, or a state file to keep data between runs of a program, but which won't exist the very first time the program is run. – Steve Summit Feb 02 '21 at 23:52
  • 1
    Sorry, my explanation was almost completely wrong. I was thinking of ENOENT. In this case, it's "If the previous file-access function, which failed, failed for a reason other than that the file or directory *did* exist, print a relevant error message and exit." You might do this after calling `mkdir()`, to create a directory. If it tried and failed to create the directory, that's probably an error. But if the directory's already there, the program doesn't have to make it, so proceed. – Steve Summit Feb 02 '21 at 23:54
  • `errno` is a sort of a global variable that contains a code indicating why the last system call failed. `` contains a bunch of constants -- `EPERM`, `ENOENT`, `EEXIST`, etc. -- encoding the failure reasons. And then `perror()` is a library function which looks at `errno` and prints a human-redable string like "Permission denied" corresponding to the code in `errno`. See also the comments at [this recent question](https://stackoverflow.com/questions/65940663/why-is-direct-i-o-file-not-being-written-to#comment116593669_65940663). – Steve Summit Feb 02 '21 at 23:59
  • @SteveSummit Thank you your answer fulfilled my question – woowoo Feb 03 '21 at 00:42

1 Answers1

3

If you dig into errno.h, you will find EEXIST is a C macro defined in another file. On my Debian PC, it's in /usr/include/asm-generic/errno-base.h.

#define EEXIST      17  /* File exists */

Run man errno to find the meaning of errno variable:

DESCRIPTION
       The  <errno.h> header file defines the integer variable errno, which is
       set by system calls and some library functions in the event of an error
       to indicate what went wrong.

So the meaning of the code block is to check errno generated by the last function and if it's not "File exists" (file does not exist), print the error message.

Run man perror for more information. Seriously, you should learn C from some book before asking such a question to stackoverflow.

D.Liu
  • 96
  • 4