Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check whether this is directory-path or any filename-path?

by this

Why does fopen("any_path_name",'r') not give NULL as return?

i get to know that in linux directories and files are considered to be file. so when i give any directory-path or file-path in fopen with read mode it doesnt give NULL file descriptor and?

so how can i check that whether it is dirctory path or file-path ? if i am getting some path from command argument?

like image 245
Jeegar Patel Avatar asked Dec 27 '22 09:12

Jeegar Patel


2 Answers

man 2 stat:

NAME
     fstat, fstat64, lstat, lstat64, stat, stat64 -- get file status

...

     struct stat {
         dev_t           st_dev;           /* ID of device containing file */
         mode_t          st_mode;          /* Mode of file (see below) */

...

     The status information word st_mode has the following bits:

...

     #define        S_IFDIR  0040000  /* directory */
like image 183
zed_0xff Avatar answered Jan 04 '23 08:01

zed_0xff


You can use S_ISDIR macro .

like image 38
Igor Avatar answered Jan 04 '23 10:01

Igor