Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S_ISREG macro undefined

Tags:

c

curl

macros

Questions

  • Are the posix macros S_ISREG, S_ISDIR etc linux only? I need to find out because i am trying to compile CURL and it is trying to use them on windows
  • What include file can i use to access them on windows.

This is the offending code

/*we ignore file size for char/block devices, sockets etc*/
if(S_ISREG(fileinfo.st_mode))
   uploadfilesize= fileinfo.st_size;
}

and it causes an error

error LNK2019: unresolved external symbol _S_ISREG referenced in function _operate file tool_operate.obj

They are referenced in the following questions

  • How to use S_ISREG() and S_ISDIR() POSIX Macros?
  • Differentiate between a unix directory and file in C and C++
  • Problem reading directories in C

Apparently S_ISREG() is part of a bunch of posix macros and is apparently supposed to tell us if a file is a "regular file" but all the examples I found had linux specific include files.

like image 723
Dr Deo Avatar asked Jun 28 '12 06:06

Dr Deo


3 Answers

Currently curl 7.21.5 defines in setup.h this:

#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
like image 81
Oliver Zendel Avatar answered Nov 15 '22 10:11

Oliver Zendel


After having inspected Microsoft's sys/stat.h, I found that the following modification of @OliverZendel's answer worked for me, with Visual Studio 2017, and hopefully on other compilers as well:

// Windows does not define the S_ISREG and S_ISDIR macros in stat.h, so we do.
// We have to define _CRT_INTERNAL_NONSTDC_NAMES 1 before #including sys/stat.h
// in order for Microsoft's stat.h to define names like S_IFMT, S_IFREG, and S_IFDIR,
// rather than just defining  _S_IFMT, _S_IFREG, and _S_IFDIR as it normally does.
#define _CRT_INTERNAL_NONSTDC_NAMES 1
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
  #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
  #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
like image 5
Some Guy Avatar answered Nov 15 '22 10:11

Some Guy


No such thing on windows, you can use the FindFirstFile, FindNextFile win32 api, the return structure contains something similar but not the same.

If you use gcc/mingw library they have a stat() simulation. You need to include sys/stat.h for that macro.

like image 3
pizza Avatar answered Nov 15 '22 10:11

pizza