]> andersk Git - openssh.git/blame - openbsd-compat/bsd-misc.c
- (dtucker) [openbsd-compat/xmmap.c] Include stdlib.h for mkstemp prototype.
[openssh.git] / openbsd-compat / bsd-misc.c
CommitLineData
419e26e7 1
8efc0c15 2/*
9cd11896 3 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
bfc9a610 4 *
9cd11896 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.
bfc9a610 8 *
9cd11896 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.
bfc9a610 16 */
8efc0c15 17
416ed5a7 18#include "includes.h"
b5b88c19 19
65737b47 20#include <sys/types.h>
ab26f490 21#ifdef HAVE_SYS_SELECT_H
22# include <sys/select.h>
23#endif
8151aaa5 24#ifdef HAVE_SYS_TIME_H
25# include <sys/time.h>
26#endif
27
28cb0a43 28#include <string.h>
b5b88c19 29#include <signal.h>
8151aaa5 30#include <stdlib.h>
65737b47 31#include <unistd.h>
b5b88c19 32
d0104542 33#include "xmalloc.h"
8efc0c15 34
3cc5d223 35#ifndef HAVE___PROGNAME
35cf0057 36char *__progname;
37#endif
38
d0104542 39/*
40 * NB. duplicate __progname in case it is an alias for argv[0]
41 * Otherwise it may get clobbered by setproctitle()
42 */
fda04d7d 43char *ssh_get_progname(char *argv0)
260d427b 44{
45#ifdef HAVE___PROGNAME
46 extern char *__progname;
47
d0104542 48 return xstrdup(__progname);
260d427b 49#else
50 char *p;
51
52 if (argv0 == NULL)
9901cb37 53 return ("unknown"); /* XXX */
260d427b 54 p = strrchr(argv0, '/');
55 if (p == NULL)
56 p = argv0;
57 else
58 p++;
d0104542 59
9901cb37 60 return (xstrdup(p));
260d427b 61#endif
62}
63
a5c9cd31 64#ifndef HAVE_SETLOGIN
65int setlogin(const char *name)
66{
9901cb37 67 return (0);
a5c9cd31 68}
69#endif /* !HAVE_SETLOGIN */
70
71#ifndef HAVE_INNETGR
72int innetgr(const char *netgroup, const char *host,
73 const char *user, const char *domain)
74{
9901cb37 75 return (0);
a5c9cd31 76}
77#endif /* HAVE_INNETGR */
78
79#if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
80int seteuid(uid_t euid)
81{
9901cb37 82 return (setreuid(-1, euid));
a5c9cd31 83}
84#endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
819b676f 85
febd3f8e 86#if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
87int setegid(uid_t egid)
88{
9901cb37 89 return(setresgid(-1, egid, -1));
febd3f8e 90}
91#endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
92
416ed5a7 93#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
94const char *strerror(int e)
819b676f 95{
416ed5a7 96 extern int sys_nerr;
97 extern char *sys_errlist[];
98
e2bccbcd 99 if ((e >= 0) && (e < sys_nerr))
9901cb37 100 return (sys_errlist[e]);
101
102 return ("unlisted error");
819b676f 103}
416ed5a7 104#endif
89c7e31c 105
106#ifndef HAVE_UTIMES
107int utimes(char *filename, struct timeval *tvp)
108{
109 struct utimbuf ub;
110
6c266dc3 111 ub.actime = tvp[0].tv_sec;
112 ub.modtime = tvp[1].tv_sec;
89c7e31c 113
9901cb37 114 return (utime(filename, &ub));
89c7e31c 115}
116#endif
f25cd32c 117
118#ifndef HAVE_TRUNCATE
9901cb37 119int truncate(const char *path, off_t length)
f25cd32c 120{
121 int fd, ret, saverrno;
122
123 fd = open(path, O_WRONLY);
124 if (fd < 0)
9901cb37 125 return (-1);
f25cd32c 126
127 ret = ftruncate(fd, length);
128 saverrno = errno;
9901cb37 129 close(fd);
f25cd32c 130 if (ret == -1)
131 errno = saverrno;
9901cb37 132
f25cd32c 133 return(ret);
134}
135#endif /* HAVE_TRUNCATE */
136
5271b55c 137#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
138int nanosleep(const struct timespec *req, struct timespec *rem)
139{
140 int rc, saverrno;
141 extern int errno;
142 struct timeval tstart, tstop, tremain, time2wait;
143
144 TIMESPEC_TO_TIMEVAL(&time2wait, req)
145 (void) gettimeofday(&tstart, NULL);
146 rc = select(0, NULL, NULL, NULL, &time2wait);
147 if (rc == -1) {
148 saverrno = errno;
149 (void) gettimeofday (&tstop, NULL);
150 errno = saverrno;
151 tremain.tv_sec = time2wait.tv_sec -
152 (tstop.tv_sec - tstart.tv_sec);
153 tremain.tv_usec = time2wait.tv_usec -
154 (tstop.tv_usec - tstart.tv_usec);
155 tremain.tv_sec += tremain.tv_usec / 1000000L;
156 tremain.tv_usec %= 1000000L;
157 } else {
158 tremain.tv_sec = 0;
159 tremain.tv_usec = 0;
160 }
161 TIMEVAL_TO_TIMESPEC(&tremain, rem)
162
163 return(rc);
164}
5271b55c 165#endif
166
c5d3dd1b 167#ifndef HAVE_TCGETPGRP
168pid_t
169tcgetpgrp(int fd)
170{
0644eab4 171 int ctty_pgrp;
c5d3dd1b 172
f29c37a9 173 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
c5d3dd1b 174 return(-1);
175 else
176 return(ctty_pgrp);
177}
178#endif /* HAVE_TCGETPGRP */
179
5af25b1d 180#ifndef HAVE_TCSENDBREAK
181int
182tcsendbreak(int fd, int duration)
183{
184# if defined(TIOCSBRK) && defined(TIOCCBRK)
185 struct timeval sleepytime;
186
187 sleepytime.tv_sec = 0;
188 sleepytime.tv_usec = 400000;
189 if (ioctl(fd, TIOCSBRK, 0) == -1)
190 return (-1);
191 (void)select(0, 0, 0, 0, &sleepytime);
192 if (ioctl(fd, TIOCCBRK, 0) == -1)
193 return (-1);
194 return (0);
195# else
196 return -1;
197# endif
198}
199#endif /* HAVE_TCSENDBREAK */
f00d1f78 200
201mysig_t
202mysignal(int sig, mysig_t act)
203{
204#ifdef HAVE_SIGACTION
205 struct sigaction sa, osa;
206
207 if (sigaction(sig, NULL, &osa) == -1)
208 return (mysig_t) -1;
209 if (osa.sa_handler != act) {
210 memset(&sa, 0, sizeof(sa));
211 sigemptyset(&sa.sa_mask);
212 sa.sa_flags = 0;
213#ifdef SA_INTERRUPT
214 if (sig == SIGALRM)
215 sa.sa_flags |= SA_INTERRUPT;
216#endif
217 sa.sa_handler = act;
218 if (sigaction(sig, &sa, NULL) == -1)
219 return (mysig_t) -1;
220 }
221 return (osa.sa_handler);
222#else
cc1102cb 223 #undef signal
f00d1f78 224 return (signal(sig, act));
225#endif
226}
35fc74ed 227
228#ifndef HAVE_STRDUP
229char *
230strdup(const char *str)
231{
232 size_t len;
233 char *cp;
234
235 len = strlen(str) + 1;
236 cp = malloc(len);
237 if (cp != NULL)
bd2a0801 238 return(memcpy(cp, str, len));
239 return NULL;
35fc74ed 240}
241#endif
This page took 3.059931 seconds and 5 git commands to generate.