00001 #include "ruby/config.h"
00002
00003 #if defined _WIN32
00004 #elif defined HAVE_FCNTL && defined HAVE_FCNTL_H
00005
00006
00007
00008
00009 # ifndef LOCK_SH
00010 # define LOCK_SH 1
00011 # endif
00012 # ifndef LOCK_EX
00013 # define LOCK_EX 2
00014 # endif
00015 # ifndef LOCK_NB
00016 # define LOCK_NB 4
00017 # endif
00018 # ifndef LOCK_UN
00019 # define LOCK_UN 8
00020 # endif
00021
00022 #include <fcntl.h>
00023 #include <unistd.h>
00024 #include <errno.h>
00025
00026 int
00027 flock(int fd, int operation)
00028 {
00029 struct flock lock;
00030
00031 switch (operation & ~LOCK_NB) {
00032 case LOCK_SH:
00033 lock.l_type = F_RDLCK;
00034 break;
00035 case LOCK_EX:
00036 lock.l_type = F_WRLCK;
00037 break;
00038 case LOCK_UN:
00039 lock.l_type = F_UNLCK;
00040 break;
00041 default:
00042 errno = EINVAL;
00043 return -1;
00044 }
00045 lock.l_whence = SEEK_SET;
00046 lock.l_start = lock.l_len = 0L;
00047
00048 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
00049 }
00050
00051 #elif defined(HAVE_LOCKF)
00052
00053 #include <unistd.h>
00054 #include <errno.h>
00055
00056
00057
00058
00059
00060
00061
00062 # ifndef F_ULOCK
00063 # define F_ULOCK 0
00064 # endif
00065 # ifndef F_LOCK
00066 # define F_LOCK 1
00067 # endif
00068 # ifndef F_TLOCK
00069 # define F_TLOCK 2
00070 # endif
00071 # ifndef F_TEST
00072 # define F_TEST 3
00073 # endif
00074
00075
00076
00077
00078 # ifndef LOCK_SH
00079 # define LOCK_SH 1
00080 # endif
00081 # ifndef LOCK_EX
00082 # define LOCK_EX 2
00083 # endif
00084 # ifndef LOCK_NB
00085 # define LOCK_NB 4
00086 # endif
00087 # ifndef LOCK_UN
00088 # define LOCK_UN 8
00089 # endif
00090
00091 int
00092 flock(int fd, int operation)
00093 {
00094 switch (operation) {
00095
00096
00097 case LOCK_SH:
00098 rb_notimplement();
00099 return -1;
00100
00101 case LOCK_EX:
00102 return lockf (fd, F_LOCK, 0);
00103
00104
00105 case LOCK_SH|LOCK_NB:
00106 rb_notimplement();
00107 return -1;
00108
00109 case LOCK_EX|LOCK_NB:
00110 return lockf (fd, F_TLOCK, 0);
00111
00112
00113 case LOCK_UN:
00114 return lockf (fd, F_ULOCK, 0);
00115
00116
00117 default:
00118 errno = EINVAL;
00119 return -1;
00120 }
00121 }
00122 #else
00123 int
00124 flock(int fd, int operation)
00125 {
00126 rb_notimplement();
00127 return -1;
00128 }
00129 #endif
00130