3 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
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.
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SELECT_H
22 # include <sys/select.h>
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
35 #ifndef HAVE___PROGNAME
40 * NB. duplicate __progname in case it is an alias for argv[0]
41 * Otherwise it may get clobbered by setproctitle()
43 char *ssh_get_progname(char *argv0)
46 #ifdef HAVE___PROGNAME
47 extern char *__progname;
52 return ("unknown"); /* XXX */
53 p = strrchr(argv0, '/');
59 if ((q = strdup(p)) == NULL) {
67 int setlogin(const char *name)
71 #endif /* !HAVE_SETLOGIN */
74 int innetgr(const char *netgroup, const char *host,
75 const char *user, const char *domain)
79 #endif /* HAVE_INNETGR */
81 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
82 int seteuid(uid_t euid)
84 return (setreuid(-1, euid));
86 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
88 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
89 int setegid(uid_t egid)
91 return(setresgid(-1, egid, -1));
93 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
95 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
96 const char *strerror(int e)
99 extern char *sys_errlist[];
101 if ((e >= 0) && (e < sys_nerr))
102 return (sys_errlist[e]);
104 return ("unlisted error");
109 int utimes(char *filename, struct timeval *tvp)
113 ub.actime = tvp[0].tv_sec;
114 ub.modtime = tvp[1].tv_sec;
116 return (utime(filename, &ub));
120 #ifndef HAVE_TRUNCATE
121 int truncate(const char *path, off_t length)
123 int fd, ret, saverrno;
125 fd = open(path, O_WRONLY);
129 ret = ftruncate(fd, length);
137 #endif /* HAVE_TRUNCATE */
139 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
140 int nanosleep(const struct timespec *req, struct timespec *rem)
144 struct timeval tstart, tstop, tremain, time2wait;
146 TIMESPEC_TO_TIMEVAL(&time2wait, req)
147 (void) gettimeofday(&tstart, NULL);
148 rc = select(0, NULL, NULL, NULL, &time2wait);
151 (void) gettimeofday (&tstop, NULL);
153 tremain.tv_sec = time2wait.tv_sec -
154 (tstop.tv_sec - tstart.tv_sec);
155 tremain.tv_usec = time2wait.tv_usec -
156 (tstop.tv_usec - tstart.tv_usec);
157 tremain.tv_sec += tremain.tv_usec / 1000000L;
158 tremain.tv_usec %= 1000000L;
164 TIMEVAL_TO_TIMESPEC(&tremain, rem)
170 #if !defined(HAVE_USLEEP)
171 int usleep(unsigned int useconds)
175 ts.tv_sec = useconds / 1000000;
176 ts.tv_nsec = (useconds % 1000000) * 1000;
177 return nanosleep(&ts, NULL);
181 #ifndef HAVE_TCGETPGRP
187 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
192 #endif /* HAVE_TCGETPGRP */
194 #ifndef HAVE_TCSENDBREAK
196 tcsendbreak(int fd, int duration)
198 # if defined(TIOCSBRK) && defined(TIOCCBRK)
199 struct timeval sleepytime;
201 sleepytime.tv_sec = 0;
202 sleepytime.tv_usec = 400000;
203 if (ioctl(fd, TIOCSBRK, 0) == -1)
205 (void)select(0, 0, 0, 0, &sleepytime);
206 if (ioctl(fd, TIOCCBRK, 0) == -1)
213 #endif /* HAVE_TCSENDBREAK */
217 strdup(const char *str)
222 len = strlen(str) + 1;
225 return(memcpy(cp, str, len));
234 return (c == ' ' || c == '\t');
242 #if defined(HAVE_GETPGRP) && !defined(GETPGRP_VOID) && GETPGRP_VOID == 0
244 #elif defined(HAVE_GETPGRP)
256 pledge(const char *promises, const char *paths[])
263 /* a mbtowc that only supports ASCII */
265 mbtowc(wchar_t *pwc, const char *s, size_t n)
267 if (s == NULL || *s == '\0')
268 return 0; /* ASCII is not state-dependent */
269 if (*s < 0 || *s > 0x7f || n < 1) {
283 return (j < 0 ? -j : j);
289 bzero(void *b, size_t n)
291 (void)memset(b, 0, n);
312 #ifdef FFLUSH_NULL_BUG
314 int _ssh_compat_fflush(FILE *f)
321 if (r1 == -1 || r2 == -1 || r3 == -1)