1/*-
2 * Copyright (c) 2003-2006, Maxime Henrion <mux@FreeBSD.org>
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28#ifndef _MISC_H_
29#define _MISC_H_
30
31#include <sys/types.h>
32
33#ifdef __FreeBSD__
34#include <md5.h>
35#define	MD5_DIGEST_LENGTH	16
36#define	MD5_Init		MD5Init
37#define	MD5_Final		MD5Final
38#define	MD5_Update		MD5Update
39#else
40#include <openssl/md5.h>
41#endif
42
43/* If we're not compiling in a C99 environment, define the C99 types. */
44#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
45
46#ifdef uint32_t
47#undef uint32_t
48#endif
49#define	uint32_t	u_int32_t
50
51#ifdef uint16_t
52#undef uint16_t
53#endif
54#define	uint16_t	u_int16_t
55
56#ifdef uint8_t
57#undef uint8_t
58#endif
59#define	uint8_t		u_int8_t
60
61#else
62#include <stdint.h>
63#endif
64
65/* This is a GCC-specific keyword but some other compilers (namely icc)
66   understand it, and the code won't work if we can't disable padding
67   anyways. */
68#undef __packed
69#define	__packed		__attribute__((__packed__))
70
71/* We explicitely don't define this with icc because it defines __GNUC__
72   but doesn't support it. */
73#undef __printflike
74#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
75    (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC__MINOR__ >= 7)
76#define	__printflike(fmtarg, firstvararg) \
77	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
78#else
79#define	__printflike(fmtarg, firstvararg)
80#endif
81
82/* Exit codes. */
83#define	STATUS_SUCCESS		0
84#define	STATUS_FAILURE		1
85#define	STATUS_TRANSIENTFAILURE	2
86#define	STATUS_INTERRUPTED	3
87
88struct config;
89struct stream;
90
91/* Thread parameters. */
92struct thread_args {
93	struct config *config;
94	struct stream *rd;
95	struct stream *wr;
96	int status;
97	char *errmsg;
98};
99
100/* Minimum size for MD5_File() and MD5_End() buffers. */
101#define	MD5_DIGEST_SIZE		33
102
103#define	min(a, b)		((a) > (b) ? (b) : (a))
104#define	max(a, b)		((a) < (b) ? (b) : (a))
105
106struct backoff_timer;
107struct pattlist;
108struct tm;
109
110int		 asciitoint(const char *, int *, int);
111int		 lprintf(int, const char *, ...) __printflike(2, 3);
112int		 MD5_File(char *, char *);
113void		 MD5_End(char *, MD5_CTX *);
114int		 rcsdatetotm(const char *, struct tm *);
115time_t		 rcsdatetotime(const char *);
116int		 pathcmp(const char *, const char *);
117size_t		 commonpathlength(const char *, size_t, const char *, size_t);
118const char	*pathlast(const char *);
119int		 isrcs(const char *, size_t *);
120char		*checkoutpath(const char *, const char *);
121char		*cvspath(const char *, const char *, int);
122char		*atticpath(const char *, const char *);
123char		*path_prefix(char *);
124char		*path_first(char *);
125int		 mkdirhier(char *, mode_t);
126char		*tempname(const char *);
127void		*xmalloc(size_t);
128void		*xrealloc(void *, size_t);
129char		*xstrdup(const char *);
130int		 xasprintf(char **, const char *, ...) __printflike(2, 3);
131int		 rcsnum_cmp(char *, char *);
132int		 rcsrev_istrunk(char *);
133char		*rcsrev_prefix(char *);
134
135struct pattlist		*pattlist_new(void);
136void			 pattlist_add(struct pattlist *, const char *);
137char			*pattlist_get(struct pattlist *, size_t);
138size_t			 pattlist_size(struct pattlist *);
139void			 pattlist_free(struct pattlist *);
140
141struct backoff_timer	*bt_new(time_t, time_t, float, float);
142time_t			 bt_get(struct backoff_timer *);
143void			 bt_pause(struct backoff_timer *);
144void			 bt_free(struct backoff_timer *);
145
146#endif /* !_MISC_H_ */
147