blob: b0fdbe03cb77fb672e0fb2e7bbda17d49dfca205 [file] [log] [blame]
Ben Lindstromc93e84c2001-05-12 00:08:37 +00001/* $OpenBSD: misc.c,v 1.8 2001/05/11 14:59:56 markus Exp $ */
Damien Millere4340be2000-09-16 13:29:08 +11002
3/*
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
25 */
26
Damien Miller61c51502000-08-18 14:01:04 +100027#include "includes.h"
Ben Lindstromc93e84c2001-05-12 00:08:37 +000028RCSID("$OpenBSD: misc.c,v 1.8 2001/05/11 14:59:56 markus Exp $");
Damien Miller61c51502000-08-18 14:01:04 +100029
Kevin Stevesb6e773a2001-02-04 13:20:36 +000030#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000032#include "xmalloc.h"
Damien Miller61c51502000-08-18 14:01:04 +100033
34char *
35chop(char *s)
36{
37 char *t = s;
38 while (*t) {
39 if(*t == '\n' || *t == '\r') {
40 *t = '\0';
41 return s;
42 }
43 t++;
44 }
45 return s;
46
47}
48
49void
50set_nonblock(int fd)
51{
52 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000053
Damien Miller61c51502000-08-18 14:01:04 +100054 val = fcntl(fd, F_GETFL, 0);
55 if (val < 0) {
56 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
57 return;
58 }
Damien Miller69b69aa2000-10-28 14:19:58 +110059 if (val & O_NONBLOCK) {
Ben Lindstromc93e84c2001-05-12 00:08:37 +000060 debug2("fd %d is O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100061 return;
Damien Miller69b69aa2000-10-28 14:19:58 +110062 }
Damien Miller61c51502000-08-18 14:01:04 +100063 debug("fd %d setting O_NONBLOCK", fd);
64 val |= O_NONBLOCK;
65 if (fcntl(fd, F_SETFL, val) == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +110066 if (errno != ENODEV)
67 error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
68 fd, strerror(errno));
Damien Miller61c51502000-08-18 14:01:04 +100069}
70
Ben Lindstromc93e84c2001-05-12 00:08:37 +000071void
72unset_nonblock(int fd)
73{
74 int val;
75
76 val = fcntl(fd, F_GETFL, 0);
77 if (val < 0) {
78 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
79 return;
80 }
81 if (!(val & O_NONBLOCK)) {
82 debug2("fd %d is not O_NONBLOCK", fd);
83 return;
84 }
85 debug("fd %d setting O_NONBLOCK", fd);
86 val &= ~O_NONBLOCK;
87 if (fcntl(fd, F_SETFL, val) == -1)
88 if (errno != ENODEV)
89 error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
90 fd, strerror(errno));
91}
92
Damien Miller61c51502000-08-18 14:01:04 +100093/* Characters considered whitespace in strsep calls. */
94#define WHITESPACE " \t\r\n"
95
96char *
97strdelim(char **s)
98{
99 char *old;
100 int wspace = 0;
101
102 if (*s == NULL)
103 return NULL;
104
105 old = *s;
106
107 *s = strpbrk(*s, WHITESPACE "=");
108 if (*s == NULL)
109 return (old);
110
111 /* Allow only one '=' to be skipped */
112 if (*s[0] == '=')
113 wspace = 1;
114 *s[0] = '\0';
115
116 *s += strspn(*s + 1, WHITESPACE) + 1;
117 if (*s[0] == '=' && !wspace)
118 *s += strspn(*s + 1, WHITESPACE) + 1;
119
120 return (old);
121}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000122
Ben Lindstrom086cf212001-03-05 05:56:40 +0000123struct passwd *
124pwcopy(struct passwd *pw)
125{
126 struct passwd *copy = xmalloc(sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000127
Ben Lindstrom086cf212001-03-05 05:56:40 +0000128 memset(copy, 0, sizeof(*copy));
129 copy->pw_name = xstrdup(pw->pw_name);
130 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000131 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000132 copy->pw_uid = pw->pw_uid;
133 copy->pw_gid = pw->pw_gid;
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000134#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000135 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000136#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000137 copy->pw_dir = xstrdup(pw->pw_dir);
138 copy->pw_shell = xstrdup(pw->pw_shell);
139 return copy;
140}
141
Ben Lindstrom19066a12001-04-12 23:39:26 +0000142int a2port(const char *s)
143{
144 long port;
145 char *endp;
146
147 errno = 0;
148 port = strtol(s, &endp, 0);
149 if (s == endp || *endp != '\0' ||
150 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
151 port <= 0 || port > 65535)
152 return 0;
153
154 return port;
155}
156
Ben Lindstrom4529b702001-05-03 23:39:53 +0000157char *
158cleanhostname(char *host)
159{
160 if (*host == '[' && host[strlen(host) - 1] == ']') {
161 host[strlen(host) - 1] = '\0';
162 return (host + 1);
163 } else
164 return host;
165}
166
167char *
168colon(char *cp)
169{
170 int flag = 0;
171
172 if (*cp == ':') /* Leading colon is part of file name. */
173 return (0);
174 if (*cp == '[')
175 flag = 1;
176
177 for (; *cp; ++cp) {
178 if (*cp == '@' && *(cp+1) == '[')
179 flag = 1;
180 if (*cp == ']' && *(cp+1) == ':' && flag)
181 return (cp+1);
182 if (*cp == ':' && !flag)
183 return (cp);
184 if (*cp == '/')
185 return (0);
186 }
187 return (0);
188}
189
Ben Lindstrom387c4722001-05-08 20:27:25 +0000190void
191addargs(arglist *args, char *fmt, ...)
192{
193 va_list ap;
194 char buf[1024];
195
196 va_start(ap, fmt);
197 vsnprintf(buf, sizeof(buf), fmt, ap);
198 va_end(ap);
199
200 if (args->list == NULL) {
201 args->nalloc = 32;
202 args->num = 0;
203 } else if (args->num+2 >= args->nalloc)
204 args->nalloc *= 2;
205
206 args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
207 args->list[args->num++] = xstrdup(buf);
208 args->list[args->num] = NULL;
209}
210
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000211mysig_t
212mysignal(int sig, mysig_t act)
213{
214#ifdef HAVE_SIGACTION
215 struct sigaction sa, osa;
216
Kevin Stevese74ebd02001-02-17 17:10:16 +0000217 if (sigaction(sig, NULL, &osa) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000218 return (mysig_t) -1;
219 if (osa.sa_handler != act) {
Kevin Stevese74ebd02001-02-17 17:10:16 +0000220 memset(&sa, 0, sizeof(sa));
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000221 sigemptyset(&sa.sa_mask);
222 sa.sa_flags = 0;
Kevin Steveseff26f22001-02-18 03:42:02 +0000223#if defined(SA_RESTART)
Damien Miller722ccb12001-02-18 15:18:43 +1100224 if (sig == SIGCHLD)
Kevin Steves799bed82001-02-16 14:58:12 +0000225 sa.sa_flags |= SA_RESTART;
Damien Miller722ccb12001-02-18 15:18:43 +1100226#endif
227#if defined(SA_INTERRUPT)
228 if (sig == SIGALRM)
Damien Miller0318e2e2001-02-18 13:04:23 +1100229 sa.sa_flags |= SA_INTERRUPT;
230#endif
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000231 sa.sa_handler = act;
Kevin Stevese74ebd02001-02-17 17:10:16 +0000232 if (sigaction(sig, &sa, NULL) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000233 return (mysig_t) -1;
234 }
235 return (osa.sa_handler);
236#else
237 return (signal(sig, act));
238#endif
239}