1 /*-------------------------------------------------------------------------
2    string.h - ISO header for string library functions
3 
4    Copyright (C) 1998, Sandeep Dutta
5    Copyright (C) 2009-2019, Philipp Klaus Krause pkk@spth.de
6 
7    This library is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11 
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this library; see the file COPYING. If not, write to the
19    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20    MA 02110-1301, USA.
21 
22    As a special exception, if you link this library with other files,
23    some of which are compiled with SDCC, to produce an executable,
24    this library does not by itself cause the resulting executable to
25    be covered by the GNU General Public License. This exception does
26    not however invalidate any other reasons why the executable file
27    might be covered by the GNU General Public License.
28 -------------------------------------------------------------------------*/
29 
30 #ifndef __SDCC_STRING_H
31 #define __SDCC_STRING_H 1
32 
33 #ifndef NULL
34 # define NULL (void *)0
35 #endif
36 
37 #ifndef __SIZE_T_DEFINED
38 # define __SIZE_T_DEFINED
39   typedef unsigned int size_t;
40 #endif
41 
42 /* Bounds-checking interfaces from annex K of the C11 standard. */
43 #if defined (__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
44 
45 #ifndef __RSIZE_T_DEFINED
46 #define __RSIZE_T_DEFINED
47 typedef size_t rsize_t;
48 #endif
49 
50 #ifndef __ERRNO_T_DEFINED
51 #define __ERRNO_T_DEFINED
52 typedef int errno_t;
53 #endif
54 
55 #endif
56 
57 #if defined(__SDCC_mcs51) || defined(__SDCC_hc08) || defined(__SDCC_ds390) || defined(__SDCC_pic14) || defined(__SDCC_pic16)
58 #define __SDCC_BROKEN_STRING_FUNCTIONS
59 #endif
60 
61 /* The function prototypes are ordered as in the ISO C11 standard. */
62 
63 /* Todo: fix the "restrict" stuff for C99 compliance. */
64 
65 /* Copying functions: */
66 void *memccpy (void */*restrict*/ dst, const void */*restict*/ src, int c, size_t n);
67 extern void *memcpy (void * /*restrict */ dest, const void * /*restrict*/ src, size_t n);
68 #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r3ka)
69 extern void *memmove (void *dest, const void *src, size_t n) __preserves_regs(iyl, iyh);
70 #else
71 extern void *memmove (void *dest, const void *src, size_t n);
72 #endif
73 #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r3ka) || defined(__SDCC_tlcs90) || defined (__SDCC_ez80_z80)
74 extern char *strcpy (char * /*restrict*/ dest, const char * /*restrict*/ src) __preserves_regs(iyl, iyh);
75 #else
76 extern char *strcpy (char * /*restrict*/ dest, const char * /*restrict*/ src);
77 #endif
78 extern char *strncpy (char * /*restrict*/ dest, const char * /*restrict*/ src, size_t n);
79 
80 /* Concatenation functions: */
81 extern char *strcat (char * /*restrict*/ dest, const char * /*restrict*/ src);
82 extern char *strncat (char * /*restrict*/ dest, const char * /*restrict*/ src, size_t n);
83 
84 /* C2X Duplication functions */
85 extern char *strdup (const char *s);
86 extern char *strndup (const char *s, size_t n);
87 
88 /* Comparison functions: */
89 extern int memcmp (const void *s1, const void *s2, size_t n);
90 extern int strcmp (const char *s1, const char *s2);
91 #define strcoll(s1, s2) strcmp(s1, s2)
92 /*int strcoll(const char *s1, const char *s2) {return strcmp(s1, s2);}*/
93 extern int strncmp (const char *s1, const char *s2, size_t n);
94 extern size_t strxfrm (char *dest, const char *src, size_t n);
95 
96 /* Search functions: */
97 extern void *memchr (const void *s, int c, size_t n);
98 #ifdef __SDCC_BROKEN_STRING_FUNCTIONS
99 extern char *strchr (const char *s, char c); /* c should be int according to standard. */
100 #else
101 extern char *strchr (const char *s, int c);
102 #endif
103 extern size_t strcspn(const char *s, const char *reject);
104 extern char *strpbrk(const char *s, const char *accept);
105 #ifdef __SDCC_BROKEN_STRING_FUNCTIONS
106 extern char *strrchr(const char *s, char c); /* c should be int according to standard. */
107 #else
108 extern char *strrchr(const char *s, int c);
109 #endif
110 extern size_t strspn (const char *s, const char *accept);
111 extern char *strstr (const char *haystack, const char *needle);
112 extern char *strtok (char * /* restrict*/ str, const char * /*restrict*/ delim);
113 
114 /* Miscanelleous functions: */
115 #ifdef __SDCC_BROKEN_STRING_FUNCTIONS
116 extern void *memset (void *s, unsigned char c, size_t n); /* c should be int according to standard. */
117 #else
118 extern void *memset (void *s, int c, size_t n);
119 #endif
120 
121 /* extern char *strerror(int errnum); */
122 #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_tlcs90) || defined (__SDCC_ez80_z80)
123 extern size_t strlen (const char *s) __preserves_regs(d, e, iyl, iyh);
124 #else
125 extern size_t strlen (const char *s);
126 #endif
127 
128 #ifdef __SDCC_ds390
129 extern void __xdata * memcpyx(void __xdata *, void __xdata *, int) __naked;
130 #endif
131 
132 #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r3ka) || defined (__SDCC_ez80_z80)
133 #define memcpy(dst, src, n) __builtin_memcpy(dst, src, n)
134 #define strcpy(dst, src) __builtin_strcpy(dst, src)
135 #define strncpy(dst, src, n) __builtin_strncpy(dst, src, n)
136 #define strchr(s, c) __builtin_strchr(s, c)
137 #define memset(dst, c, n) __builtin_memset(dst, c, n)
138 #else
139 extern void *__memcpy (void * /*restrict */ dest, const void * /*restrict*/ src, size_t n);
140 #define memcpy(dst, src, n) __memcpy(dst, src, n)
141 #endif
142 
143 #endif
144 
145