2 * Copyright (c) 1999-2005, 2007, 2009-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
25 #include <sys/param.h>
28 # include <sys/file.h>
29 #endif /* HAVE_FLOCK */
34 # include "compat/stdbool.h"
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_STRINGS_H */
46 #endif /* HAVE_UNISTD_H */
48 #if TIME_WITH_SYS_TIME
51 #ifndef HAVE_STRUCT_TIMESPEC
52 # include "compat/timespec.h"
57 #include "sudo_debug.h"
60 # define LINE_MAX 2048
64 * Update the access and modify times on an fd or file.
67 touch(int fd, char *path, struct timeval *tvp)
69 struct timeval times[2];
71 debug_decl(touch, SUDO_DEBUG_UTIL)
74 times[0].tv_sec = times[1].tv_sec = tvp->tv_sec;
75 times[0].tv_usec = times[1].tv_usec = tvp->tv_usec;
78 #if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES)
80 rval = futimes(fd, tvp ? times : NULL);
84 rval = utimes(path, tvp ? times : NULL);
85 debug_return_int(rval);
93 lock_file(int fd, int lockit)
96 debug_decl(lock_file, SUDO_DEBUG_UTIL)
109 debug_return_bool(lockf(fd, op, 0) == 0);
113 lock_file(int fd, int lockit)
116 debug_decl(lock_file, SUDO_DEBUG_UTIL)
123 op = LOCK_EX | LOCK_NB;
129 debug_return_bool(flock(fd, op) == 0);
133 lock_file(int fd, int lockit)
138 debug_decl(lock_file, SUDO_DEBUG_UTIL)
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_LOCK) ? F_SETLKW : F_SETLK;
147 debug_return_bool(fcntl(fd, func, &lock) == 0);
155 * Read a line of input, remove comments and strip off leading
156 * and trailing spaces. Returns static storage that is reused.
159 sudo_parseln(FILE *fp)
163 static char buf[LINE_MAX];
164 debug_decl(sudo_parseln, SUDO_DEBUG_UTIL)
166 if (fgets(buf, sizeof(buf), fp) != NULL) {
167 /* Remove comments */
168 if ((cp = strchr(buf, '#')) != NULL)
171 /* Trim leading and trailing whitespace/newline */
173 while (len > 0 && isspace((unsigned char)buf[len - 1]))
175 for (cp = buf; isblank((unsigned char)*cp); cp++)
178 debug_return_str(cp);