DT_DIR undefined
Asked Answered
A

1

6

I want to check if file returned by readdir is directory. I tried do it using DT_DIR constant (as man readdir says) but it's undefined. What file should I include to get it?

Now I use

#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>

gcc version is 4.6.1

Compilation string:

gcc a.c --std=c99 -Wall
Awl answered 11/2, 2012 at 15:49 Comment(0)
R
10

You need to have the _BSD_SOURCE feature test macro defined to get those defines, they are not standard, and GCC does not define that macro when compiling for C99.

gcc -std=c99 -D_BSD_SOURCE -Wall a.c
Rrhoea answered 11/2, 2012 at 15:58 Comment(4)
Hm, thanks it works. But I guess it isn't always OK. to change this constant, because it may change something else. Is it way to do it other way? Are my own constants OK(i really don't think so) or they may be changed on other OS? (i.e 4 for me with directories)Awl
PS: -D_BSD_SOURCE is enough, we needn't value hereAwl
Using -D_BSD_SOURCE doesn't change any constants. It pulls in additional defines and functions that come from BSD and are not POSIX or standard C. If your code requires that, then it is not portable. If you want it to be portable, don't rely on the d_type field of struct dirent, use stat instead. Hardcoding 4 is a bad idea.Rrhoea
Since version 2.19 of glibc the macro _DEFAULT_SOURCE should be used instead of _BSD_SOURCE.Esmerolda

© 2022 - 2024 — McMap. All rights reserved.