File type indicator, sys / stat.h st_mode value of a regular file

I am trying to determine file types for directory entries (Windows Unix, etc.).

In sys / stat.h, the upper order of the high order st_mode is encoded:

#define S_IFDIR 0x4000 /* directory */ #define S_IFIFO 0x1000 /* FIFO special */ #define S_IFCHR 0x2000 /* character special */ #define S_IFBLK 0x3000 /* block special */ #define S_IFREG 0x8000 /* or just 0x0000, regular */ 

From the comment, it seems that nybble could be 0 or 8 to represent a “regular file”.

So this asks the question: in what circumstances is it 0, not 8? If I defined these codes, I would have reserved 0 for failure unknown / undefined / invalid / not-a-file or something like that.

Indeed, the S_ISREG macro:

 #define S_ISREG(m) ((m) & S_IFREG) 

It seems to me that a regular file should always be expected to code 8 (and 0 would be aberration?).

Was it a valid assumption to interpret 0 as an unknown or invalid file and ignore the comment "or only 0x0000" and always expect 8 to be used for all regular files?

+4
source share
2 answers

Most sources indicate that S_ISREG checks are sufficient; I'm not sure when you will see 0x0000 as a “regular” file.

I believe some older implementations use 0x0000 (the really old header in the DJGPP header includes this), but this is the only real link I can find. Everything else points to 0x8000.

Basically, use the S_ISREG macro and hope that the header on what you are compiling against does the right thing.

+1
source

I would trust the definitions of S_IFREG and S_ISREG. I have never worked with a file system that violates these macros.

I assume that the definition of 0x0000 for a regular file is to handle legacy file systems that could use a different encoding of the file type information. What OS and file system are you using?

0
source

Source: https://habr.com/ru/post/1286726/


All Articles