Welcome to mirror list, hosted at ThFree Co, Russian Federation.

flock.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b15962d94dc7ebc0e602190ab5af170f92003fb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* One of many ways to emulate flock() on top of real (good) POSIX locks.
 *
 * This flock() emulation is based upon source taken from the Red Hat
 * implementation used in their imap-2002d SRPM.
 *
 * $RH: flock.c,v 1.2 2000/08/23 17:07:00 nalin Exp $
 */
/* flock.c

   Copyright 2003 Red Hat, Inc.

   This file is part of Cygwin.

   This software is a copyrighted work licensed under the terms of the
   Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
   details. */

#include "winsup.h"
#include "cygerrno.h"
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>

int
flock (int fd, int operation)
{
  int i, cmd;
  struct __flock64 l = { 0, 0, 0, 0, 0 };
  if (operation & LOCK_NB)
    {
      cmd = F_SETLK;
    }
  else
    {
      cmd = F_SETLKW;
    }
  l.l_whence = SEEK_SET;
  switch (operation & (~LOCK_NB))
    {
    case LOCK_EX:
      l.l_type = F_WRLCK;
      i = fcntl_worker (fd, cmd, &l);
      if (i == -1)
	{
	  if ((get_errno () == EAGAIN) || (get_errno () == EACCES))
	    {
	      set_errno (EWOULDBLOCK);
	    }
	}
      break;
    case LOCK_SH:
      l.l_type = F_RDLCK;
      i = fcntl_worker (fd, cmd, &l);
      if (i == -1)
	{
	  if ((get_errno () == EAGAIN) || (get_errno () == EACCES))
	    {
	      set_errno (EWOULDBLOCK);
	    }
	}
      break;
    case LOCK_UN:
      l.l_type = F_UNLCK;
      i = fcntl_worker (fd, cmd, &l);
      if (i == -1)
	{
	  if ((get_errno () == EAGAIN) || (get_errno () == EACCES))
	    {
	      set_errno (EWOULDBLOCK);
	    }
	}
      break;
    default:
      i = -1;
      set_errno (EINVAL);
      break;
    }
  return i;
}

#ifdef FLOCK_EMULATE_IS_MAIN
int
main (int argc, char **argv)
{
  int fd = open (argv[1], O_WRONLY);
  flock (fd, LOCK_EX);
  return 0;
}
#endif