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.
21 RCSID("$Id: bsd-misc.c,v 1.25 2004/08/15 08:41:00 djm Exp $");
23 #ifndef HAVE___PROGNAME
28 * NB. duplicate __progname in case it is an alias for argv[0]
29 * Otherwise it may get clobbered by setproctitle()
31 char *ssh_get_progname(char *argv0
)
33 #ifdef HAVE___PROGNAME
34 extern char *__progname
;
36 return xstrdup(__progname
);
41 return ("unknown"); /* XXX */
42 p
= strrchr(argv0
, '/');
53 int setlogin(const char *name
)
57 #endif /* !HAVE_SETLOGIN */
60 int innetgr(const char *netgroup
, const char *host
,
61 const char *user
, const char *domain
)
65 #endif /* HAVE_INNETGR */
67 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
68 int seteuid(uid_t euid
)
70 return (setreuid(-1, euid
));
72 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
74 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
75 int setegid(uid_t egid
)
77 return(setresgid(-1, egid
, -1));
79 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
81 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
82 const char *strerror(int e
)
85 extern char *sys_errlist
[];
87 if ((e
>= 0) && (e
< sys_nerr
))
88 return (sys_errlist
[e
]);
90 return ("unlisted error");
95 int utimes(char *filename
, struct timeval
*tvp
)
99 ub
.actime
= tvp
[0].tv_sec
;
100 ub
.modtime
= tvp
[1].tv_sec
;
102 return (utime(filename
, &ub
));
106 #ifndef HAVE_TRUNCATE
107 int truncate(const char *path
, off_t length
)
109 int fd
, ret
, saverrno
;
111 fd
= open(path
, O_WRONLY
);
115 ret
= ftruncate(fd
, length
);
123 #endif /* HAVE_TRUNCATE */
125 #if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP)
127 * Cygwin setgroups should be a noop.
130 setgroups(size_t size
, const gid_t
*list
)
136 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
137 int nanosleep(const struct timespec
*req
, struct timespec
*rem
)
141 struct timeval tstart
, tstop
, tremain
, time2wait
;
143 TIMESPEC_TO_TIMEVAL(&time2wait
, req
)
144 (void) gettimeofday(&tstart
, NULL
);
145 rc
= select(0, NULL
, NULL
, NULL
, &time2wait
);
148 (void) gettimeofday (&tstop
, NULL
);
150 tremain
.tv_sec
= time2wait
.tv_sec
-
151 (tstop
.tv_sec
- tstart
.tv_sec
);
152 tremain
.tv_usec
= time2wait
.tv_usec
-
153 (tstop
.tv_usec
- tstart
.tv_usec
);
154 tremain
.tv_sec
+= tremain
.tv_usec
/ 1000000L;
155 tremain
.tv_usec
%= 1000000L;
160 TIMEVAL_TO_TIMESPEC(&tremain
, rem
)
166 #ifndef HAVE_TCGETPGRP
172 if (ioctl(fd
, TIOCGPGRP
, &ctty_pgrp
) == -1)
177 #endif /* HAVE_TCGETPGRP */
179 #ifndef HAVE_TCSENDBREAK
181 tcsendbreak(int fd
, int duration
)
183 # if defined(TIOCSBRK) && defined(TIOCCBRK)
184 struct timeval sleepytime
;
186 sleepytime
.tv_sec
= 0;
187 sleepytime
.tv_usec
= 400000;
188 if (ioctl(fd
, TIOCSBRK
, 0) == -1)
190 (void)select(0, 0, 0, 0, &sleepytime
);
191 if (ioctl(fd
, TIOCCBRK
, 0) == -1)
198 #endif /* HAVE_TCSENDBREAK */
201 mysignal(int sig
, mysig_t act
)
203 #ifdef HAVE_SIGACTION
204 struct sigaction sa
, osa
;
206 if (sigaction(sig
, NULL
, &osa
) == -1)
208 if (osa
.sa_handler
!= act
) {
209 memset(&sa
, 0, sizeof(sa
));
210 sigemptyset(&sa
.sa_mask
);
214 sa
.sa_flags
|= SA_INTERRUPT
;
217 if (sigaction(sig
, &sa
, NULL
) == -1)
220 return (osa
.sa_handler
);
223 return (signal(sig
, act
));