1 /* $OpenBSD: misc.c,v 1.18 2002/03/04 13:10:46 markus Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * 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.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 RCSID("$OpenBSD: misc.c,v 1.18 2002/03/04 13:10:46 markus Exp $");
34 /* remove newline at end of string */
40 if (*t == '\n' || *t == '\r') {
50 /* set/unset filedescriptor to non-blocking */
56 val = fcntl(fd, F_GETFL, 0);
58 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
61 if (val & O_NONBLOCK) {
62 debug2("fd %d is O_NONBLOCK", fd);
65 debug("fd %d setting O_NONBLOCK", fd);
67 if (fcntl(fd, F_SETFL, val) == -1)
68 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
73 unset_nonblock(int fd)
77 val = fcntl(fd, F_GETFL, 0);
79 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
82 if (!(val & O_NONBLOCK)) {
83 debug2("fd %d is not O_NONBLOCK", fd);
86 debug("fd %d clearing O_NONBLOCK", fd);
88 if (fcntl(fd, F_SETFL, val) == -1)
89 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
93 /* disable nagle on socket */
101 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
102 error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
106 debug2("fd %d is TCP_NODELAY", fd);
110 debug("fd %d setting TCP_NODELAY", fd);
111 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
112 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
115 /* Characters considered whitespace in strsep calls. */
116 #define WHITESPACE " \t\r\n"
118 /* return next token in configuration line */
130 *s = strpbrk(*s, WHITESPACE "=");
134 /* Allow only one '=' to be skipped */
139 *s += strspn(*s + 1, WHITESPACE) + 1;
140 if (*s[0] == '=' && !wspace)
141 *s += strspn(*s + 1, WHITESPACE) + 1;
147 pwcopy(struct passwd *pw)
149 struct passwd *copy = xmalloc(sizeof(*copy));
151 memset(copy, 0, sizeof(*copy));
152 copy->pw_name = xstrdup(pw->pw_name);
153 copy->pw_passwd = xstrdup(pw->pw_passwd);
154 copy->pw_gecos = xstrdup(pw->pw_gecos);
155 copy->pw_uid = pw->pw_uid;
156 copy->pw_gid = pw->pw_gid;
157 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
158 copy->pw_expire = pw->pw_expire;
160 #ifdef HAVE_PW_CHANGE_IN_PASSWD
161 copy->pw_change = pw->pw_change;
163 #ifdef HAVE_PW_CLASS_IN_PASSWD
164 copy->pw_class = xstrdup(pw->pw_class);
166 copy->pw_dir = xstrdup(pw->pw_dir);
167 copy->pw_shell = xstrdup(pw->pw_shell);
172 * Convert ASCII string to TCP/IP port number.
173 * Port must be >0 and <=65535.
174 * Return 0 if invalid.
177 a2port(const char *s)
183 port = strtol(s, &endp, 0);
184 if (s == endp || *endp != '\0' ||
185 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
186 port <= 0 || port > 65535)
193 #define MINUTES (SECONDS * 60)
194 #define HOURS (MINUTES * 60)
195 #define DAYS (HOURS * 24)
196 #define WEEKS (DAYS * 7)
199 * Convert a time string into seconds; format is
203 * Valid time qualifiers are:
217 * Return -1 if time string is invalid.
220 convtime(const char *s)
230 if (p == NULL || *p == '\0')
234 secs = strtol(p, &endp, 10);
236 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
275 cleanhostname(char *host)
277 if (*host == '[' && host[strlen(host) - 1] == ']') {
278 host[strlen(host) - 1] = '\0';
289 if (*cp == ':') /* Leading colon is part of file name. */
295 if (*cp == '@' && *(cp+1) == '[')
297 if (*cp == ']' && *(cp+1) == ':' && flag)
299 if (*cp == ':' && !flag)
307 /* function to assist building execv() arguments */
309 addargs(arglist *args, char *fmt, ...)
315 vsnprintf(buf, sizeof(buf), fmt, ap);
318 if (args->list == NULL) {
321 } else if (args->num+2 >= args->nalloc)
324 args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
325 args->list[args->num++] = xstrdup(buf);
326 args->list[args->num] = NULL;
330 mysignal(int sig, mysig_t act)
332 #ifdef HAVE_SIGACTION
333 struct sigaction sa, osa;
335 if (sigaction(sig, NULL, &osa) == -1)
337 if (osa.sa_handler != act) {
338 memset(&sa, 0, sizeof(sa));
339 sigemptyset(&sa.sa_mask);
341 #if defined(SA_INTERRUPT)
343 sa.sa_flags |= SA_INTERRUPT;
346 if (sigaction(sig, &sa, NULL) == -1)
349 return (osa.sa_handler);
351 return (signal(sig, act));