2 * Copyright (c) 1999, 2001 Todd C. Miller <Todd.Miller@courtesan.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * 4. Products derived from this software may not be called "Sudo" nor
20 * may "Sudo" appear in their names without specific prior written
21 * permission from the author.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
38 #include <sys/param.h>
40 # include <sys/file.h>
41 #endif /* HAVE_FLOCK */
45 #endif /* HAVE_UNISTD_H */
51 # endif /* HAVE_UTIME_H */
53 # include "emul/utime.h"
54 #endif /* HAVE_UTIME */
59 static const char rcsid[] = "$Sudo: fileops.c,v 1.3 2001/12/14 19:52:47 millert Exp $";
63 * Update the access and modify times on a file.
70 #ifdef HAVE_UTIME_POSIX
71 struct utimbuf ut, *utp;
73 ut.actime = ut.modtime = when;
76 /* BSD <= 4.3 has no struct utimbuf */
79 utp[0] = utp[1] = when;
80 #endif /* HAVE_UTIME_POSIX */
82 return(utime(path, utp));
107 return(lockf(fd, op, 0) == 0);
111 lock_file(fd, lockit)
122 op = LOCK_EX | LOCK_NB;
128 return(flock(fd, op) == 0);
132 lock_file(fd, lockit)
142 lock.l_pid = getpid();
143 lock.l_type = (lockit == SUDO_UNLOCK) ? F_UNLCK : F_WRLCK;
144 lock.l_whence = SEEK_SET;
145 func = (lockit == SUDO_TLOCK) ? F_SETLK : F_SETLKW;
147 return(fcntl(fd, func, &lock) == 0);