Revert "dtudcfonted, dtudcexch: delete from repository"
[oweals/cde.git] / cde / programs / dtudcfonted / libfuty / lock.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: lock.c /main/5 1996/11/08 02:07:28 cde-fuj $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  */
30
31
32 #include        <stdio.h>
33 #include        <errno.h>
34 #include        <sys/types.h>
35 #include        <fcntl.h>
36 #include        <unistd.h>
37 #include        "udcutil.h"
38
39 /*
40  * FileLock( fd )
41  * fd:  file descripter
42  *
43  * lock a file by a writing mode
44  *
45  * normal end : 0
46  * abnormal end : -1
47  *
48  */
49
50 int
51 #if NeedFunctionPrototypes
52 FileLock( int fd )
53 #else
54 FileLock( fd )
55 int     fd;             /* a file descripter */
56 #endif
57 {
58
59         struct flock    flpar;
60
61
62         flpar.l_type = F_RDLCK;
63         flpar.l_start = 0;
64         flpar.l_len = 0;
65         flpar.l_whence = 0;
66
67         if ( fcntl( fd, F_SETLK, &flpar ) == -1 ) {
68                 USAGE("Write lock not success \n" );
69                 return  -1;
70         }
71
72         return  0;
73 }
74
75
76 /*
77  * FileUnLock( fd )
78  *
79  * free a file by a writing mode
80  *
81  * normal end : 0
82  * abnormal end : -1
83  */
84
85 int
86 #if NeedFunctionPrototypes
87 FileUnLock( int fd )
88 #else
89 FileUnLock( fd )
90 int     fd;     /* a file descripter */
91 #endif
92 {
93         struct flock    flpar;
94
95
96         flpar.l_type = F_UNLCK;
97         flpar.l_start = 0;
98         flpar.l_len = 0;
99         flpar.l_whence = 0;
100
101         if ( fcntl( fd, F_SETLK, &flpar ) == -1 ){
102                 USAGE("File unlock not success \n" );
103                 return  -1;
104         }
105         return  0;
106 }
107
108 /*
109  * isLock( fd )
110  *
111  * search whether file is locked by a writing mode
112  *
113  * 1 : file is locked by a writing mode
114  * 0 : file isn't locked by a writing mode
115  */
116
117 int
118 #if NeedFunctionPrototypes
119 isLock( int fd )
120 #else
121 isLock( fd )
122 int     fd;     /* file descripter */
123 #endif
124 {
125         struct flock    flpar;
126
127
128         flpar.l_type = F_WRLCK;
129         flpar.l_start = 0;
130         flpar.l_len = 0;
131         flpar.l_whence = 0;
132
133         if ( fcntl( fd, F_GETLK, &flpar ) == -1 ) {
134                 USAGE("Inquiry of file lock not sucess \n" );
135                 return  -1;
136         }
137
138         if ( flpar.l_type == F_UNLCK ){
139                 return  0 ;
140         } else {
141                 return  1 ;
142         }
143 }