xref: /trueos/usr.bin/csup/misc.h (revision bcd0e15cf642d6e5bf78ee585ad282b0e3061864)
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 
88 struct config;
89 struct stream;
90 
91 /* Thread parameters. */
92 struct 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 
106 struct backoff_timer;
107 struct pattlist;
108 struct tm;
109 
110 int		 asciitoint(const char *, int *, int);
111 int		 lprintf(int, const char *, ...) __printflike(2, 3);
112 int		 MD5_File(char *, char *);
113 void		 MD5_End(char *, MD5_CTX *);
114 int		 rcsdatetotm(const char *, struct tm *);
115 time_t		 rcsdatetotime(const char *);
116 int		 pathcmp(const char *, const char *);
117 size_t		 commonpathlength(const char *, size_t, const char *, size_t);
118 const char	*pathlast(const char *);
119 int		 isrcs(const char *, size_t *);
120 char		*checkoutpath(const char *, const char *);
121 char		*cvspath(const char *, const char *, int);
122 char		*atticpath(const char *, const char *);
123 char		*path_prefix(char *);
124 char		*path_first(char *);
125 int		 mkdirhier(char *, mode_t);
126 char		*tempname(const char *);
127 void		*xmalloc(size_t);
128 void		*xrealloc(void *, size_t);
129 char		*xstrdup(const char *);
130 int		 xasprintf(char **, const char *, ...) __printflike(2, 3);
131 int		 rcsnum_cmp(char *, char *);
132 int		 rcsrev_istrunk(char *);
133 char		*rcsrev_prefix(char *);
134 
135 struct pattlist		*pattlist_new(void);
136 void			 pattlist_add(struct pattlist *, const char *);
137 char			*pattlist_get(struct pattlist *, size_t);
138 size_t			 pattlist_size(struct pattlist *);
139 void			 pattlist_free(struct pattlist *);
140 
141 struct backoff_timer	*bt_new(time_t, time_t, float, float);
142 time_t			 bt_get(struct backoff_timer *);
143 void			 bt_pause(struct backoff_timer *);
144 void			 bt_free(struct backoff_timer *);
145 
146 #endif /* !_MISC_H_ */
147