1 /*
2  * Copyright (c) 2011 QUALCOMM Incorporated.  All rights reserved.
3  * The file License.txt specifies the terms for use, modification,
4  * and redistribution.
5  */
6 
7 
8 /*
9  * Copyright (c) 1989 Mark Davies
10  * Copyright (c) 1990 Andy Linton
11  * Copyright (c) 1990 Victoria University of Wellington.
12  *
13  * Redistribution and use in source and binary forms are permitted provided
14  * that: (1) source distributions retain this entire copyright notice and
15  * comment, and (2) distributions including binaries display the following
16  * acknowledgement:  ``This product includes software developed by the
17  * Victoria University of Wellington, New Zealand and its contributors''
18  * in the documentation or other materials provided with the distribution
19  * and in all advertising materials mentioning features or use of this
20  * software.
21  * Neither the name of the University nor the names of its contributors may
22  * be used to endorse or promote products derived from this software without
23  * specific prior written permission.
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  */
28 
29 
30 #include "config.h"
31 
32 #ifndef HAVE_FLOCK /*  defined(SYSV) || defined(ISC) */
33 
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <errno.h>
37 #ifdef ISC
38 #  include <net/errno.h>
39 #endif
40 #include <flock.h>               /* defines normally in <sys/file.h> */
41 
42 
43 #if defined(F_SETLK) && defined(F_SETLKW)
44 
45 int
flock(fd,operation)46 flock(fd, operation)
47 int fd, operation;
48 {
49     int op, ret;
50     struct flock arg;
51     extern int errno;
52 
53     op = (LOCK_NB & operation) ? F_SETLK : F_SETLKW;
54 
55     arg.l_type = (LOCK_EX & operation) ? F_WRLCK :
56         (LOCK_SH & operation) ? F_RDLCK : F_UNLCK;
57     arg.l_whence = arg.l_start = arg.l_len = 0;
58 
59     if ((ret = fcntl(fd, op, &arg)) == -1) {
60         if (errno == EACCES || errno == EAGAIN)
61             errno = EWOULDBLOCK;
62     }
63     return (ret);
64 }
65 
66 #else
67 
68 int
flock(fd,operation)69 flock(fd, operation)
70 int fd, operation;
71 {
72     int op, ret;
73     extern int errno;
74 
75     op = (LOCK_UN & operation) ? F_ULOCK :
76       (LOCK_NB & operation) ? F_TLOCK : F_LOCK;
77 
78     if ((ret = lockf(fd, op, 0)) == -1) {
79         if (errno == EACCES || errno == EAGAIN)
80             errno = EWOULDBLOCK;
81     }
82 
83     return (ret);
84 }
85 
86 #endif /* F_SETLK && F_SETLKW */
87 
88 #endif /* HAVE_FLOCK */
89 
90