2 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 RCSID("$Id: bsd-misc.c,v 1.21 2004/02/17 05:49:55 djm Exp $");
23 * NB. duplicate __progname in case it is an alias for argv[0]
24 * Otherwise it may get clobbered by setproctitle()
26 char *ssh_get_progname(char *argv0)
28 #ifdef HAVE___PROGNAME
29 extern char *__progname;
31 return xstrdup(__progname);
36 return ("unknown"); /* XXX */
37 p = strrchr(argv0, '/');
48 int setlogin(const char *name)
52 #endif /* !HAVE_SETLOGIN */
55 int innetgr(const char *netgroup, const char *host,
56 const char *user, const char *domain)
60 #endif /* HAVE_INNETGR */
62 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
63 int seteuid(uid_t euid)
65 return (setreuid(-1, euid));
67 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
69 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
70 int setegid(uid_t egid)
72 return(setresgid(-1, egid, -1));
74 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
76 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
77 const char *strerror(int e)
80 extern char *sys_errlist[];
82 if ((e >= 0) && (e < sys_nerr))
83 return (sys_errlist[e]);
85 return ("unlisted error");
90 int utimes(char *filename, struct timeval *tvp)
94 ub.actime = tvp[0].tv_sec;
95 ub.modtime = tvp[1].tv_sec;
97 return (utime(filename, &ub));
101 #ifndef HAVE_TRUNCATE
102 int truncate(const char *path, off_t length)
104 int fd, ret, saverrno;
106 fd = open(path, O_WRONLY);
110 ret = ftruncate(fd, length);
118 #endif /* HAVE_TRUNCATE */
120 #if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP)
122 * Cygwin setgroups should be a noop.
125 setgroups(size_t size, const gid_t *list)
131 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
132 int nanosleep(const struct timespec *req, struct timespec *rem)
136 struct timeval tstart, tstop, tremain, time2wait;
138 TIMESPEC_TO_TIMEVAL(&time2wait, req)
139 (void) gettimeofday(&tstart, NULL);
140 rc = select(0, NULL, NULL, NULL, &time2wait);
143 (void) gettimeofday (&tstop, NULL);
145 tremain.tv_sec = time2wait.tv_sec -
146 (tstop.tv_sec - tstart.tv_sec);
147 tremain.tv_usec = time2wait.tv_usec -
148 (tstop.tv_usec - tstart.tv_usec);
149 tremain.tv_sec += tremain.tv_usec / 1000000L;
150 tremain.tv_usec %= 1000000L;
155 TIMEVAL_TO_TIMESPEC(&tremain, rem)
161 #ifndef HAVE_TCGETPGRP
167 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
172 #endif /* HAVE_TCGETPGRP */
174 #ifndef HAVE_TCSENDBREAK
176 tcsendbreak(int fd, int duration)
178 # if defined(TIOCSBRK) && defined(TIOCCBRK)
179 struct timeval sleepytime;
181 sleepytime.tv_sec = 0;
182 sleepytime.tv_usec = 400000;
183 if (ioctl(fd, TIOCSBRK, 0) == -1)
185 (void)select(0, 0, 0, 0, &sleepytime);
186 if (ioctl(fd, TIOCCBRK, 0) == -1)
193 #endif /* HAVE_TCSENDBREAK */
196 mysignal(int sig, mysig_t act)
198 #ifdef HAVE_SIGACTION
199 struct sigaction sa, osa;
201 if (sigaction(sig, NULL, &osa) == -1)
203 if (osa.sa_handler != act) {
204 memset(&sa, 0, sizeof(sa));
205 sigemptyset(&sa.sa_mask);
209 sa.sa_flags |= SA_INTERRUPT;
212 if (sigaction(sig, &sa, NULL) == -1)
215 return (osa.sa_handler);
218 return (signal(sig, act));