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.
25 #ifndef HAVE___PROGNAME
30 * NB. duplicate __progname in case it is an alias for argv[0]
31 * Otherwise it may get clobbered by setproctitle()
33 char *ssh_get_progname(char *argv0)
35 #ifdef HAVE___PROGNAME
36 extern char *__progname;
38 return xstrdup(__progname);
43 return ("unknown"); /* XXX */
44 p = strrchr(argv0, '/');
55 int setlogin(const char *name)
59 #endif /* !HAVE_SETLOGIN */
62 int innetgr(const char *netgroup, const char *host,
63 const char *user, const char *domain)
67 #endif /* HAVE_INNETGR */
69 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
70 int seteuid(uid_t euid)
72 return (setreuid(-1, euid));
74 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
76 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
77 int setegid(uid_t egid)
79 return(setresgid(-1, egid, -1));
81 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
83 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
84 const char *strerror(int e)
87 extern char *sys_errlist[];
89 if ((e >= 0) && (e < sys_nerr))
90 return (sys_errlist[e]);
92 return ("unlisted error");
97 int utimes(char *filename, struct timeval *tvp)
101 ub.actime = tvp[0].tv_sec;
102 ub.modtime = tvp[1].tv_sec;
104 return (utime(filename, &ub));
108 #ifndef HAVE_TRUNCATE
109 int truncate(const char *path, off_t length)
111 int fd, ret, saverrno;
113 fd = open(path, O_WRONLY);
117 ret = ftruncate(fd, length);
125 #endif /* HAVE_TRUNCATE */
127 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
128 int nanosleep(const struct timespec *req, struct timespec *rem)
132 struct timeval tstart, tstop, tremain, time2wait;
134 TIMESPEC_TO_TIMEVAL(&time2wait, req)
135 (void) gettimeofday(&tstart, NULL);
136 rc = select(0, NULL, NULL, NULL, &time2wait);
139 (void) gettimeofday (&tstop, NULL);
141 tremain.tv_sec = time2wait.tv_sec -
142 (tstop.tv_sec - tstart.tv_sec);
143 tremain.tv_usec = time2wait.tv_usec -
144 (tstop.tv_usec - tstart.tv_usec);
145 tremain.tv_sec += tremain.tv_usec / 1000000L;
146 tremain.tv_usec %= 1000000L;
151 TIMEVAL_TO_TIMESPEC(&tremain, rem)
157 #ifndef HAVE_TCGETPGRP
163 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
168 #endif /* HAVE_TCGETPGRP */
170 #ifndef HAVE_TCSENDBREAK
172 tcsendbreak(int fd, int duration)
174 # if defined(TIOCSBRK) && defined(TIOCCBRK)
175 struct timeval sleepytime;
177 sleepytime.tv_sec = 0;
178 sleepytime.tv_usec = 400000;
179 if (ioctl(fd, TIOCSBRK, 0) == -1)
181 (void)select(0, 0, 0, 0, &sleepytime);
182 if (ioctl(fd, TIOCCBRK, 0) == -1)
189 #endif /* HAVE_TCSENDBREAK */
192 mysignal(int sig, mysig_t act)
194 #ifdef HAVE_SIGACTION
195 struct sigaction sa, osa;
197 if (sigaction(sig, NULL, &osa) == -1)
199 if (osa.sa_handler != act) {
200 memset(&sa, 0, sizeof(sa));
201 sigemptyset(&sa.sa_mask);
205 sa.sa_flags |= SA_INTERRUPT;
208 if (sigaction(sig, &sa, NULL) == -1)
211 return (osa.sa_handler);
214 return (signal(sig, act));
220 strdup(const char *str)
225 len = strlen(str) + 1;
228 return(memcpy(cp, str, len));