1 /*
2  * Header file for compatibility functions.
3  *
4  * Copyright 2020 by Gray Watson
5  *
6  * This file is part of the dmalloc package.
7  *
8  * Permission to use, copy, modify, and distribute this software for
9  * any purpose and without fee is hereby granted, provided that the
10  * above copyright notice and this permission notice appear in all
11  * copies, and that the name of Gray Watson not be used in advertising
12  * or publicity pertaining to distribution of the document or software
13  * without specific, written prior permission.
14  *
15  * Gray Watson makes no representations about the suitability of the
16  * software described herein for any purpose.  It is provided "as is"
17  * without express or implied warranty.
18  *
19  * The author may be contacted via http://dmalloc.com/
20  */
21 
22 #ifndef __COMPAT_H__
23 #define __COMPAT_H__
24 
25 #if HAVE_STDARG_H
26 # include <stdarg.h>				/* for ... */
27 #endif
28 
29 #include "conf.h"				/* for HAVE... */
30 
31 /*<<<<<<<<<<  The below prototypes are auto-generated by fillproto */
32 
33 #if HAVE_ATOI == 0
34 /*
35  * Turn a ascii-string into an integer which is returned
36  */
37 extern
38 int	atoi(const char *str);
39 #endif /* if HAVE_ATOI == 0 */
40 
41 #if HAVE_ATOL == 0
42 /*
43  * Turn a ascii-string into an integer which is returned
44  */
45 extern
46 long	atol(const char *str);
47 #endif /* if HAVE_ATOL == 0 */
48 
49 /*
50  * Local ascii to unsigned long function
51  */
52 extern
53 unsigned long	loc_atoul(const char *str);
54 
55 #if HAVE_MEMCMP == 0
56 /*
57  * Compare LEN characters, return -1,0,1 if STR1 is <,==,> STR2
58  */
59 extern
60 int	memcmp(const void *str1, const void *str2, DMALLOC_SIZE len);
61 #endif /* if HAVE_MEMCMP == 0 */
62 
63 #if HAVE_MEMCPY == 0
64 /*
65  * Copy LEN characters from SRC to DEST
66  */
67 extern
68 void	*memcpy(void *dest, const void *src, DMALLOC_SIZE len);
69 #endif /* if HAVE_MEMCPY == 0 */
70 
71 #if HAVE_MEMMOVE == 0
72 /*
73  * Copy LEN characters from SRC to DEST
74  */
75 extern
76 void	*memmove(void *dest, const void *src, DMALLOC_SIZE len);
77 #endif /* if HAVE_MEMMOVE == 0 */
78 
79 #if HAVE_MEMSET == 0
80 /*
81  * Set LEN characters in STR to character CH
82  */
83 extern
84 void	*memset(void *str, const int ch, DMALLOC_SIZE len);
85 #endif /* if HAVE_MEMSET == 0 */
86 
87 #if HAVE_STRCHR == 0
88 /*
89  * Find CH in STR by searching backwards through the string
90  */
91 extern
92 char	*strchr(const char *str, const int ch);
93 #endif /* if HAVE_STRCHR == 0 */
94 
95 #if HAVE_STRCMP == 0
96 /*
97  * Returns -1,0,1 on whether STR1 is <,==,> STR2
98  */
99 extern
100 int	strcmp(const char *str1, const char *str2);
101 #endif /* if HAVE_STRCMP == 0 */
102 
103 #if HAVE_STRCPY == 0
104 /*
105  * Copies STR2 to STR1.  Returns STR1.
106  */
107 extern
108 char	*strcpy(char *str1, const char *str2);
109 #endif /* if HAVE_STRCPY == 0 */
110 
111 #if HAVE_STRLEN == 0
112 /*
113  * Return the length in characters of STR
114  */
115 extern
116 int	strlen(const char *str);
117 #endif /* if HAVE_STRLEN == 0 */
118 
119 #if HAVE_STRNLEN == 0
120 /*
121  * Return the length in characters of STR limited by MAX_LENGTH.
122  */
123 extern
124 int	strnlen(const char *str, const int max_length);
125 #endif /* if HAVE_STRNLEN == 0 */
126 
127 #if HAVE_STRNCMP == 0
128 /*
129  * Compare at most LEN chars in STR1 and STR2 and return -1,0,1 or
130  * STR1 - STR2
131  */
132 extern
133 int	strncmp(const char *str1, const char *str2, const int len);
134 #endif /* if HAVE_STRNCMP == 0 */
135 
136 #if HAVE_STRNCPY == 0
137 /*
138  * Copy STR2 to STR1 until LEN or null
139  */
140 extern
141 char	*strncpy(char *str1, const char *str2, const int len);
142 #endif /* if HAVE_STRNCPY == 0 */
143 
144 #if HAVE_STRRCHR == 0
145 /*
146  * Find CH in STR by searching backwards through the string
147  */
148 extern
149 char	*strrchr(const char *str, const int ch);
150 #endif /* if HAVE_STRRCHR == 0 */
151 
152 #if HAVE_STRSEP == 0
153 /*
154  * char *strsep
155  *
156  * This is a function which should be in libc in every Unix.  Grumble.
157  * It basically replaces the strtok function because it is reentrant.
158  * This tokenizes a string by returning the next token in a string and
159  * punching a \0 on the first delimiter character past the token.  The
160  * difference from strtok is that you pass in the address of a string
161  * pointer which will be shifted allong the buffer being processed.
162  * With strtok you passed in a 0L for subsequant calls.  Yeach.
163  *
164  * This will count the true number of delimiter characters in the string
165  * and will return an empty token (one with \0 in the zeroth position)
166  * if there are two delimiter characters in a row.
167  *
168  * Consider the following example:
169  *
170  * char *tok, *str_p = "1,2,3, hello there ";
171  *
172  * while (1) { tok = strsep(&str_p, " ,"); if (tok == 0L) { break; } }
173  *
174  * strsep will return as tokens: "1", "2", "3", "", "hello", "there", "".
175  * Notice the two empty "" tokens where there were two delimiter
176  * characters in a row ", " and at the end of the string where there
177  * was an extra delimiter character.  If you want to ignore these
178  * tokens then add a test to see if the first character of the token
179  * is \0.
180  *
181  * RETURNS:
182  *
183  * Success - Pointer to the next delimited token in the string.
184  *
185  * Failure - 0L if there are no more tokens.
186  *
187  * ARGUMENTS:
188  *
189  * string_p - Pointer to a string pointer which will be searched for
190  * delimiters.  \0's will be added to this buffer.
191  *
192  * delim - List of delimiter characters which separate our tokens.  It
193  * does not have to remain constant through all calls across the same
194  * string.
195  */
196 extern
197 char	*strsep(char **string_p, const char *delim);
198 #endif /* if HAVE_STRSEP == 0 */
199 
200 /*
201  * Local getenv which handles some portability stuff.
202  */
203 extern
204 char	*loc_getenv(const char *var, char *buf, const int buf_size,
205 		    const int stay_safe);
206 
207 /*<<<<<<<<<<   This is end of the auto-generated output from fillproto. */
208 
209 #endif /* ! __COMPAT_H__ */
210