1 /*
2  * Copyright (c) 2002, 2003, 2004, 2005 Nick Leuta
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Definitions for porting to different operating systems.
31  */
32 
33 #ifndef	_PORT_BASE_H_
34 #define	_PORT_BASE_H_
35 
36 /*
37  * Linux porting things.
38  */
39 #ifdef LINUX
40 
41 /* MAXLOGNAME should be == UT_NAMESIZE+1 (see <utmp.h>)
42  * There are the examples of the definitions in different operating systems.
43  * In FreeBSD 4.x (defined in sys/param.h):
44 #define MAXLOGNAME 17
45  * in Linux/Glibc-2.2:
46 #define MAXLOGNAME 33
47  * We can't include <utmp.h>, because some its functions may have the same
48  * names as another functions in FTP client. So we can to define the value
49  * of UT_NAMESIZE like in examples above or to try next way (it works at least
50  * with Glibc 2.2) */
51 #ifndef _UTMP_H
52 # define _UTMP_H
53 # include <bits/utmp.h>
54 # undef _UTMP_H
55 #endif
56 #define MAXLOGNAME UT_NAMESIZE+1
57 
58 /*
59  *	From FreeBSD's stdio.h
60  */
61 #include <stdio.h>
62 #include <stdarg.h>
63 
64 char	*fgetln(FILE *, size_t *);
65 
66 /* Next functions are implemented in Linux systems. They are defined in stdio.h
67  * and are available if _GNU_SOURCE is defined, for example:
68 #define _GNU_SOURCE
69 #include <stdio.h>
70  * We can't use this way, because some functions from the set of GNU extensions
71  * are in conflict with the functions with the same names from ftpd's code.
72  *
73  * It's possible to use "simple" form of definition like
74 int	 asprintf(char **, const char *, ...);
75  * but we try to keep those definitions from glibc's headers. */
76 extern int vasprintf (char **__restrict __ptr, __const char *__restrict __f,
77 		      va_list __arg)
78      __THROW __attribute__ ((__format__ (__printf__, 2, 0)));
79 extern int asprintf (char **__restrict __ptr,
80 		       __const char *__restrict __fmt, ...)
81      __THROW __attribute__ ((__format__ (__printf__, 2, 3)));
82 
83 /*
84  *	From FreeBSD's stdlib.h
85  */
86 void    *reallocf(void *, size_t);
87 
88 /*
89  *	From FreeBSD's string.h
90  */
91 size_t	 strlcpy(char *, const char *, size_t);
92 
93 /*
94  *	From FreeBSD's sys/select.h
95  */
96 #define	FD_COPY(f, t)	(void)(*(t) = *(f))
97 
98 /*
99  *	Special porting things.
100  */
101 #define AI_ADDRLEN(HI) \
102     HI->ai_addrlen - (HI->ai_family == AF_INET ? \
103     sizeof(struct sockaddr) - __SOCKADDR_COMMON_SIZE - sizeof (in_port_t) - \
104     sizeof(struct in_addr) : 0)
105 
106 #ifdef INET6
107 #define SA_LEN(addr) \
108     (addr)->sa_family == AF_INET ? sizeof(struct sockaddr_in) : \
109     sizeof(struct sockaddr_in6)
110 #else /* !INET6 */
111 #define SA_LEN(addr) \
112     sizeof(struct sockaddr_in)
113 #endif /* INET6 */
114 
115 #ifdef INET6
116 #define SU_LEN(addr) \
117     (addr).su_si.si_family == AF_INET ? sizeof(struct sockaddr_in) : \
118     sizeof(struct sockaddr_in6)
119 #else /* !INET6 */
120 #define SU_LEN(addr) \
121     sizeof(struct sockaddr_in)
122 #endif /* INET6 */
123 
124 #endif /* LINUX */
125 
126 /*
127  * NetBSD porting things.
128  */
129 #ifdef NETBSD
130 
131 #include <sys/socket.h>
132 /*
133  *	From FreeBSD's libutil.h
134  */
135 extern int	realhostname_sa(char *host, size_t hsize, struct sockaddr *addr,
136 			     int addrlen);
137 
138 /*
139  *	From FreeBSD's stdlib.h
140  */
141 void    *reallocf(void *, size_t);
142 
143 #endif /* NETBSD */
144 
145 #endif /* _PORT_BASE_H_ */
146