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.
26 #ifndef HAVE___PROGNAME
31 * NB. duplicate __progname in case it is an alias for argv[0]
32 * Otherwise it may get clobbered by setproctitle()
34 char *ssh_get_progname(char *argv0)
36 #ifdef HAVE___PROGNAME
37 extern char *__progname;
39 return xstrdup(__progname);
44 return ("unknown"); /* XXX */
45 p = strrchr(argv0, '/');
56 int setlogin(const char *name)
60 #endif /* !HAVE_SETLOGIN */
63 int innetgr(const char *netgroup, const char *host,
64 const char *user, const char *domain)
68 #endif /* HAVE_INNETGR */
70 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
71 int seteuid(uid_t euid)
73 return (setreuid(-1, euid));
75 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
77 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
78 int setegid(uid_t egid)
80 return(setresgid(-1, egid, -1));
82 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
84 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
85 const char *strerror(int e)
88 extern char *sys_errlist[];
90 if ((e >= 0) && (e < sys_nerr))
91 return (sys_errlist[e]);
93 return ("unlisted error");
98 int utimes(char *filename, struct timeval *tvp)
102 ub.actime = tvp[0].tv_sec;
103 ub.modtime = tvp[1].tv_sec;
105 return (utime(filename, &ub));
109 #ifndef HAVE_TRUNCATE
110 int truncate(const char *path, off_t length)
112 int fd, ret, saverrno;
114 fd = open(path, O_WRONLY);
118 ret = ftruncate(fd, length);
126 #endif /* HAVE_TRUNCATE */
128 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
129 int nanosleep(const struct timespec *req, struct timespec *rem)
133 struct timeval tstart, tstop, tremain, time2wait;
135 TIMESPEC_TO_TIMEVAL(&time2wait, req)
136 (void) gettimeofday(&tstart, NULL);
137 rc = select(0, NULL, NULL, NULL, &time2wait);
140 (void) gettimeofday (&tstop, NULL);
142 tremain.tv_sec = time2wait.tv_sec -
143 (tstop.tv_sec - tstart.tv_sec);
144 tremain.tv_usec = time2wait.tv_usec -
145 (tstop.tv_usec - tstart.tv_usec);
146 tremain.tv_sec += tremain.tv_usec / 1000000L;
147 tremain.tv_usec %= 1000000L;
152 TIMEVAL_TO_TIMESPEC(&tremain, rem)
158 #ifndef HAVE_TCGETPGRP
164 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
169 #endif /* HAVE_TCGETPGRP */
171 #ifndef HAVE_TCSENDBREAK
173 tcsendbreak(int fd, int duration)
175 # if defined(TIOCSBRK) && defined(TIOCCBRK)
176 struct timeval sleepytime;
178 sleepytime.tv_sec = 0;
179 sleepytime.tv_usec = 400000;
180 if (ioctl(fd, TIOCSBRK, 0) == -1)
182 (void)select(0, 0, 0, 0, &sleepytime);
183 if (ioctl(fd, TIOCCBRK, 0) == -1)
190 #endif /* HAVE_TCSENDBREAK */
193 mysignal(int sig, mysig_t act)
195 #ifdef HAVE_SIGACTION
196 struct sigaction sa, osa;
198 if (sigaction(sig, NULL, &osa) == -1)
200 if (osa.sa_handler != act) {
201 memset(&sa, 0, sizeof(sa));
202 sigemptyset(&sa.sa_mask);
206 sa.sa_flags |= SA_INTERRUPT;
209 if (sigaction(sig, &sa, NULL) == -1)
212 return (osa.sa_handler);
215 return (signal(sig, act));
221 strdup(const char *str)
226 len = strlen(str) + 1;
229 return(memcpy(cp, str, len));