2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: misc.c,v 1.23 2003/10/28 09:08:06 markus Exp $");
32 /* remove newline at end of string */
38 if (*t == '\n' || *t == '\r') {
48 /* set/unset filedescriptor to non-blocking */
54 val = fcntl(fd, F_GETFL, 0);
56 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
59 if (val & O_NONBLOCK) {
60 debug2("fd %d is O_NONBLOCK", fd);
63 debug2("fd %d setting O_NONBLOCK", fd);
65 if (fcntl(fd, F_SETFL, val) == -1)
66 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
71 unset_nonblock(int fd)
75 val = fcntl(fd, F_GETFL, 0);
77 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
80 if (!(val & O_NONBLOCK)) {
81 debug2("fd %d is not O_NONBLOCK", fd);
84 debug("fd %d clearing O_NONBLOCK", fd);
86 if (fcntl(fd, F_SETFL, val) == -1)
87 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
91 /* disable nagle on socket */
99 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
100 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
104 debug2("fd %d is TCP_NODELAY", fd);
108 debug2("fd %d setting TCP_NODELAY", fd);
109 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
110 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
113 /* Characters considered whitespace in strsep calls. */
114 #define WHITESPACE " \t\r\n"
116 /* return next token in configuration line */
128 *s = strpbrk(*s, WHITESPACE "=");
132 /* Allow only one '=' to be skipped */
137 *s += strspn(*s + 1, WHITESPACE) + 1;
138 if (*s[0] == '=' && !wspace)
139 *s += strspn(*s + 1, WHITESPACE) + 1;
145 pwcopy(struct passwd *pw)
147 struct passwd *copy = xmalloc(sizeof(*copy));
149 memset(copy, 0, sizeof(*copy));
150 copy->pw_name = xstrdup(pw->pw_name);
151 copy->pw_passwd = xstrdup(pw->pw_passwd);
152 copy->pw_gecos = xstrdup(pw->pw_gecos);
153 copy->pw_uid = pw->pw_uid;
154 copy->pw_gid = pw->pw_gid;
155 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
156 copy->pw_expire = pw->pw_expire;
158 #ifdef HAVE_PW_CHANGE_IN_PASSWD
159 copy->pw_change = pw->pw_change;
161 #ifdef HAVE_PW_CLASS_IN_PASSWD
162 copy->pw_class = xstrdup(pw->pw_class);
164 copy->pw_dir = xstrdup(pw->pw_dir);
165 copy->pw_shell = xstrdup(pw->pw_shell);
170 * Convert ASCII string to TCP/IP port number.
171 * Port must be >0 and <=65535.
172 * Return 0 if invalid.
175 a2port(const char *s)
181 port = strtol(s, &endp, 0);
182 if (s == endp || *endp != '\0' ||
183 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
184 port <= 0 || port > 65535)
191 #define MINUTES (SECONDS * 60)
192 #define HOURS (MINUTES * 60)
193 #define DAYS (HOURS * 24)
194 #define WEEKS (DAYS * 7)
197 * Convert a time string into seconds; format is
201 * Valid time qualifiers are:
215 * Return -1 if time string is invalid.
218 convtime(const char *s)
228 if (p == NULL || *p == '\0')
232 secs = strtol(p, &endp, 10);
234 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
273 cleanhostname(char *host)
275 if (*host == '[' && host[strlen(host) - 1] == ']') {
276 host[strlen(host) - 1] = '\0';
287 if (*cp == ':') /* Leading colon is part of file name. */
293 if (*cp == '@' && *(cp+1) == '[')
295 if (*cp == ']' && *(cp+1) == ':' && flag)
297 if (*cp == ':' && !flag)
305 /* function to assist building execv() arguments */
307 addargs(arglist *args, char *fmt, ...)
314 vsnprintf(buf, sizeof(buf), fmt, ap);
317 nalloc = args->nalloc;
318 if (args->list == NULL) {
321 } else if (args->num+2 >= nalloc)
324 args->list = xrealloc(args->list, nalloc * sizeof(char *));
325 args->nalloc = nalloc;
326 args->list[args->num++] = xstrdup(buf);
327 args->list[args->num] = NULL;