3 * Copyright (c) 1995-2005 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the Institute nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 #ifdef HAVE_SYS_PARAM_H
49 #include <sys/param.h>
51 #ifdef HAVE_INTTYPES_H
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
57 #ifdef HAVE_SYS_BITYPES_H
58 #include <sys/bitypes.h>
60 #ifdef HAVE_BIND_BITYPES_H
61 #include <bind/bitypes.h>
63 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
64 #include <netinet/in6_machtypes.h>
69 #ifdef HAVE_SYS_SOCKET_H
70 #include <sys/socket.h>
78 #ifdef HAVE_SYS_STAT_H
81 #ifdef HAVE_NETINET_IN_H
82 #include <netinet/in.h>
84 #ifdef HAVE_NETINET_IN6_H
85 #include <netinet/in6.h>
87 #ifdef HAVE_NETINET6_IN6_H
88 #include <netinet6/in6.h>
90 #ifdef HAVE_ARPA_INET_H
91 #include <arpa/inet.h>
96 #ifdef HAVE_ARPA_NAMESER_H
97 #include <arpa/nameser.h>
112 #ifdef HAVE_TERMIOS_H
115 #if defined(HAVE_SYS_IOCTL_H) && SunOS != 40
116 #include <sys/ioctl.h>
118 #ifdef TIME_WITH_SYS_TIME
119 #include <sys/time.h>
121 #elif defined(HAVE_SYS_TIME_H)
122 #include <sys/time.h>
135 #include <roken-common.h>
139 #ifdef HAVE_UINTPTR_T
140 #define rk_UNCONST(x) ((void *)(uintptr_t)(const void *)(x))
142 #define rk_UNCONST(x) ((void *)(unsigned long)(const void *)(x))
145 #if !defined(HAVE_SETSID) && defined(HAVE__SETSID)
146 #define setsid _setsid
150 #define putenv rk_putenv
151 int ROKEN_LIB_FUNCTION
putenv(const char *);
154 #if !defined(HAVE_SETENV) || defined(NEED_SETENV_PROTO)
156 #define setenv rk_setenv
158 int ROKEN_LIB_FUNCTION
setenv(const char *, const char *, int);
161 #if !defined(HAVE_UNSETENV) || defined(NEED_UNSETENV_PROTO)
162 #ifndef HAVE_UNSETENV
163 #define unsetenv rk_unsetenv
165 void ROKEN_LIB_FUNCTION
unsetenv(const char *);
168 #if !defined(HAVE_GETUSERSHELL) || defined(NEED_GETUSERSHELL_PROTO)
169 #ifndef HAVE_GETUSERSHELL
170 #define getusershell rk_getusershell
171 #define endusershell rk_endusershell
173 char * ROKEN_LIB_FUNCTION
getusershell(void);
174 void ROKEN_LIB_FUNCTION
endusershell(void);
177 #if !defined(HAVE_SNPRINTF) || defined(NEED_SNPRINTF_PROTO)
178 #ifndef HAVE_SNPRINTF
179 #define snprintf rk_snprintf
181 int ROKEN_LIB_FUNCTION
182 rk_snprintf (char *, size_t, const char *, ...)
183 __attribute__ ((format (printf
, 3, 4)));
186 #if !defined(HAVE_VSNPRINTF) || defined(NEED_VSNPRINTF_PROTO)
187 #ifndef HAVE_VSNPRINTF
188 #define vsnprintf rk_vsnprintf
190 int ROKEN_LIB_FUNCTION
191 rk_vsnprintf (char *, size_t, const char *, va_list)
192 __attribute__((format (printf
, 3, 0)));
195 #if !defined(HAVE_ASPRINTF) || defined(NEED_ASPRINTF_PROTO)
196 #ifndef HAVE_ASPRINTF
197 #define asprintf rk_asprintf
199 int ROKEN_LIB_FUNCTION
200 rk_asprintf (char **, const char *, ...)
201 __attribute__ ((format (printf
, 2, 3)));
204 #if !defined(HAVE_VASPRINTF) || defined(NEED_VASPRINTF_PROTO)
205 #ifndef HAVE_VASPRINTF
206 #define vasprintf rk_vasprintf
208 int ROKEN_LIB_FUNCTION
209 rk_vasprintf (char **, const char *, va_list)
210 __attribute__((format (printf
, 2, 0)));
213 #if !defined(HAVE_ASNPRINTF) || defined(NEED_ASNPRINTF_PROTO)
214 #ifndef HAVE_ASNPRINTF
215 #define asnprintf rk_asnprintf
217 int ROKEN_LIB_FUNCTION
218 rk_asnprintf (char **, size_t, const char *, ...)
219 __attribute__ ((format (printf
, 3, 4)));
222 #if !defined(HAVE_VASNPRINTF) || defined(NEED_VASNPRINTF_PROTO)
223 #ifndef HAVE_VASNPRINTF
224 #define vasnprintf rk_vasnprintf
226 int ROKEN_LIB_FUNCTION
227 vasnprintf (char **, size_t, const char *, va_list)
228 __attribute__((format (printf
, 3, 0)));
232 #define strdup rk_strdup
233 char * ROKEN_LIB_FUNCTION
strdup(const char *);
236 #if !defined(HAVE_STRNDUP) || defined(NEED_STRNDUP_PROTO)
238 #define strndup rk_strndup
240 char * ROKEN_LIB_FUNCTION
strndup(const char *, size_t);
244 #define strlwr rk_strlwr
245 char * ROKEN_LIB_FUNCTION
strlwr(char *);
249 #define strnlen rk_strnlen
250 size_t ROKEN_LIB_FUNCTION
strnlen(const char*, size_t);
253 #if !defined(HAVE_STRSEP) || defined(NEED_STRSEP_PROTO)
255 #define strsep rk_strsep
257 char * ROKEN_LIB_FUNCTION
strsep(char**, const char*);
260 #if !defined(HAVE_STRSEP_COPY) || defined(NEED_STRSEP_COPY_PROTO)
261 #ifndef HAVE_STRSEP_COPY
262 #define strsep_copy rk_strsep_copy
264 ssize_t ROKEN_LIB_FUNCTION
strsep_copy(const char**, const char*, char*, size_t);
267 #ifndef HAVE_STRCASECMP
268 #define strcasecmp rk_strcasecmp
269 int ROKEN_LIB_FUNCTION
strcasecmp(const char *, const char *);
272 #ifdef NEED_FCLOSE_PROTO
273 int ROKEN_LIB_FUNCTION
fclose(FILE *);
276 #ifdef NEED_STRTOK_R_PROTO
277 char * ROKEN_LIB_FUNCTION
strtok_r(char *, const char *, char **);
281 #define strupr rk_strupr
282 char * ROKEN_LIB_FUNCTION
strupr(char *);
286 #define strlcpy rk_strlcpy
287 size_t ROKEN_LIB_FUNCTION
strlcpy (char *, const char *, size_t);
291 #define strlcat rk_strlcat
292 size_t ROKEN_LIB_FUNCTION
strlcat (char *, const char *, size_t);
295 #ifndef HAVE_GETDTABLESIZE
296 #define getdtablesize rk_getdtablesize
297 int ROKEN_LIB_FUNCTION
getdtablesize(void);
300 #if !defined(HAVE_STRERROR) && !defined(strerror)
301 #define strerror rk_strerror
302 char * ROKEN_LIB_FUNCTION
strerror(int);
305 #if !defined(HAVE_HSTRERROR) || defined(NEED_HSTRERROR_PROTO)
306 #ifndef HAVE_HSTRERROR
307 #define hstrerror rk_hstrerror
309 /* This causes a fatal error under Psoriasis */
310 #if !(defined(SunOS) && (SunOS >= 50))
311 const char * ROKEN_LIB_FUNCTION
hstrerror(int);
315 #if !HAVE_DECL_H_ERRNO
319 #if !defined(HAVE_INET_ATON) || defined(NEED_INET_ATON_PROTO)
320 #ifndef HAVE_INET_ATON
321 #define inet_aton rk_inet_aton
323 int ROKEN_LIB_FUNCTION
inet_aton(const char *, struct in_addr
*);
326 #ifndef HAVE_INET_NTOP
327 #define inet_ntop rk_inet_ntop
328 const char * ROKEN_LIB_FUNCTION
329 inet_ntop(int af
, const void *src
, char *dst
, size_t size
);
332 #ifndef HAVE_INET_PTON
333 #define inet_pton rk_inet_pton
334 int ROKEN_LIB_FUNCTION
335 inet_pton(int, const char *, void *);
338 #if !defined(HAVE_GETCWD)
339 #define getcwd rk_getcwd
340 char* ROKEN_LIB_FUNCTION
getcwd(char *, size_t);
345 struct passwd
* ROKEN_LIB_FUNCTION
k_getpwnam (const char *);
346 struct passwd
* ROKEN_LIB_FUNCTION
k_getpwuid (uid_t
);
349 const char * ROKEN_LIB_FUNCTION
get_default_username (void);
352 #define seteuid rk_seteuid
353 int ROKEN_LIB_FUNCTION
seteuid(uid_t
);
357 #define setegid rk_setegid
358 int ROKEN_LIB_FUNCTION
setegid(gid_t
);
362 #define lstat rk_lstat
363 int ROKEN_LIB_FUNCTION
lstat(const char *, struct stat
*);
366 #if !defined(HAVE_MKSTEMP) || defined(NEED_MKSTEMP_PROTO)
368 #define mkstemp rk_mkstemp
370 int ROKEN_LIB_FUNCTION
mkstemp(char *);
374 #define cgetent rk_cgetent
375 #define cgetstr rk_cgetstr
376 int ROKEN_LIB_FUNCTION
cgetent(char **, char **, const char *);
377 int ROKEN_LIB_FUNCTION
cgetstr(char *, const char *, char **);
380 #ifndef HAVE_INITGROUPS
381 #define initgroups rk_initgroups
382 int ROKEN_LIB_FUNCTION
initgroups(const char *, gid_t
);
386 #define fchown rk_fchown
387 int ROKEN_LIB_FUNCTION
fchown(int, uid_t
, gid_t
);
390 #if !defined(HAVE_DAEMON) || defined(NEED_DAEMON_PROTO)
392 #define daemon rk_daemon
394 int ROKEN_LIB_FUNCTION
daemon(int, int);
398 #define chown rk_chown
399 int ROKEN_LIB_FUNCTION
chown(const char *, uid_t
, gid_t
);
404 int ROKEN_LIB_FUNCTION
405 rcmd(char **, unsigned short, const char *,
406 const char *, const char *, int *);
409 #if !defined(HAVE_INNETGR) || defined(NEED_INNETGR_PROTO)
411 #define innetgr rk_innetgr
413 int ROKEN_LIB_FUNCTION
innetgr(const char*, const char*,
414 const char*, const char*);
417 #ifndef HAVE_IRUSEROK
418 #define iruserok rk_iruserok
419 int ROKEN_LIB_FUNCTION
iruserok(unsigned, int,
420 const char *, const char *);
423 #if !defined(HAVE_GETHOSTNAME) || defined(NEED_GETHOSTNAME_PROTO)
424 #ifndef HAVE_GETHOSTNAME
425 #define gethostname rk_gethostname
427 int ROKEN_LIB_FUNCTION
gethostname(char *, int);
431 #define writev rk_writev
432 ssize_t ROKEN_LIB_FUNCTION
433 writev(int, const struct iovec
*, int);
437 #define readv rk_readv
438 ssize_t ROKEN_LIB_FUNCTION
439 readv(int, const struct iovec
*, int);
443 #define pidfile rk_pidfile
444 void ROKEN_LIB_FUNCTION
pidfile (const char*);
448 #define bswap32 rk_bswap32
449 unsigned int ROKEN_LIB_FUNCTION
bswap32(unsigned int);
453 #define bswap16 rk_bswap16
454 unsigned short ROKEN_LIB_FUNCTION
bswap16(unsigned short);
459 #define LOCK_SH 1 /* Shared lock */
462 #define LOCK_EX 2 /* Exclusive lock */
465 #define LOCK_NB 4 /* Don't block when locking */
468 #define LOCK_UN 8 /* Unlock */
471 #define flock(_x,_y) rk_flock(_x,_y)
472 int rk_flock(int fd
, int operation
);
473 #endif /* HAVE_FLOCK */
476 #define dirfd(x) ((x)->dd_fd)
479 time_t ROKEN_LIB_FUNCTION
tm2time (struct tm
, int);
481 int ROKEN_LIB_FUNCTION
unix_verify_user(char *, char *);
483 int ROKEN_LIB_FUNCTION
roken_concat (char *, size_t, ...);
485 size_t ROKEN_LIB_FUNCTION
roken_mconcat (char **, size_t, ...);
487 int ROKEN_LIB_FUNCTION
roken_vconcat (char *, size_t, va_list);
489 size_t ROKEN_LIB_FUNCTION
490 roken_vmconcat (char **, size_t, va_list);
492 ssize_t ROKEN_LIB_FUNCTION
net_write (int, const void *, size_t);
494 ssize_t ROKEN_LIB_FUNCTION
net_read (int, void *, size_t);
496 int ROKEN_LIB_FUNCTION
issuid(void);
498 #ifndef HAVE_STRUCT_WINSIZE
500 unsigned short ws_row
, ws_col
;
501 unsigned short ws_xpixel
, ws_ypixel
;
505 int ROKEN_LIB_FUNCTION
get_window_size(int fd
, struct winsize
*);
508 #define vsyslog rk_vsyslog
509 void ROKEN_LIB_FUNCTION
vsyslog(int, const char *, va_list);
512 #if !HAVE_DECL_OPTARG
515 #if !HAVE_DECL_OPTIND
518 #if !HAVE_DECL_OPTERR
522 #ifndef HAVE_GETIPNODEBYNAME
523 #define getipnodebyname rk_getipnodebyname
524 struct hostent
* ROKEN_LIB_FUNCTION
525 getipnodebyname (const char *, int, int, int *);
528 #ifndef HAVE_GETIPNODEBYADDR
529 #define getipnodebyaddr rk_getipnodebyaddr
530 struct hostent
* ROKEN_LIB_FUNCTION
531 getipnodebyaddr (const void *, size_t, int, int *);
534 #ifndef HAVE_FREEHOSTENT
535 #define freehostent rk_freehostent
536 void ROKEN_LIB_FUNCTION
537 freehostent (struct hostent
*);
540 #ifndef HAVE_COPYHOSTENT
541 #define copyhostent rk_copyhostent
542 struct hostent
* ROKEN_LIB_FUNCTION
543 copyhostent (const struct hostent
*);
546 #ifndef HAVE_SOCKLEN_T
547 typedef int socklen_t
;
550 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
552 #ifndef HAVE_SA_FAMILY_T
553 typedef unsigned short sa_family_t
;
557 #define _SS_MAXSIZE sizeof(struct sockaddr_in6)
559 #define _SS_MAXSIZE sizeof(struct sockaddr_in)
562 #define _SS_ALIGNSIZE sizeof(unsigned long)
564 #if HAVE_STRUCT_SOCKADDR_SA_LEN
566 typedef unsigned char roken_sa_family_t
;
568 #define _SS_PAD1SIZE ((2 * _SS_ALIGNSIZE - sizeof (roken_sa_family_t) - sizeof(unsigned char)) % _SS_ALIGNSIZE)
569 #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + sizeof(unsigned char) + _SS_PAD1SIZE + _SS_ALIGNSIZE))
571 struct sockaddr_storage
{
572 unsigned char ss_len
;
573 roken_sa_family_t ss_family
;
574 char __ss_pad1
[_SS_PAD1SIZE
];
575 unsigned long __ss_align
[_SS_PAD2SIZE
/ sizeof(unsigned long) + 1];
578 #else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */
580 typedef unsigned short roken_sa_family_t
;
582 #define _SS_PAD1SIZE ((2 * _SS_ALIGNSIZE - sizeof (roken_sa_family_t)) % _SS_ALIGNSIZE)
583 #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + _SS_PAD1SIZE + _SS_ALIGNSIZE))
585 struct sockaddr_storage
{
586 roken_sa_family_t ss_family
;
587 char __ss_pad1
[_SS_PAD1SIZE
];
588 unsigned long __ss_align
[_SS_PAD2SIZE
/ sizeof(unsigned long) + 1];
591 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
593 #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
595 #ifndef HAVE_STRUCT_ADDRINFO
603 struct sockaddr
*ai_addr
;
604 struct addrinfo
*ai_next
;
608 #ifndef HAVE_GETADDRINFO
609 #define getaddrinfo rk_getaddrinfo
610 int ROKEN_LIB_FUNCTION
611 getaddrinfo(const char *,
613 const struct addrinfo
*,
617 #ifndef HAVE_GETNAMEINFO
618 #define getnameinfo rk_getnameinfo
619 int ROKEN_LIB_FUNCTION
620 getnameinfo(const struct sockaddr
*, socklen_t
,
626 #ifndef HAVE_FREEADDRINFO
627 #define freeaddrinfo rk_freeaddrinfo
628 void ROKEN_LIB_FUNCTION
629 freeaddrinfo(struct addrinfo
*);
632 #ifndef HAVE_GAI_STRERROR
633 #define gai_strerror rk_gai_strerror
634 const char * ROKEN_LIB_FUNCTION
638 int ROKEN_LIB_FUNCTION
639 getnameinfo_verified(const struct sockaddr
*, socklen_t
,
644 int ROKEN_LIB_FUNCTION
645 roken_getaddrinfo_hostspec(const char *, int, struct addrinfo
**);
646 int ROKEN_LIB_FUNCTION
647 roken_getaddrinfo_hostspec2(const char *, int, int, struct addrinfo
**);
649 #ifndef HAVE_STRFTIME
650 #define strftime rk_strftime
651 size_t ROKEN_LIB_FUNCTION
652 strftime (char *, size_t, const char *, const struct tm
*);
655 #ifndef HAVE_STRPTIME
656 #define strptime rk_strptime
657 char * ROKEN_LIB_FUNCTION
658 strptime (const char *, const char *, struct tm
*);
662 #define emalloc rk_emalloc
663 void * ROKEN_LIB_FUNCTION
emalloc (size_t);
666 #define ecalloc rk_ecalloc
667 void * ROKEN_LIB_FUNCTION
ecalloc(size_t, size_t);
669 #ifndef HAVE_EREALLOC
670 #define erealloc rk_erealloc
671 void * ROKEN_LIB_FUNCTION
erealloc (void *, size_t);
674 #define estrdup rk_estrdup
675 char * ROKEN_LIB_FUNCTION
estrdup (const char *);
682 int ROKEN_LIB_FUNCTION
683 roken_gethostby_setup(const char*, const char*);
684 struct hostent
* ROKEN_LIB_FUNCTION
685 roken_gethostbyname(const char*);
686 struct hostent
* ROKEN_LIB_FUNCTION
687 roken_gethostbyaddr(const void*, size_t, int);
689 #ifdef GETSERVBYNAME_PROTO_COMPATIBLE
690 #define roken_getservbyname(x,y) getservbyname(x,y)
692 #define roken_getservbyname(x,y) getservbyname((char *)x, (char *)y)
695 #ifdef OPENLOG_PROTO_COMPATIBLE
696 #define roken_openlog(a,b,c) openlog(a,b,c)
698 #define roken_openlog(a,b,c) openlog((char *)a,b,c)
701 #ifdef GETSOCKNAME_PROTO_COMPATIBLE
702 #define roken_getsockname(a,b,c) getsockname(a,b,c)
704 #define roken_getsockname(a,b,c) getsockname(a, b, (void*)c)
707 #ifndef HAVE_SETPROGNAME
708 #define setprogname rk_setprogname
709 void ROKEN_LIB_FUNCTION
setprogname(const char *);
712 #ifndef HAVE_GETPROGNAME
713 #define getprogname rk_getprogname
714 const char * ROKEN_LIB_FUNCTION
getprogname(void);
717 #if !defined(HAVE_SETPROGNAME) && !defined(HAVE_GETPROGNAME) && !HAVE_DECL___PROGNAME
718 extern const char *__progname
;
721 void ROKEN_LIB_FUNCTION
mini_inetd_addrinfo (struct addrinfo
*);
722 void ROKEN_LIB_FUNCTION
mini_inetd (int);
724 #ifndef HAVE_LOCALTIME_R
725 #define localtime_r rk_localtime_r
726 struct tm
* ROKEN_LIB_FUNCTION
727 localtime_r(const time_t *, struct tm
*);
730 #if !defined(HAVE_STRSVIS) || defined(NEED_STRSVIS_PROTO)
732 #define strsvis rk_strsvis
734 int ROKEN_LIB_FUNCTION
735 strsvis(char *, const char *, int, const char *);
738 #if !defined(HAVE_STRUNVIS) || defined(NEED_STRUNVIS_PROTO)
739 #ifndef HAVE_STRUNVIS
740 #define strunvis rk_strunvis
742 int ROKEN_LIB_FUNCTION
743 strunvis(char *, const char *);
746 #if !defined(HAVE_STRVIS) || defined(NEED_STRVIS_PROTO)
748 #define strvis rk_strvis
750 int ROKEN_LIB_FUNCTION
751 strvis(char *, const char *, int);
754 #if !defined(HAVE_STRVISX) || defined(NEED_STRVISX_PROTO)
756 #define strvisx rk_strvisx
758 int ROKEN_LIB_FUNCTION
759 strvisx(char *, const char *, size_t, int);
762 #if !defined(HAVE_SVIS) || defined(NEED_SVIS_PROTO)
766 char * ROKEN_LIB_FUNCTION
767 svis(char *, int, int, int, const char *);
770 #if !defined(HAVE_UNVIS) || defined(NEED_UNVIS_PROTO)
772 #define unvis rk_unvis
774 int ROKEN_LIB_FUNCTION
775 unvis(char *, int, int *, int);
778 #if !defined(HAVE_VIS) || defined(NEED_VIS_PROTO)
782 char * ROKEN_LIB_FUNCTION
783 vis(char *, int, int, int);
786 #if !defined(HAVE_CLOSEFROM)
787 #define closefrom rk_closefrom
788 int ROKEN_LIB_FUNCTION
792 #if !defined(HAVE_TIMEGM)
793 #define timegm rk_timegm
794 time_t ROKEN_LIB_FUNCTION
795 rk_timegm(struct tm
*tm
);
798 #ifdef SOCKET_WRAPPER_REPLACE
799 #include <socket_wrapper.h>