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
)
45 #ifdef HAVE___PROGNAME
46 extern char *__progname
;
48 return xstrdup(__progname
);
53 return ("unknown"); /* XXX */
54 p
= strrchr(argv0
, '/');
65 int setlogin(const char *name
)
69 #endif /* !HAVE_SETLOGIN */
72 int innetgr(const char *netgroup
, const char *host
,
73 const char *user
, const char *domain
)
77 #endif /* HAVE_INNETGR */
79 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
80 int seteuid(uid_t euid
)
82 return (setreuid(-1, euid
));
84 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
86 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
87 int setegid(uid_t egid
)
89 return(setresgid(-1, egid
, -1));
91 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
93 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
94 const char *strerror(int e
)
97 extern char *sys_errlist
[];
99 if ((e
>= 0) && (e
< sys_nerr
))
100 return (sys_errlist
[e
]);
102 return ("unlisted error");
107 int utimes(char *filename
, struct timeval
*tvp
)
111 ub
.actime
= tvp
[0].tv_sec
;
112 ub
.modtime
= tvp
[1].tv_sec
;
114 return (utime(filename
, &ub
));
118 #ifndef HAVE_TRUNCATE
119 int truncate(const char *path
, off_t length
)
121 int fd
, ret
, saverrno
;
123 fd
= open(path
, O_WRONLY
);
127 ret
= ftruncate(fd
, length
);
135 #endif /* HAVE_TRUNCATE */
137 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
138 int nanosleep(const struct timespec
*req
, struct timespec
*rem
)
142 struct timeval tstart
, tstop
, tremain
, time2wait
;
144 TIMESPEC_TO_TIMEVAL(&time2wait
, req
)
145 (void) gettimeofday(&tstart
, NULL
);
146 rc
= select(0, NULL
, NULL
, NULL
, &time2wait
);
149 (void) gettimeofday (&tstop
, NULL
);
151 tremain
.tv_sec
= time2wait
.tv_sec
-
152 (tstop
.tv_sec
- tstart
.tv_sec
);
153 tremain
.tv_usec
= time2wait
.tv_usec
-
154 (tstop
.tv_usec
- tstart
.tv_usec
);
155 tremain
.tv_sec
+= tremain
.tv_usec
/ 1000000L;
156 tremain
.tv_usec
%= 1000000L;
162 TIMEVAL_TO_TIMESPEC(&tremain
, rem
)
168 #ifndef HAVE_TCGETPGRP
174 if (ioctl(fd
, TIOCGPGRP
, &ctty_pgrp
) == -1)
179 #endif /* HAVE_TCGETPGRP */
181 #ifndef HAVE_TCSENDBREAK
183 tcsendbreak(int fd
, int duration
)
185 # if defined(TIOCSBRK) && defined(TIOCCBRK)
186 struct timeval sleepytime
;
188 sleepytime
.tv_sec
= 0;
189 sleepytime
.tv_usec
= 400000;
190 if (ioctl(fd
, TIOCSBRK
, 0) == -1)
192 (void)select(0, 0, 0, 0, &sleepytime
);
193 if (ioctl(fd
, TIOCCBRK
, 0) == -1)
200 #endif /* HAVE_TCSENDBREAK */
203 mysignal(int sig
, mysig_t act
)
205 #ifdef HAVE_SIGACTION
206 struct sigaction sa
, osa
;
208 if (sigaction(sig
, NULL
, &osa
) == -1)
210 if (osa
.sa_handler
!= act
) {
211 memset(&sa
, 0, sizeof(sa
));
212 sigemptyset(&sa
.sa_mask
);
216 sa
.sa_flags
|= SA_INTERRUPT
;
219 if (sigaction(sig
, &sa
, NULL
) == -1)
222 return (osa
.sa_handler
);
225 return (signal(sig
, act
));
231 strdup(const char *str
)
236 len
= strlen(str
) + 1;
239 return(memcpy(cp
, str
, len
));
247 return (c
== ' ' || c
== '\t');