1eff741agavin/*	$NetBSD: tnftp.h,v 1.33 2009/11/14 08:32:42 lukem Exp $	*/
2eff741agavin
3eff741agavin#define	FTP_PRODUCT	PACKAGE_NAME
4eff741agavin#define	FTP_VERSION	PACKAGE_VERSION
5eff741agavin
6eff741agavin#include "tnftp_config.h"
7eff741agavin
8eff741agavin#include <stdio.h>
9eff741agavin#include <ctype.h>
10eff741agavin#include <errno.h>
11eff741agavin
12eff741agavin#if defined(HAVE_SYS_TYPES_H)
13eff741agavin# include <sys/types.h>
14eff741agavin#endif
15eff741agavin#if defined(STDC_HEADERS)
16eff741agavin# include <stdarg.h>
17eff741agavin# include <stdlib.h>
18eff741agavin# include <string.h>
19eff741agavin#endif
20eff741agavin#if defined(HAVE_LIBGEN_H)
21eff741agavin# include <libgen.h>
22eff741agavin#endif
23eff741agavin#if defined(HAVE_UNISTD_H)
24eff741agavin# include <unistd.h>
25eff741agavin#endif
26eff741agavin#if defined(HAVE_POLL_H)
27eff741agavin# include <poll.h>
28eff741agavin#elif defined(HAVE_SYS_POLL_H)
29eff741agavin# include <sys/poll.h>
30eff741agavin#endif
31eff741agavin#if defined(HAVE_SYS_SOCKET_H)
32eff741agavin# include <sys/socket.h>
33eff741agavin#endif
34eff741agavin#if defined(HAVE_NETINET_IN_H)
35eff741agavin# include <netinet/in.h>
36eff741agavin#endif
37eff741agavin#if defined(HAVE_NETINET_IN_SYSTM_H)
38eff741agavin# include <netinet/in_systm.h>
39eff741agavin#endif
40eff741agavin#if defined(HAVE_NETINET_IP_H)
41eff741agavin# include <netinet/ip.h>
42eff741agavin#endif
43eff741agavin#if defined(HAVE_NETDB_H)
44eff741agavin# if HAVE_DECL_AI_NUMERICHOST
45eff741agavin#  include <netdb.h>
46eff741agavin# else	/* !HAVE_DECL_AI_NUMERICHOST */
47eff741agavin#  define getaddrinfo non_rfc2553_getaddrinfo
48eff741agavin#  include <netdb.h>
49eff741agavin#  undef getaddrinfo
50eff741agavin# endif	/* !HAVE_DECL_AI_NUMERICHOST */
51eff741agavin#endif
52eff741agavin#if defined(HAVE_ARPA_INET_H)
53eff741agavin# include <arpa/inet.h>
54eff741agavin#endif
55eff741agavin#if defined(HAVE_DIRENT_H)
56eff741agavin# include <dirent.h>
57eff741agavin#else
58eff741agavin# define dirent direct
59eff741agavin# if defined(HAVE_SYS_NDIR_H)
60eff741agavin#  include <sys/ndir.h>
61eff741agavin# endif
62eff741agavin# if defined(HAVE_SYS_DIR_H)
63eff741agavin#  include <sys/dir.h>
64eff741agavin# endif
65eff741agavin# if defined(HAVE_NDIR_H)
66eff741agavin#  include <ndir.h>
67eff741agavin# endif
68eff741agavin#endif
69eff741agavin
70eff741agavin#if defined(HAVE_SYS_IOCTL_H)
71eff741agavin# include <sys/ioctl.h>
72eff741agavin#endif
73eff741agavin#if defined(HAVE_SYS_PARAM_H)
74eff741agavin# include <sys/param.h>
75eff741agavin#endif
76eff741agavin#if defined(HAVE_SYS_STAT_H)
77eff741agavin# include <sys/stat.h>
78eff741agavin#endif
79eff741agavin#if defined(HAVE_SYS_SYSLIMITS_H)
80eff741agavin# include <sys/syslimits.h>
81eff741agavin#endif
82eff741agavin#if defined(HAVE_SYS_WAIT_H)
83eff741agavin# include <sys/wait.h>
84eff741agavin#endif
85eff741agavin
86eff741agavin#if defined(HAVE_ARPA_FTP_H)
87eff741agavin# include <arpa/ftp.h>
88eff741agavin#endif
89eff741agavin
90eff741agavin#if defined(HAVE_FCNTL_H)
91eff741agavin# include <fcntl.h>
92eff741agavin#endif
93eff741agavin#if defined(HAVE_LIMITS_H)
94eff741agavin# include <limits.h>
95eff741agavin#endif
96eff741agavin#if defined(HAVE_PWD_H)
97eff741agavin# include <pwd.h>
98eff741agavin#endif
99eff741agavin#if defined(HAVE_SETJMP_H)
100eff741agavin# include <setjmp.h>
101eff741agavin#endif
102eff741agavin#if defined(HAVE_SIGNAL_H)
103eff741agavin# include <signal.h>
104eff741agavin#endif
105eff741agavin#if defined(HAVE_STDDEF_H)
106eff741agavin# include <stddef.h>
107eff741agavin#endif
108eff741agavin#if defined(HAVE_TERMIOS_H)
109eff741agavin# include <termios.h>
110eff741agavin#endif
111eff741agavin
112eff741agavin#if defined(HAVE_POLL)
113eff741agavin/* we use poll */
114eff741agavin#elif defined(HAVE_SELECT)
115eff741agavin/* we use select */
116eff741agavin#else /* !defined(HAVE_POLL) && !defined(HAVE_SELECT) */
117eff741agavin# error "no poll() or select() found"
118eff741agavin#endif
119eff741agavin#if !defined(POLLIN)
120eff741agavin# define POLLIN		0x0001
121eff741agavin#endif
122eff741agavin#if !defined(POLLOUT)
123eff741agavin# define POLLOUT	0x0004
124eff741agavin#endif
125eff741agavin#if !defined(POLLRDNORM)
126eff741agavin# define POLLRDNORM	0x0040
127eff741agavin#endif
128eff741agavin#if !defined(POLLWRNORM)
129eff741agavin# define POLLWRNORM	POLLOUT
130eff741agavin#endif
131eff741agavin#if !defined(POLLRDBAND)
132eff741agavin# define POLLRDBAND	0x0080
133eff741agavin#endif
134eff741agavin#if !defined(INFTIM)
135eff741agavin# define INFTIM -1
136eff741agavin#endif
137eff741agavin#if !defined(HAVE_STRUCT_POLLFD)
138eff741agavinstruct pollfd {
139eff741agavin	int	fd;
140eff741agavin	short	events;
141eff741agavin	short	revents;
142eff741agavin};
143eff741agavin#endif
144eff741agavin
145eff741agavin#if defined(TIME_WITH_SYS_TIME)
146eff741agavin# include <sys/time.h>
147eff741agavin# include <time.h>
148eff741agavin#else
149eff741agavin# if defined(HAVE_SYS_TIME_H)
150eff741agavin#  include <sys/time.h>
151eff741agavin# else
152eff741agavin#  include <time.h>
153eff741agavin# endif
154eff741agavin#endif
155eff741agavin
156eff741agavin#if defined(HAVE_ERR_H)
157eff741agavin# include <err.h>
158eff741agavin#endif
159eff741agavin
160eff741agavin#if defined(USE_GLOB_H)	/* not set by configure; used by other build systems */
161eff741agavin# include <glob.h>
162eff741agavin#else
163eff741agavin# include "ftpglob.h"
164eff741agavin#endif
165eff741agavin
166eff741agavin#if defined(HAVE_PATHS_H)
167eff741agavin# include <paths.h>
168eff741agavin#endif
169eff741agavin#if !defined(_PATH_BSHELL)
170eff741agavin# define _PATH_BSHELL	"/bin/sh"
171eff741agavin#endif
172eff741agavin#if !defined(_PATH_TMP)
173eff741agavin# define _PATH_TMP	"/tmp/"
174eff741agavin#endif
175eff741agavin
176eff741agavintypedef struct _stringlist {
177eff741agavin	char	**sl_str;
178eff741agavin	size_t	  sl_max;
179eff741agavin	size_t	  sl_cur;
180eff741agavin} StringList;
181eff741agavin
182eff741agavinStringList *sl_init(void);
183eff741agavinint	 sl_add(StringList *, char *);
184eff741agavinvoid	 sl_free(StringList *, int);
185eff741agavinchar	*sl_find(StringList *, char *);
186eff741agavin
187eff741agavin#if defined(HAVE_TERMCAP_H)
188eff741agavin# include <termcap.h>
189eff741agavin#else
190eff741agavinint	 tgetent(char *, const char *);
191eff741agavinchar	*tgetstr(const char *, char **);
192eff741agavinint	 tgetflag(const char *);
193eff741agavinint	 tgetnum(const char *);
194eff741agavinchar	*tgoto(const char *, int, int);
195eff741agavinvoid	 tputs(const char *, int, int (*)(int));
196eff741agavin#endif /* !HAVE_TERMCAP_H */
197eff741agavin
198eff741agavin#if defined(HAVE_VIS_H) && defined(HAVE_STRVIS) && defined(HAVE_STRUNVIS)
199eff741agavin# include <vis.h>
200eff741agavin#else
201eff741agavin# include "ftpvis.h"
202eff741agavin#endif
203eff741agavin
204eff741agavin#if !defined(HAVE_IN_PORT_T)
205eff741agavintypedef unsigned short in_port_t;
206eff741agavin#endif
207eff741agavin
208eff741agavin#if !defined(HAVE_SA_FAMILY_T)
209eff741agavintypedef unsigned short sa_family_t;
210eff741agavin#endif
211eff741agavin
212eff741agavin#if !defined(HAVE_SOCKLEN_T)
213eff741agavintypedef unsigned int socklen_t;
214eff741agavin#endif
215eff741agavin
216eff741agavin#if defined(USE_INET6)
217eff741agavin# define INET6
218eff741agavin#endif
219eff741agavin
220eff741agavin#if !HAVE_DECL_AI_NUMERICHOST
221eff741agavin
222eff741agavin				/* RFC 2553 */
223eff741agavin#undef	EAI_ADDRFAMILY
224eff741agavin#define	EAI_ADDRFAMILY	 1	/* address family for hostname not supported */
225eff741agavin#undef	EAI_AGAIN
226eff741agavin#define	EAI_AGAIN	 2	/* temporary failure in name resolution */
227eff741agavin#undef	EAI_BADFLAGS
228eff741agavin#define	EAI_BADFLAGS	 3	/* invalid value for ai_flags */
229eff741agavin#undef	EAI_FAIL
230eff741agavin#define	EAI_FAIL	 4	/* non-recoverable failure in name resolution */
231eff741agavin#undef	EAI_FAMILY
232eff741agavin#define	EAI_FAMILY	 5	/* ai_family not supported */
233eff741agavin#undef	EAI_MEMORY
234eff741agavin#define	EAI_MEMORY	 6	/* memory allocation failure */
235eff741agavin#undef	EAI_NODATA
236eff741agavin#define	EAI_NODATA	 7	/* no address associated with hostname */
237eff741agavin#undef	EAI_NONAME
238eff741agavin#define	EAI_NONAME	 8	/* hostname nor servname provided, or not known */
239eff741agavin#undef	EAI_SERVICE
240eff741agavin#define	EAI_SERVICE	 9	/* servname not supported for ai_socktype */
241eff741agavin#undef	EAI_SOCKTYPE
242eff741agavin#define	EAI_SOCKTYPE	10	/* ai_socktype not supported */
243eff741agavin#undef	EAI_SYSTEM
244eff741agavin#define	EAI_SYSTEM	11	/* system error returned in errno */
245eff741agavin
246eff741agavin				/* KAME extensions? */
247eff741agavin#undef	EAI_BADHINTS
248eff741agavin#define	EAI_BADHINTS	12
249eff741agavin#undef	EAI_PROTOCOL
250eff741agavin#define	EAI_PROTOCOL	13
251eff741agavin#undef	EAI_MAX
252eff741agavin#define	EAI_MAX		14
253eff741agavin
254eff741agavin				/* RFC 2553 */
255eff741agavin#undef	NI_MAXHOST
256eff741agavin#define	NI_MAXHOST	1025
257eff741agavin#undef	NI_MAXSERV
258eff741agavin#define	NI_MAXSERV	32
259eff741agavin
260eff741agavin#undef	NI_NOFQDN
261eff741agavin#define	NI_NOFQDN	0x00000001
262eff741agavin#undef	NI_NUMERICHOST
263eff741agavin#define	NI_NUMERICHOST	0x00000002
264eff741agavin#undef	NI_NAMEREQD
265eff741agavin#define	NI_NAMEREQD	0x00000004
266eff741agavin#undef	NI_NUMERICSERV
267eff741agavin#define	NI_NUMERICSERV	0x00000008
268eff741agavin#undef	NI_DGRAM
269eff741agavin#define	NI_DGRAM	0x00000010
270eff741agavin
271eff741agavin				/* RFC 2553 */
272eff741agavin#undef	AI_PASSIVE
273eff741agavin#define	AI_PASSIVE	0x00000001 /* get address to use bind() */
274eff741agavin#undef	AI_CANONNAME
275eff741agavin#define	AI_CANONNAME	0x00000002 /* fill ai_canonname */
276eff741agavin
277eff741agavin				/* KAME extensions ? */
278eff741agavin#undef	AI_NUMERICHOST
279eff741agavin#define	AI_NUMERICHOST	0x00000004 /* prevent name resolution */
280eff741agavin#undef	AI_MASK
281eff741agavin#define	AI_MASK		(AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST)
282eff741agavin
283eff741agavin				/* RFC 2553 */
284eff741agavin#undef	AI_ALL
285eff741agavin#define	AI_ALL		0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
286eff741agavin#undef	AI_V4MAPPED_CFG
287eff741agavin#define	AI_V4MAPPED_CFG	0x00000200 /* accept IPv4-mapped if kernel supports */
288eff741agavin#undef	AI_ADDRCONFIG
289eff741agavin#define	AI_ADDRCONFIG	0x00000400 /* only if any address is assigned */
290eff741agavin#undef	AI_V4MAPPED
291eff741agavin#define	AI_V4MAPPED	0x00000800 /* accept IPv4-mapped IPv6 address */
292eff741agavin
293eff741agavin#endif /* !HAVE_DECL_AI_NUMERICHOST */
294eff741agavin
295eff741agavin
296eff741agavin#if !HAVE_DECL_AI_NUMERICHOST && !defined(HAVE_STRUCT_ADDRINFO) \
297eff741agavin    && !defined(USE_SOCKS)
298eff741agavin
299eff741agavinstruct addrinfo {
300eff741agavin	int		ai_flags;	/* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
301eff741agavin	int		ai_family;	/* PF_xxx */
302eff741agavin	int		ai_socktype;	/* SOCK_xxx */
303eff741agavin	int		ai_protocol;	/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
304eff741agavin	socklen_t	ai_addrlen;	/* length of ai_addr */
305eff741agavin	char		*ai_canonname;	/* canonical name for hostname */
306eff741agavin	struct sockaddr *ai_addr;	/* binary address */
307eff741agavin	struct addrinfo *ai_next;	/* next structure in linked list */
308eff741agavin};
309eff741agavin
310eff741agavinint	getaddrinfo(const char *, const char *,
311eff741agavin	    const struct addrinfo *, struct addrinfo **);
312eff741agavinint	getnameinfo(const struct sockaddr *, socklen_t,
313eff741agavin	    char *, size_t, char *, size_t, int);
314eff741agavinvoid	freeaddrinfo(struct addrinfo *);
315eff741agavinconst char *gai_strerror(int);
316eff741agavin
317eff741agavin#endif /* !HAVE_DECL_AI_NUMERICHOST && !defined(HAVE_STRUCT_ADDRINFO) \
318eff741agavin	&& !defined(USE_SOCKS) */
319eff741agavin
320eff741agavin#if !defined(HAVE_STRUCT_DIRENT_D_NAMLEN)
321eff741agavin# define DIRENT_MISSING_D_NAMLEN
322eff741agavin#endif
323eff741agavin
324eff741agavin#if !HAVE_DECL_H_ERRNO
325eff741agavinextern int	h_errno;
326eff741agavin#endif
327eff741agavin#define HAVE_H_ERRNO	1		/* XXX: an assumption for now... */
328eff741agavin
329eff741agavin#if !HAVE_DECL_FCLOSE
330eff741agavinint	fclose(FILE *);
331eff741agavin#endif
332eff741agavin
333eff741agavin#if !HAVE_DECL_GETPASS
334eff741agavinchar	*getpass(const char *);
335eff741agavin#endif
336eff741agavin
337eff741agavin#if !HAVE_DECL_OPTARG
338eff741agavinextern char    *optarg;
339eff741agavin#endif
340eff741agavin
341eff741agavin#if !HAVE_DECL_OPTIND
342eff741agavinextern int	optind;
343eff741agavin#endif
344eff741agavin
345eff741agavin#if !HAVE_DECL_PCLOSE
346eff741agavinint	pclose(FILE *);
347eff741agavin#endif
348eff741agavin
349eff741agavin#if !HAVE_DECL_DIRNAME
350eff741agavinchar	*dirname(char *);
351eff741agavin#endif
352eff741agavin
353eff741agavin#if !defined(HAVE_ERR)
354eff741agavinvoid	err(int, const char *, ...);
355eff741agavinvoid	errx(int, const char *, ...);
356eff741agavinvoid	warn(const char *, ...);
357eff741agavinvoid	warnx(const char *, ...);
358eff741agavin#endif
359eff741agavin
360eff741agavin#if !defined(HAVE_FGETLN)
361eff741agavinchar   *fgetln(FILE *, size_t *);
362eff741agavin#endif
363eff741agavin
364eff741agavin#if !defined(HAVE_FSEEKO)
365eff741agavinint	fseeko(FILE *, off_t, int);
366eff741agavin#endif
367eff741agavin
368eff741agavin#if !defined(HAVE_INET_NTOP)
369eff741agavinconst char *inet_ntop(int, const void *, char *, socklen_t);
370eff741agavin#endif
371eff741agavin
372eff741agavin#if !defined(HAVE_INET_PTON)
373eff741agavinint inet_pton(int, const char *, void *);
374eff741agavin#endif
375eff741agavin
376eff741agavin#if !defined(HAVE_MKSTEMP)
377eff741agavinint	mkstemp(char *);
378eff741agavin#endif
379eff741agavin
380eff741agavin#if !defined(HAVE_SETPROGNAME)
381eff741agavinconst char *getprogname(void);
382eff741agavinvoid	setprogname(const char *);
383eff741agavin#endif
384eff741agavin
385eff741agavin#if !defined(HAVE_SNPRINTF)
386eff741agavinint	snprintf(char *, size_t, const char *, ...);
387eff741agavin#endif
388eff741agavin
389eff741agavin#if !defined(HAVE_STRDUP)
390eff741agavinchar   *strdup(const char *);
391eff741agavin#endif
392eff741agavin
393eff741agavin#if !defined(HAVE_STRERROR)
394eff741agavinchar   *strerror(int);
395eff741agavin#endif
396eff741agavin
397eff741agavin#if !defined(HAVE_STRPTIME) || !HAVE_DECL_STRPTIME
398eff741agavinchar   *strptime(const char *, const char *, struct tm *);
399eff741agavin#endif
400eff741agavin
401eff741agavin#if defined(HAVE_PRINTF_LONG_LONG) && defined(HAVE_LONG_LONG_INT)
402eff741agavin# if !defined(HAVE_STRTOLL)
403eff741agavinlong long strtoll(const char *, char **, int);
404eff741agavin# endif
405eff741agavin# if !defined(LLONG_MIN)
406eff741agavin#  define LLONG_MIN	(-0x7fffffffffffffffLL-1)
407eff741agavin# endif
408eff741agavin# if !defined(LLONG_MAX)
409eff741agavin#  define LLONG_MAX	(0x7fffffffffffffffLL)
410eff741agavin# endif
411eff741agavin#else  /* !(defined(HAVE_PRINTF_LONG_LONG) && defined(HAVE_LONG_LONG_INT)) */
412eff741agavin# define NO_LONG_LONG	1
413eff741agavin#endif /* !(defined(HAVE_PRINTF_LONG_LONG) && defined(HAVE_LONG_LONG_INT)) */
414eff741agavin
415eff741agavin#if !defined(HAVE_TIMEGM)
416eff741agavintime_t	timegm(struct tm *);
417eff741agavin#endif
418eff741agavin
419eff741agavin#if !defined(HAVE_STRLCAT)
420eff741agavinsize_t	strlcat(char *, const char *, size_t);
421eff741agavin#endif
422eff741agavin
423eff741agavin#if !defined(HAVE_STRLCPY)
424eff741agavinsize_t	strlcpy(char *, const char *, size_t);
425eff741agavin#endif
426eff741agavin
427eff741agavin#if !defined(HAVE_STRSEP)
428eff741agavinchar   *strsep(char **stringp, const char *delim);
429eff741agavin#endif
430eff741agavin
431eff741agavin#if !defined(HAVE_UTIMES)
432eff741agavinint utimes(const char *, const struct timeval *);
433eff741agavin#endif
434eff741agavin
435eff741agavin#if !defined(HAVE_MEMMOVE)
436eff741agavin# define memmove(a,b,c)	bcopy((b),(a),(c))
437eff741agavin	/* XXX: add others #defines for borken systems? */
438eff741agavin#endif
439eff741agavin
440eff741agavin#if defined(HAVE_GETPASSPHRASE)
441eff741agavin# define getpass getpassphrase
442eff741agavin#endif
443eff741agavin
444eff741agavin#if !defined(MIN)
445eff741agavin# define MIN(a, b)	((a) < (b) ? (a) : (b))
446eff741agavin#endif
447eff741agavin#if !defined(MAX)
448eff741agavin# define MAX(a, b)	((a) < (b) ? (b) : (a))
449eff741agavin#endif
450eff741agavin
451eff741agavin#if !defined(timersub)
452eff741agavin# define timersub(tvp, uvp, vvp)					\
453eff741agavin	do {								\
454eff741agavin		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
455eff741agavin		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
456eff741agavin		if ((vvp)->tv_usec < 0) {				\
457eff741agavin			(vvp)->tv_sec--;				\
458eff741agavin			(vvp)->tv_usec += 1000000;			\
459eff741agavin		}							\
460eff741agavin	} while (0)
461eff741agavin#endif
462eff741agavin
463eff741agavin#if !defined(S_ISLNK)
464eff741agavin# define S_ISLNK(m)	((m & S_IFMT) == S_IFLNK)
465eff741agavin#endif
466eff741agavin
467eff741agavin#define	EPOCH_YEAR	1970
468eff741agavin#define	SECSPERHOUR	3600
469eff741agavin#define	SECSPERDAY	86400
470eff741agavin#define	TM_YEAR_BASE	1900
471eff741agavin
472eff741agavin#if defined(USE_SOCKS)		/* (Dante) SOCKS5 */
473eff741agavin#define connect		Rconnect
474eff741agavin#define bind		Rbind
475eff741agavin#define getsockname	Rgetsockname
476eff741agavin#define getpeername	Rgetpeername
477eff741agavin#define accept		Raccept
478eff741agavin#define rresvport	Rrresvport
479eff741agavin#define bindresvport	Rbindresvport
480eff741agavin#define gethostbyname	Rgethostbyname
481eff741agavin#define gethostbyname2	Rgethostbyname2
482eff741agavin#define sendto		Rsendto
483eff741agavin#define recvfrom	Rrecvfrom
484eff741agavin#define recvfrom	Rrecvfrom
485eff741agavin#define write		Rwrite
486eff741agavin#define writev		Rwritev
487eff741agavin#define send		Rsend
488eff741agavin#define sendmsg		Rsendmsg
489eff741agavin#define read		Rread
490eff741agavin#define readv		Rreadv
491eff741agavin#define recv		Rrecv
492eff741agavin#define recvmsg		Rrecvmsg
493eff741agavin#define getaddrinfo	Rgetaddrinfo
494eff741agavin#define getipnodebyname	Rgetipnodebyname
495eff741agavin#endif /* defined(USE_SOCKS) */
496