*** empty log message ***
[krb5.git] / src / lib / krb5 / os / lock_file.c
1 /*
2  * $Source$
3  * $Author$
4  *
5  * Copyright 1990 by the Massachusetts Institute of Technology.
6  *
7  * For copying and distribution information, please see the file
8  * <krb5/mit-copyright.h>.
9  *
10  * libos: krb5_lock_file routine
11  */
12
13 #ifndef lint
14 static char rcsid_lock_file_c [] =
15 "$Id$";
16 #endif  lint
17
18 #include <krb5/copyright.h>
19
20 #include <krb5/krb5.h>
21 #include <krb5/krb5_err.h>
22 #include <krb5/libos.h>
23
24 #include <stdio.h>
25 #include <sys/file.h>
26
27 extern int errno;
28
29 krb5_error_code
30 krb5_lock_file(filep, mode)
31 FILE *filep;
32 int mode;
33 {
34     int flock_flag = -1;
35
36     switch (mode & ~KRB5_LOCKMODE_DONTBLOCK) {
37     case KRB5_LOCKMODE_SHARED:
38         flock_flag = LOCK_SH;
39     case KRB5_LOCKMODE_EXCLUSIVE:
40         flock_flag = LOCK_EX;
41     case KRB5_LOCKMODE_UNLOCK:
42         flock_flag = LOCK_UN;
43     }
44     if (flock_flag == -1)
45         return(KRB5_LIBOS_BADLOCKFLAG);
46     if (mode & KRB5_LOCKMODE_DONTBLOCK)
47         flock_flag |= LOCK_NB;
48
49     if (flock(fileno(filep), flock_flag) == -1)
50         return(errno);
51     return 0;
52 }