13

fcntl() uses struct flock structure to define and check file locks. Unfortunately, on different Unix systems the fields in this structure are in different order. Does anybody know how one could check for it with autoconf or at least check if the structure is in specific format (e.g. the question would be - does the struct format matches the Linux format)?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
StasM
  • 10,593
  • 6
  • 56
  • 103

1 Answers1

3

You can use this autoconf macro to find if a certain member of struct flock exists:

AC_CHECK_MEMBERS([struct flock.l_type],[],[],[[#include <fcntl.h>]])

Github has a variety of autoconf files you can look at for additional ideas by searching for "struct flock" in *.ac files.

Update: The problem of struct flock order is discussed in an old post on the debian-bugs list.

We could take inspiration from that bug and do this in configure:

AC_MSG_CHECKING("whether flock struct is linux ordered or not")
AC_TRY_RUN([
  #include <fcntl.h>
  struct flock lock = { 1, 2, 3, 4, 5 };
  int main() { return lock.l_type==1 ? 0 : 1; }
], [
    AC_DEFINE(HAVE_FLOCK_LINUX) 
    AC_MSG_RESULT("yes")
], AC_MSG_RESULT("no") )

You can also do this check in your program at runtime, it doesn't have to be a configure step.

patrickmdnet
  • 3,332
  • 1
  • 29
  • 34
  • The problem is that, for example, Linux and *BSD both have l_type but in different place in the struct. I need to figure out which one I have. E.g. on linux l_type is first, and on *BSD it's fourth. – StasM Mar 25 '13 at 03:06
  • With the update, I like the approach, I'll try it and see if it works out. – StasM Mar 25 '13 at 22:43
  • As an alternate approach, you could use the `offsetof` facility to find the offset of `l_type` in the struct. – ldav1s Mar 26 '13 at 18:38