string2.h 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /* Machine-independant string function optimizations.
  2. Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #ifndef _STRING_H
  18. # error "Never use <bits/string2.h> directly; include <string.h> instead."
  19. #endif
  20. #ifndef __NO_STRING_INLINES
  21. /* Unlike the definitions in the header <bits/string.h> the
  22. definitions contained here are not optimized down to assembler
  23. level. Those optimizations are not always a good idea since this
  24. means the code size increases a lot. Instead the definitions here
  25. optimize some functions in a way which do not dramatically
  26. increase the code size and which do not use assembler. The main
  27. trick is to use GNU CC's `__builtin_constant_p' function.
  28. Every function XXX which has a defined version in
  29. <bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
  30. to make sure we don't get redefinitions.
  31. We must use here macros instead of inline functions since the
  32. trick won't work with the latter. */
  33. #ifndef __STRING_INLINE
  34. # ifdef __cplusplus
  35. # define __STRING_INLINE inline
  36. # else
  37. # define __STRING_INLINE extern __inline
  38. # endif
  39. #endif
  40. #if _STRING_ARCH_unaligned
  41. /* If we can do unaligned memory accesses we must know the endianess. */
  42. # include <endian.h>
  43. # include <bits/types.h>
  44. # if __BYTE_ORDER == __LITTLE_ENDIAN
  45. # define __STRING2_SMALL_GET16(src, idx) \
  46. (((__const unsigned char *) (__const char *) (src))[idx + 1] << 8 \
  47. | ((__const unsigned char *) (__const char *) (src))[idx])
  48. # define __STRING2_SMALL_GET32(src, idx) \
  49. (((((__const unsigned char *) (__const char *) (src))[idx + 3] << 8 \
  50. | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8 \
  51. | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8 \
  52. | ((__const unsigned char *) (__const char *) (src))[idx])
  53. # else
  54. # define __STRING2_SMALL_GET16(src, idx) \
  55. (((__const unsigned char *) (__const char *) (src))[idx] << 8 \
  56. | ((__const unsigned char *) (__const char *) (src))[idx + 1])
  57. # define __STRING2_SMALL_GET32(src, idx) \
  58. (((((__const unsigned char *) (__const char *) (src))[idx] << 8 \
  59. | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8 \
  60. | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8 \
  61. | ((__const unsigned char *) (__const char *) (src))[idx + 3])
  62. # endif
  63. #else
  64. /* These are a few types we need for the optimizations if we cannot
  65. use unaligned memory accesses. */
  66. # define __STRING2_COPY_TYPE(N) \
  67. typedef struct { unsigned char __arr[N]; } \
  68. __STRING2_COPY_ARR##N __attribute__ ((packed))
  69. __STRING2_COPY_TYPE (2);
  70. __STRING2_COPY_TYPE (3);
  71. __STRING2_COPY_TYPE (4);
  72. __STRING2_COPY_TYPE (5);
  73. __STRING2_COPY_TYPE (6);
  74. __STRING2_COPY_TYPE (7);
  75. __STRING2_COPY_TYPE (8);
  76. # undef __STRING2_COPY_TYPE
  77. #endif
  78. /* Dereferencing a pointer arg to run sizeof on it fails for the void
  79. pointer case, so we use this instead.
  80. Note that __x is evaluated twice. */
  81. #define __string2_1bptr_p(__x) \
  82. ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
  83. /* Set N bytes of S to C. */
  84. #ifndef _HAVE_STRING_ARCH_memset
  85. # if _STRING_ARCH_unaligned
  86. # define memset(s, c, n) \
  87. (__extension__ (__builtin_constant_p (n) && (n) <= 16 \
  88. ? ((n) == 1 \
  89. ? __memset_1 (s, c) \
  90. : __memset_gc (s, c, n)) \
  91. : (__builtin_constant_p (c) && (c) == '\0' \
  92. ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
  93. : memset (s, c, n))))
  94. # define __memset_1(s, c) ({ void *__s = (s); \
  95. *((__uint8_t *) __s) = (__uint8_t) c; __s; })
  96. # define __memset_gc(s, c, n) \
  97. ({ void *__s = (s); \
  98. union { \
  99. unsigned int __ui; \
  100. unsigned short int __usi; \
  101. unsigned char __uc; \
  102. } *__u = __s; \
  103. __uint8_t __c = (__uint8_t) (c); \
  104. \
  105. /* This `switch' statement will be removed at compile-time. */ \
  106. switch ((unsigned int) (n)) \
  107. { \
  108. case 15: \
  109. __u->__ui = __c * 0x01010101; \
  110. __u = __extension__ ((void *) __u + 4); \
  111. case 11: \
  112. __u->__ui = __c * 0x01010101; \
  113. __u = __extension__ ((void *) __u + 4); \
  114. case 7: \
  115. __u->__ui = __c * 0x01010101; \
  116. __u = __extension__ ((void *) __u + 4); \
  117. case 3: \
  118. __u->__usi = (unsigned short int) __c * 0x0101; \
  119. __u = __extension__ ((void *) __u + 2); \
  120. __u->__uc = (unsigned char) __c; \
  121. break; \
  122. \
  123. case 14: \
  124. __u->__ui = __c * 0x01010101; \
  125. __u = __extension__ ((void *) __u + 4); \
  126. case 10: \
  127. __u->__ui = __c * 0x01010101; \
  128. __u = __extension__ ((void *) __u + 4); \
  129. case 6: \
  130. __u->__ui = __c * 0x01010101; \
  131. __u = __extension__ ((void *) __u + 4); \
  132. case 2: \
  133. __u->__usi = (unsigned short int) __c * 0x0101; \
  134. break; \
  135. \
  136. case 13: \
  137. __u->__ui = __c * 0x01010101; \
  138. __u = __extension__ ((void *) __u + 4); \
  139. case 9: \
  140. __u->__ui = __c * 0x01010101; \
  141. __u = __extension__ ((void *) __u + 4); \
  142. case 5: \
  143. __u->__ui = __c * 0x01010101; \
  144. __u = __extension__ ((void *) __u + 4); \
  145. case 1: \
  146. __u->__uc = (unsigned char) __c; \
  147. break; \
  148. \
  149. case 16: \
  150. __u->__ui = __c * 0x01010101; \
  151. __u = __extension__ ((void *) __u + 4); \
  152. case 12: \
  153. __u->__ui = __c * 0x01010101; \
  154. __u = __extension__ ((void *) __u + 4); \
  155. case 8: \
  156. __u->__ui = __c * 0x01010101; \
  157. __u = __extension__ ((void *) __u + 4); \
  158. case 4: \
  159. __u->__ui = __c * 0x01010101; \
  160. case 0: \
  161. break; \
  162. } \
  163. \
  164. __s; })
  165. # else
  166. # define memset(s, c, n) \
  167. (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
  168. ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
  169. : memset (s, c, n)))
  170. # endif
  171. /* GCC optimizes memset(s, 0, n) but not bzero(s, n).
  172. The optimization is broken before EGCS 1.1. */
  173. # if defined __GNUC__ \
  174. && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 90))
  175. # define __bzero(s, n) __builtin_memset (s, '\0', n)
  176. # endif
  177. #endif
  178. /* Copy N bytes from SRC to DEST, returning pointer to byte following the
  179. last copied. */
  180. #ifdef __USE_GNU
  181. # ifndef _HAVE_STRING_ARCH_mempcpy
  182. # define __mempcpy(dest, src, n) \
  183. (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
  184. && __string2_1bptr_p (src) && n <= 8 \
  185. ? __mempcpy_small (dest, __mempcpy_args (src), n) \
  186. : __mempcpy (dest, src, n)))
  187. /* In glibc we use this function frequently but for namespace reasons
  188. we have to use the name `__mempcpy'. */
  189. # define mempcpy(dest, src, n) __mempcpy (dest, src, n)
  190. # if _STRING_ARCH_unaligned
  191. # define __mempcpy_args(src) \
  192. ((char *) (src))[0], ((char *) (src))[2], ((char *) (src))[4], \
  193. ((char *) (src))[6], \
  194. __extension__ __STRING2_SMALL_GET16 (src, 0), \
  195. __extension__ __STRING2_SMALL_GET16 (src, 4), \
  196. __extension__ __STRING2_SMALL_GET32 (src, 0), \
  197. __extension__ __STRING2_SMALL_GET32 (src, 4)
  198. __STRING_INLINE void *__mempcpy_small (void *, char, char, char, char,
  199. __uint16_t, __uint16_t, __uint32_t,
  200. __uint32_t, size_t);
  201. __STRING_INLINE void *
  202. __mempcpy_small (void *__dest1,
  203. char __src0_1, char __src2_1, char __src4_1, char __src6_1,
  204. __uint16_t __src0_2, __uint16_t __src4_2,
  205. __uint32_t __src0_4, __uint32_t __src4_4,
  206. size_t __srclen)
  207. {
  208. union {
  209. __uint32_t __ui;
  210. __uint16_t __usi;
  211. unsigned char __uc;
  212. unsigned char __c;
  213. } *__u = __dest1;
  214. switch ((unsigned int) __srclen)
  215. {
  216. case 1:
  217. __u->__c = __src0_1;
  218. __u = __extension__ ((void *) __u + 1);
  219. break;
  220. case 2:
  221. __u->__usi = __src0_2;
  222. __u = __extension__ ((void *) __u + 2);
  223. break;
  224. case 3:
  225. __u->__usi = __src0_2;
  226. __u = __extension__ ((void *) __u + 2);
  227. __u->__c = __src2_1;
  228. __u = __extension__ ((void *) __u + 1);
  229. break;
  230. case 4:
  231. __u->__ui = __src0_4;
  232. __u = __extension__ ((void *) __u + 4);
  233. break;
  234. case 5:
  235. __u->__ui = __src0_4;
  236. __u = __extension__ ((void *) __u + 4);
  237. __u->__c = __src4_1;
  238. __u = __extension__ ((void *) __u + 1);
  239. break;
  240. case 6:
  241. __u->__ui = __src0_4;
  242. __u = __extension__ ((void *) __u + 4);
  243. __u->__usi = __src4_2;
  244. __u = __extension__ ((void *) __u + 2);
  245. break;
  246. case 7:
  247. __u->__ui = __src0_4;
  248. __u = __extension__ ((void *) __u + 4);
  249. __u->__usi = __src4_2;
  250. __u = __extension__ ((void *) __u + 2);
  251. __u->__c = __src6_1;
  252. __u = __extension__ ((void *) __u + 1);
  253. break;
  254. case 8:
  255. __u->__ui = __src0_4;
  256. __u = __extension__ ((void *) __u + 4);
  257. __u->__ui = __src4_4;
  258. __u = __extension__ ((void *) __u + 4);
  259. break;
  260. }
  261. return (void *) __u;
  262. }
  263. # else
  264. # define __mempcpy_args(src) \
  265. ((__const char *) (src))[0], \
  266. __extension__ ((__STRING2_COPY_ARR2) \
  267. { { ((__const char *) (src))[0], ((__const char *) (src))[1] } }), \
  268. __extension__ ((__STRING2_COPY_ARR3) \
  269. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  270. ((__const char *) (src))[2] } }), \
  271. __extension__ ((__STRING2_COPY_ARR4) \
  272. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  273. ((__const char *) (src))[2], ((__const char *) (src))[3] } }), \
  274. __extension__ ((__STRING2_COPY_ARR5) \
  275. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  276. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  277. ((__const char *) (src))[4] } }), \
  278. __extension__ ((__STRING2_COPY_ARR6) \
  279. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  280. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  281. ((__const char *) (src))[4], ((__const char *) (src))[5] } }), \
  282. __extension__ ((__STRING2_COPY_ARR7) \
  283. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  284. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  285. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  286. ((__const char *) (src))[6] } }), \
  287. __extension__ ((__STRING2_COPY_ARR8) \
  288. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  289. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  290. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  291. ((__const char *) (src))[6], ((__const char *) (src))[7] } })
  292. __STRING_INLINE void *__mempcpy_small (void *, char, __STRING2_COPY_ARR2,
  293. __STRING2_COPY_ARR3,
  294. __STRING2_COPY_ARR4,
  295. __STRING2_COPY_ARR5,
  296. __STRING2_COPY_ARR6,
  297. __STRING2_COPY_ARR7,
  298. __STRING2_COPY_ARR8, size_t);
  299. __STRING_INLINE void *
  300. __mempcpy_small (void *__dest, char __src1,
  301. __STRING2_COPY_ARR2 __src2, __STRING2_COPY_ARR3 __src3,
  302. __STRING2_COPY_ARR4 __src4, __STRING2_COPY_ARR5 __src5,
  303. __STRING2_COPY_ARR6 __src6, __STRING2_COPY_ARR7 __src7,
  304. __STRING2_COPY_ARR8 __src8, size_t __srclen)
  305. {
  306. union {
  307. char __c;
  308. __STRING2_COPY_ARR2 __sca2;
  309. __STRING2_COPY_ARR3 __sca3;
  310. __STRING2_COPY_ARR4 __sca4;
  311. __STRING2_COPY_ARR5 __sca5;
  312. __STRING2_COPY_ARR6 __sca6;
  313. __STRING2_COPY_ARR7 __sca7;
  314. __STRING2_COPY_ARR8 __sca8;
  315. } *__u = __dest;
  316. switch ((unsigned int) __srclen)
  317. {
  318. case 1:
  319. __u->__c = __src1;
  320. break;
  321. case 2:
  322. __extension__ __u->__sca2 = __src2;
  323. break;
  324. case 3:
  325. __extension__ __u->__sca3 = __src3;
  326. break;
  327. case 4:
  328. __extension__ __u->__sca4 = __src4;
  329. break;
  330. case 5:
  331. __extension__ __u->__sca5 = __src5;
  332. break;
  333. case 6:
  334. __extension__ __u->__sca6 = __src6;
  335. break;
  336. case 7:
  337. __extension__ __u->__sca7 = __src7;
  338. break;
  339. case 8:
  340. __extension__ __u->__sca8 = __src8;
  341. break;
  342. }
  343. return __extension__ ((void *) __u + __srclen);
  344. }
  345. # endif
  346. # endif
  347. #endif
  348. /* Return pointer to C in S. */
  349. #ifndef _HAVE_STRING_ARCH_strchr
  350. extern __ptr_t __rawmemchr (const __ptr_t __s, int __c);
  351. # define strchr(s, c) \
  352. (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
  353. ? (char *) __rawmemchr (s, c) \
  354. : strchr (s, c)))
  355. #endif
  356. /* Copy SRC to DEST. */
  357. #ifndef _HAVE_STRING_ARCH_strcpy
  358. # define strcpy(dest, src) \
  359. (__extension__ (__builtin_constant_p (src) \
  360. ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
  361. ? __strcpy_small (dest, __strcpy_args (src), \
  362. strlen (src) + 1) \
  363. : (char *) memcpy (dest, src, strlen (src) + 1)) \
  364. : strcpy (dest, src)))
  365. # if _STRING_ARCH_unaligned
  366. # define __strcpy_args(src) \
  367. __extension__ __STRING2_SMALL_GET16 (src, 0), \
  368. __extension__ __STRING2_SMALL_GET16 (src, 4), \
  369. __extension__ __STRING2_SMALL_GET32 (src, 0), \
  370. __extension__ __STRING2_SMALL_GET32 (src, 4)
  371. __STRING_INLINE char *__strcpy_small (char *, __uint16_t, __uint16_t,
  372. __uint32_t, __uint32_t, size_t);
  373. __STRING_INLINE char *
  374. __strcpy_small (char *__dest,
  375. __uint16_t __src0_2, __uint16_t __src4_2,
  376. __uint32_t __src0_4, __uint32_t __src4_4,
  377. size_t __srclen)
  378. {
  379. union {
  380. __uint32_t __ui;
  381. __uint16_t __usi;
  382. unsigned char __uc;
  383. } *__u = (void *) __dest;
  384. switch ((unsigned int) __srclen)
  385. {
  386. case 1:
  387. __u->__uc = '\0';
  388. break;
  389. case 2:
  390. __u->__usi = __src0_2;
  391. break;
  392. case 3:
  393. __u->__usi = __src0_2;
  394. __u = __extension__ ((void *) __u + 2);
  395. __u->__uc = '\0';
  396. break;
  397. case 4:
  398. __u->__ui = __src0_4;
  399. break;
  400. case 5:
  401. __u->__ui = __src0_4;
  402. __u = __extension__ ((void *) __u + 4);
  403. __u->__uc = '\0';
  404. break;
  405. case 6:
  406. __u->__ui = __src0_4;
  407. __u = __extension__ ((void *) __u + 4);
  408. __u->__usi = __src4_2;
  409. break;
  410. case 7:
  411. __u->__ui = __src0_4;
  412. __u = __extension__ ((void *) __u + 4);
  413. __u->__usi = __src4_2;
  414. __u = __extension__ ((void *) __u + 2);
  415. __u->__uc = '\0';
  416. break;
  417. case 8:
  418. __u->__ui = __src0_4;
  419. __u = __extension__ ((void *) __u + 4);
  420. __u->__ui = __src4_4;
  421. break;
  422. }
  423. return __dest;
  424. }
  425. # else
  426. # define __strcpy_args(src) \
  427. __extension__ ((__STRING2_COPY_ARR2) \
  428. { { ((__const char *) (src))[0], '\0' } }), \
  429. __extension__ ((__STRING2_COPY_ARR3) \
  430. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  431. '\0' } }), \
  432. __extension__ ((__STRING2_COPY_ARR4) \
  433. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  434. ((__const char *) (src))[2], '\0' } }), \
  435. __extension__ ((__STRING2_COPY_ARR5) \
  436. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  437. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  438. '\0' } }), \
  439. __extension__ ((__STRING2_COPY_ARR6) \
  440. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  441. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  442. ((__const char *) (src))[4], '\0' } }), \
  443. __extension__ ((__STRING2_COPY_ARR7) \
  444. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  445. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  446. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  447. '\0' } }), \
  448. __extension__ ((__STRING2_COPY_ARR8) \
  449. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  450. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  451. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  452. ((__const char *) (src))[6], '\0' } })
  453. __STRING_INLINE char *__strcpy_small (char *, __STRING2_COPY_ARR2,
  454. __STRING2_COPY_ARR3,
  455. __STRING2_COPY_ARR4,
  456. __STRING2_COPY_ARR5,
  457. __STRING2_COPY_ARR6,
  458. __STRING2_COPY_ARR7,
  459. __STRING2_COPY_ARR8, size_t);
  460. __STRING_INLINE char *
  461. __strcpy_small (char *__dest,
  462. __STRING2_COPY_ARR2 __src2, __STRING2_COPY_ARR3 __src3,
  463. __STRING2_COPY_ARR4 __src4, __STRING2_COPY_ARR5 __src5,
  464. __STRING2_COPY_ARR6 __src6, __STRING2_COPY_ARR7 __src7,
  465. __STRING2_COPY_ARR8 __src8, size_t __srclen)
  466. {
  467. union {
  468. char __c;
  469. __STRING2_COPY_ARR2 __sca2;
  470. __STRING2_COPY_ARR3 __sca3;
  471. __STRING2_COPY_ARR4 __sca4;
  472. __STRING2_COPY_ARR5 __sca5;
  473. __STRING2_COPY_ARR6 __sca6;
  474. __STRING2_COPY_ARR7 __sca7;
  475. __STRING2_COPY_ARR8 __sca8;
  476. } *__u = (void *) __dest;
  477. switch ((unsigned int) __srclen)
  478. {
  479. case 1:
  480. __u->__c = '\0';
  481. break;
  482. case 2:
  483. __extension__ __u->__sca2 = __src2;
  484. break;
  485. case 3:
  486. __extension__ __u->__sca3 = __src3;
  487. break;
  488. case 4:
  489. __extension__ __u->__sca4 = __src4;
  490. break;
  491. case 5:
  492. __extension__ __u->__sca5 = __src5;
  493. break;
  494. case 6:
  495. __extension__ __u->__sca6 = __src6;
  496. break;
  497. case 7:
  498. __extension__ __u->__sca7 = __src7;
  499. break;
  500. case 8:
  501. __extension__ __u->__sca8 = __src8;
  502. break;
  503. }
  504. return __dest;
  505. }
  506. # endif
  507. #endif
  508. /* Copy SRC to DEST, returning pointer to final NUL byte. */
  509. #ifdef __USE_GNU
  510. # ifndef _HAVE_STRING_ARCH_stpcpy
  511. # define __stpcpy(dest, src) \
  512. (__extension__ (__builtin_constant_p (src) \
  513. ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
  514. ? __stpcpy_small (dest, __stpcpy_args (src), \
  515. strlen (src) + 1) \
  516. : ((char *) __mempcpy (dest, src, strlen (src) + 1) - 1))\
  517. : __stpcpy (dest, src)))
  518. /* In glibc we use this function frequently but for namespace reasons
  519. we have to use the name `__stpcpy'. */
  520. # define stpcpy(dest, src) __stpcpy (dest, src)
  521. # if _STRING_ARCH_unaligned
  522. # define __stpcpy_args(src) \
  523. __extension__ __STRING2_SMALL_GET16 (src, 0), \
  524. __extension__ __STRING2_SMALL_GET16 (src, 4), \
  525. __extension__ __STRING2_SMALL_GET32 (src, 0), \
  526. __extension__ __STRING2_SMALL_GET32 (src, 4)
  527. __STRING_INLINE char *__stpcpy_small (char *, __uint16_t, __uint16_t,
  528. __uint32_t, __uint32_t, size_t);
  529. __STRING_INLINE char *
  530. __stpcpy_small (char *__dest,
  531. __uint16_t __src0_2, __uint16_t __src4_2,
  532. __uint32_t __src0_4, __uint32_t __src4_4,
  533. size_t __srclen)
  534. {
  535. union {
  536. unsigned int __ui;
  537. unsigned short int __usi;
  538. unsigned char __uc;
  539. char __c;
  540. } *__u = (void *) __dest;
  541. switch ((unsigned int) __srclen)
  542. {
  543. case 1:
  544. __u->__uc = '\0';
  545. break;
  546. case 2:
  547. __u->__usi = __src0_2;
  548. __u = __extension__ ((void *) __u + 1);
  549. break;
  550. case 3:
  551. __u->__usi = __src0_2;
  552. __u = __extension__ ((void *) __u + 2);
  553. __u->__uc = '\0';
  554. break;
  555. case 4:
  556. __u->__ui = __src0_4;
  557. __u = __extension__ ((void *) __u + 3);
  558. break;
  559. case 5:
  560. __u->__ui = __src0_4;
  561. __u = __extension__ ((void *) __u + 4);
  562. __u->__uc = '\0';
  563. break;
  564. case 6:
  565. __u->__ui = __src0_4;
  566. __u = __extension__ ((void *) __u + 4);
  567. __u->__usi = __src4_2;
  568. __u = __extension__ ((void *) __u + 1);
  569. break;
  570. case 7:
  571. __u->__ui = __src0_4;
  572. __u = __extension__ ((void *) __u + 4);
  573. __u->__usi = __src4_2;
  574. __u = __extension__ ((void *) __u + 2);
  575. __u->__uc = '\0';
  576. break;
  577. case 8:
  578. __u->__ui = __src0_4;
  579. __u = __extension__ ((void *) __u + 4);
  580. __u->__ui = __src4_4;
  581. __u = __extension__ ((void *) __u + 3);
  582. break;
  583. }
  584. return &__u->__c;
  585. }
  586. # else
  587. # define __stpcpy_args(src) \
  588. __extension__ ((__STRING2_COPY_ARR2) \
  589. { { ((__const char *) (src))[0], '\0' } }), \
  590. __extension__ ((__STRING2_COPY_ARR3) \
  591. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  592. '\0' } }), \
  593. __extension__ ((__STRING2_COPY_ARR4) \
  594. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  595. ((__const char *) (src))[2], '\0' } }), \
  596. __extension__ ((__STRING2_COPY_ARR5) \
  597. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  598. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  599. '\0' } }), \
  600. __extension__ ((__STRING2_COPY_ARR6) \
  601. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  602. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  603. ((__const char *) (src))[4], '\0' } }), \
  604. __extension__ ((__STRING2_COPY_ARR7) \
  605. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  606. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  607. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  608. '\0' } }), \
  609. __extension__ ((__STRING2_COPY_ARR8) \
  610. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  611. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  612. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  613. ((__const char *) (src))[6], '\0' } })
  614. __STRING_INLINE char *__stpcpy_small (char *, __STRING2_COPY_ARR2,
  615. __STRING2_COPY_ARR3,
  616. __STRING2_COPY_ARR4,
  617. __STRING2_COPY_ARR5,
  618. __STRING2_COPY_ARR6,
  619. __STRING2_COPY_ARR7,
  620. __STRING2_COPY_ARR8, size_t);
  621. __STRING_INLINE char *
  622. __stpcpy_small (char *__dest,
  623. __STRING2_COPY_ARR2 __src2, __STRING2_COPY_ARR3 __src3,
  624. __STRING2_COPY_ARR4 __src4, __STRING2_COPY_ARR5 __src5,
  625. __STRING2_COPY_ARR6 __src6, __STRING2_COPY_ARR7 __src7,
  626. __STRING2_COPY_ARR8 __src8, size_t __srclen)
  627. {
  628. union {
  629. char __c;
  630. __STRING2_COPY_ARR2 __sca2;
  631. __STRING2_COPY_ARR3 __sca3;
  632. __STRING2_COPY_ARR4 __sca4;
  633. __STRING2_COPY_ARR5 __sca5;
  634. __STRING2_COPY_ARR6 __sca6;
  635. __STRING2_COPY_ARR7 __sca7;
  636. __STRING2_COPY_ARR8 __sca8;
  637. } *__u = (void *) __dest;
  638. switch ((unsigned int) __srclen)
  639. {
  640. case 1:
  641. __u->__c = '\0';
  642. break;
  643. case 2:
  644. __extension__ __u->__sca2 = __src2;
  645. break;
  646. case 3:
  647. __extension__ __u->__sca3 = __src3;
  648. break;
  649. case 4:
  650. __extension__ __u->__sca4 = __src4;
  651. break;
  652. case 5:
  653. __extension__ __u->__sca5 = __src5;
  654. break;
  655. case 6:
  656. __extension__ __u->__sca6 = __src6;
  657. break;
  658. case 7:
  659. __extension__ __u->__sca7 = __src7;
  660. break;
  661. case 8:
  662. __extension__ __u->__sca8 = __src8;
  663. break;
  664. }
  665. return __dest + __srclen - 1;
  666. }
  667. # endif
  668. # endif
  669. #endif
  670. /* Copy no more than N characters of SRC to DEST. */
  671. #ifndef _HAVE_STRING_ARCH_strncpy
  672. # if defined _HAVE_STRING_ARCH_memset && defined _HAVE_STRING_ARCH_mempcpy
  673. # define strncpy(dest, src, n) \
  674. (__extension__ ({ char *__dest = (dest); \
  675. __builtin_constant_p (src) && __builtin_constant_p (n) \
  676. ? (strlen (src) + 1 >= ((size_t) (n)) \
  677. ? (char *) memcpy (__dest, src, n) \
  678. : (memset (__mempcpy (__dest, src, strlen (src)), \
  679. '\0', n - strlen (src)), \
  680. __dest)) \
  681. : strncpy (__dest, src, n); }))
  682. # else
  683. # define strncpy(dest, src, n) \
  684. (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
  685. ? (strlen (src) + 1 >= ((size_t) (n)) \
  686. ? (char *) memcpy (dest, src, n) \
  687. : strncpy (dest, src, n)) \
  688. : strncpy (dest, src, n)))
  689. # endif
  690. #endif
  691. /* Append no more than N characters from SRC onto DEST. */
  692. #ifndef _HAVE_STRING_ARCH_strncat
  693. # ifdef _HAVE_STRING_ARCH_strchr
  694. # define strncat(dest, src, n) \
  695. (__extension__ ({ char *__dest = (dest); \
  696. __builtin_constant_p (src) && __builtin_constant_p (n) \
  697. ? (strlen (src) < ((size_t) (n)) \
  698. ? strcat (__dest, src) \
  699. : (memcpy (strchr (__dest, '\0'), src, n), __dest)) \
  700. : strncat (dest, src, n); }))
  701. # else
  702. # define strncat(dest, src, n) \
  703. (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
  704. ? (strlen (src) < ((size_t) (n)) \
  705. ? strcat (dest, src) \
  706. : strncat (dest, src, n)) \
  707. : strncat (dest, src, n)))
  708. # endif
  709. #endif
  710. /* Compare characters of S1 and S2. */
  711. #ifndef _HAVE_STRING_ARCH_strcmp
  712. # define strcmp(s1, s2) \
  713. __extension__ \
  714. ({ size_t __s1_len, __s2_len; \
  715. (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
  716. && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
  717. (!__string2_1bptr_p (s1) || __s1_len >= 4) \
  718. && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
  719. ? memcmp ((__const char *) (s1), (__const char *) (s2), \
  720. (__s1_len < __s2_len ? __s1_len : __s2_len) + 1) \
  721. : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
  722. && (__s1_len = strlen (s1), __s1_len < 4) \
  723. ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
  724. ? __strcmp_cc (s1, s2, __s1_len) \
  725. : __strcmp_cg (s1, s2, __s1_len)) \
  726. : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
  727. && (__s2_len = strlen (s2), __s2_len < 4) \
  728. ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
  729. ? __strcmp_cc (s1, s2, __s2_len) \
  730. : __strcmp_gc (s1, s2, __s2_len)) \
  731. : strcmp (s1, s2)))); })
  732. # define __strcmp_cc(s1, s2, l) \
  733. (__extension__ ({ register int __result = \
  734. (((__const unsigned char *) (__const char *) (s1))[0] \
  735. - ((__const unsigned char *) (__const char *)(s2))[0]);\
  736. if (l > 0 && __result == 0) \
  737. { \
  738. __result = (((__const unsigned char *) \
  739. (__const char *) (s1))[1] \
  740. - ((__const unsigned char *) \
  741. (__const char *) (s2))[1]); \
  742. if (l > 1 && __result == 0) \
  743. { \
  744. __result = \
  745. (((__const unsigned char *) \
  746. (__const char *) (s1))[2] \
  747. - ((__const unsigned char *) \
  748. (__const char *) (s2))[2]); \
  749. if (l > 2 && __result == 0) \
  750. __result = \
  751. (((__const unsigned char *) \
  752. (__const char *) (s1))[3] \
  753. - ((__const unsigned char *) \
  754. (__const char *) (s2))[3]); \
  755. } \
  756. } \
  757. __result; }))
  758. # define __strcmp_cg(s1, s2, l1) \
  759. (__extension__ ({ __const unsigned char *__s2 = \
  760. (__const unsigned char *) (__const char *) (s2); \
  761. register int __result = \
  762. (((__const unsigned char *) (__const char *) (s1))[0] \
  763. - __s2[0]); \
  764. if (l1 > 0 && __result == 0) \
  765. { \
  766. __result = (((__const unsigned char *) \
  767. (__const char *) (s1))[1] - __s2[1]); \
  768. if (l1 > 1 && __result == 0) \
  769. { \
  770. __result = (((__const unsigned char *) \
  771. (__const char *) (s1))[2] - __s2[2]);\
  772. if (l1 > 2 && __result == 0) \
  773. __result = (((__const unsigned char *) \
  774. (__const char *) (s1))[3] \
  775. - __s2[3]); \
  776. } \
  777. } \
  778. __result; }))
  779. # define __strcmp_gc(s1, s2, l2) \
  780. (__extension__ ({ __const unsigned char *__s1 = \
  781. (__const unsigned char *) (__const char *) (s1); \
  782. register int __result = \
  783. __s1[0] - ((__const unsigned char *) \
  784. (__const char *) (s2))[0]; \
  785. if (l2 > 0 && __result == 0) \
  786. { \
  787. __result = (__s1[1] \
  788. - ((__const unsigned char *) \
  789. (__const char *) (s2))[1]); \
  790. if (l2 > 1 && __result == 0) \
  791. { \
  792. __result = \
  793. (__s1[2] - ((__const unsigned char *) \
  794. (__const char *) (s2))[2]); \
  795. if (l2 > 2 && __result == 0) \
  796. __result = \
  797. (__s1[3] \
  798. - ((__const unsigned char *) \
  799. (__const char *) (s2))[3]); \
  800. } \
  801. } \
  802. __result; }))
  803. #endif
  804. /* Compare N characters of S1 and S2. */
  805. #ifndef _HAVE_STRING_ARCH_strncmp
  806. # define strncmp(s1, s2, n) \
  807. (__extension__ (__builtin_constant_p (n) \
  808. && ((__builtin_constant_p (s1) \
  809. && strlen (s1) < ((size_t) (n))) \
  810. || (__builtin_constant_p (s2) \
  811. && strlen (s2) < ((size_t) (n)))) \
  812. ? strcmp (s1, s2) : strncmp (s1, s2, n)))
  813. #endif
  814. /* Return the length of the initial segment of S which
  815. consists entirely of characters not in REJECT. */
  816. #ifndef _HAVE_STRING_ARCH_strcspn
  817. # define strcspn(s, reject) \
  818. __extension__ \
  819. ({ char __r0, __r1, __r2; \
  820. (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
  821. ? ((__r0 = ((__const char *) (reject))[0], __r0 == '\0') \
  822. ? strlen (s) \
  823. : ((__r1 = ((__const char *) (reject))[1], __r1 == '\0') \
  824. ? __strcspn_c1 (s, __r0) \
  825. : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
  826. ? __strcspn_c2 (s, __r0, __r1) \
  827. : (((__const char *) (reject))[3] == '\0' \
  828. ? __strcspn_c3 (s, __r0, __r1, __r2) \
  829. : strcspn (s, reject))))) \
  830. : strcspn (s, reject)); })
  831. __STRING_INLINE size_t __strcspn_c1 (__const char *__s, char __reject);
  832. __STRING_INLINE size_t
  833. __strcspn_c1 (__const char *__s, char __reject)
  834. {
  835. register size_t __result = 0;
  836. while (__s[__result] != '\0' && __s[__result] != __reject)
  837. ++__result;
  838. return __result;
  839. }
  840. __STRING_INLINE size_t __strcspn_c2 (__const char *__s, char __reject1,
  841. char __reject2);
  842. __STRING_INLINE size_t
  843. __strcspn_c2 (__const char *__s, char __reject1, char __reject2)
  844. {
  845. register size_t __result = 0;
  846. while (__s[__result] != '\0' && __s[__result] != __reject1
  847. && __s[__result] != __reject2)
  848. ++__result;
  849. return __result;
  850. }
  851. __STRING_INLINE size_t __strcspn_c3 (__const char *__s, char __reject1,
  852. char __reject2, char __reject3);
  853. __STRING_INLINE size_t
  854. __strcspn_c3 (__const char *__s, char __reject1, char __reject2,
  855. char __reject3)
  856. {
  857. register size_t __result = 0;
  858. while (__s[__result] != '\0' && __s[__result] != __reject1
  859. && __s[__result] != __reject2 && __s[__result] != __reject3)
  860. ++__result;
  861. return __result;
  862. }
  863. #endif
  864. /* Return the length of the initial segment of S which
  865. consists entirely of characters in ACCEPT. */
  866. #ifndef _HAVE_STRING_ARCH_strspn
  867. # define strspn(s, accept) \
  868. __extension__ \
  869. ({ char __a0, __a1, __a2; \
  870. (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
  871. ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
  872. ? 0 \
  873. : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
  874. ? __strspn_c1 (s, __a0) \
  875. : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
  876. ? __strspn_c2 (s, __a0, __a1) \
  877. : (((__const char *) (accept))[3] == '\0' \
  878. ? __strspn_c3 (s, __a0, __a1, __a2) \
  879. : strspn (s, accept))))) \
  880. : strspn (s, accept)); })
  881. __STRING_INLINE size_t __strspn_c1 (__const char *__s, char __accept);
  882. __STRING_INLINE size_t
  883. __strspn_c1 (__const char *__s, char __accept)
  884. {
  885. register size_t __result = 0;
  886. /* Please note that __accept never can be '\0'. */
  887. while (__s[__result] == __accept)
  888. ++__result;
  889. return __result;
  890. }
  891. __STRING_INLINE size_t __strspn_c2 (__const char *__s, char __accept1,
  892. char __accept2);
  893. __STRING_INLINE size_t
  894. __strspn_c2 (__const char *__s, char __accept1, char __accept2)
  895. {
  896. register size_t __result = 0;
  897. /* Please note that __accept1 and __accept2 never can be '\0'. */
  898. while (__s[__result] == __accept1 || __s[__result] == __accept2)
  899. ++__result;
  900. return __result;
  901. }
  902. __STRING_INLINE size_t __strspn_c3 (__const char *__s, char __accept1,
  903. char __accept2, char __accept3);
  904. __STRING_INLINE size_t
  905. __strspn_c3 (__const char *__s, char __accept1, char __accept2, char __accept3)
  906. {
  907. register size_t __result = 0;
  908. /* Please note that __accept1 to __accept3 never can be '\0'. */
  909. while (__s[__result] == __accept1 || __s[__result] == __accept2
  910. || __s[__result] == __accept3)
  911. ++__result;
  912. return __result;
  913. }
  914. #endif
  915. /* Find the first occurrence in S of any character in ACCEPT. */
  916. #ifndef _HAVE_STRING_ARCH_strpbrk
  917. # define strpbrk(s, accept) \
  918. __extension__ \
  919. ({ char __a0, __a1, __a2; \
  920. (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
  921. ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
  922. ? NULL \
  923. : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
  924. ? strchr (s, __a0) \
  925. : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
  926. ? __strpbrk_c2 (s, __a0, __a1) \
  927. : (((__const char *) (accept))[3] == '\0' \
  928. ? __strpbrk_c3 (s, __a0, __a1, __a2) \
  929. : strpbrk (s, accept))))) \
  930. : strpbrk (s, accept)); })
  931. __STRING_INLINE char *__strpbrk_c2 (__const char *__s, char __accept1,
  932. char __accept2);
  933. __STRING_INLINE char *
  934. __strpbrk_c2 (__const char *__s, char __accept1, char __accept2)
  935. {
  936. /* Please note that __accept1 and __accept2 never can be '\0'. */
  937. while (*__s != '\0' && *__s != __accept1 && *__s != __accept2)
  938. ++__s;
  939. return *__s == '\0' ? NULL : (char *) (size_t) __s;
  940. }
  941. __STRING_INLINE char *__strpbrk_c3 (__const char *__s, char __accept1,
  942. char __accept2, char __accept3);
  943. __STRING_INLINE char *
  944. __strpbrk_c3 (__const char *__s, char __accept1, char __accept2,
  945. char __accept3)
  946. {
  947. /* Please note that __accept1 to __accept3 never can be '\0'. */
  948. while (*__s != '\0' && *__s != __accept1 && *__s != __accept2
  949. && *__s != __accept3)
  950. ++__s;
  951. return *__s == '\0' ? NULL : (char *) (size_t) __s;
  952. }
  953. #endif
  954. /* Find the first occurrence of NEEDLE in HAYSTACK. */
  955. #ifndef _HAVE_STRING_ARCH_strstr
  956. # define strstr(haystack, needle) \
  957. (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
  958. ? (((__const char *) (needle))[0] == '\0' \
  959. ? (char *) (size_t) (haystack) \
  960. : (((__const char *) (needle))[1] == '\0' \
  961. ? strchr (haystack, \
  962. ((__const char *) (needle))[0]) \
  963. : strstr (haystack, needle))) \
  964. : strstr (haystack, needle)))
  965. #endif
  966. #if defined __USE_GNU && !defined _FORCE_INLINES
  967. # ifndef _HAVE_STRING_ARCH_strnlen
  968. __STRING_INLINE size_t
  969. strnlen (__const char *__string, size_t __maxlen)
  970. {
  971. __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
  972. return __end ? (size_t) (__end - __string) : __maxlen;
  973. }
  974. # endif
  975. #endif
  976. #ifndef _HAVE_STRING_ARCH_strtok_r
  977. # define __strtok_r(s, sep, nextp) \
  978. (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \
  979. ? (((__const char *) (sep))[0] != '\0' \
  980. && ((__const char *) (sep))[1] == '\0' \
  981. ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp) \
  982. : __strtok_r (s, sep, nextp)) \
  983. : __strtok_r (s, sep, nextp)))
  984. __STRING_INLINE char *__strtok_r_1c (char *__s, char __sep, char **__nextp);
  985. __STRING_INLINE char *
  986. __strtok_r_1c (char *__s, char __sep, char **__nextp)
  987. {
  988. char *__result;
  989. if (__s == NULL)
  990. __s = *__nextp;
  991. while (*__s == __sep)
  992. ++__s;
  993. if (*__s == '\0')
  994. __result = NULL;
  995. else
  996. {
  997. __result = __s;
  998. while (*__s != '\0' && *__s != __sep)
  999. ++__s;
  1000. if (*__s == '\0')
  1001. *__nextp = __s;
  1002. else
  1003. {
  1004. *__s = '\0';
  1005. *__nextp = __s + 1;
  1006. }
  1007. }
  1008. return __result;
  1009. }
  1010. # if defined __USE_POSIX || defined __USE_MISC
  1011. # define strtok_r(s, sep, nextp) __strtok_r ((s), (sep), (nextp))
  1012. # endif
  1013. #endif
  1014. #ifndef _HAVE_STRING_ARCH_strsep
  1015. # define __strsep(s, reject) \
  1016. __extension__ \
  1017. ({ char __r0, __r1, __r2; \
  1018. (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
  1019. && (__r0 = ((__const char *) (reject))[0], \
  1020. ((__const char *) (reject))[0] != '\0') \
  1021. ? ((__r1 = ((__const char *) (reject))[1], \
  1022. ((__const char *) (reject))[1] == '\0') \
  1023. ? __strsep_1c (s, __r0) \
  1024. : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
  1025. ? __strsep_2c (s, __r0, __r1) \
  1026. : (((__const char *) (reject))[3] == '\0' \
  1027. ? __strsep_3c (s, __r0, __r1, __r2) \
  1028. : __strsep_g (s, reject)))) \
  1029. : __strsep_g (s, reject)); })
  1030. __STRING_INLINE char *__strsep_1c (char **__s, char __reject);
  1031. __STRING_INLINE char *
  1032. __strsep_1c (char **__s, char __reject)
  1033. {
  1034. register char *__retval = *__s;
  1035. if (__retval == NULL)
  1036. return *__s = NULL;
  1037. if (*__retval == __reject)
  1038. *(*__s)++ = '\0';
  1039. else
  1040. if ((*__s = strchr (__retval, __reject)) != NULL)
  1041. *(*__s)++ = '\0';
  1042. else
  1043. *__s = NULL;
  1044. return __retval;
  1045. }
  1046. __STRING_INLINE char *__strsep_2c (char **__s, char __reject1, char __reject2);
  1047. __STRING_INLINE char *
  1048. __strsep_2c (char **__s, char __reject1, char __reject2)
  1049. {
  1050. register char *__retval = *__s;
  1051. if (__retval == NULL)
  1052. return *__s = NULL;
  1053. if (*__retval == __reject1 || *__retval == __reject2)
  1054. *(*__s)++ = '\0';
  1055. else
  1056. {
  1057. register char *__cp = __retval;
  1058. while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2)
  1059. ++__cp;
  1060. if (*__cp != '\0')
  1061. {
  1062. *__s = __cp;
  1063. *(*__s)++ = '\0';
  1064. }
  1065. else
  1066. *__s = NULL;
  1067. }
  1068. return __retval;
  1069. }
  1070. __STRING_INLINE char *__strsep_3c (char **__s, char __reject1, char __reject2,
  1071. char __reject3);
  1072. __STRING_INLINE char *
  1073. __strsep_3c (char **__s, char __reject1, char __reject2, char __reject3)
  1074. {
  1075. register char *__retval = *__s;
  1076. if (__retval == NULL)
  1077. return *__s = NULL;
  1078. if (*__retval == __reject1 || *__retval == __reject2
  1079. || *__retval == __reject3)
  1080. *(*__s)++ = '\0';
  1081. else
  1082. {
  1083. register char *__cp = __retval;
  1084. while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2
  1085. && *__cp != __reject3)
  1086. ++__cp;
  1087. if (*__cp != '\0')
  1088. {
  1089. *__s = __cp;
  1090. *(*__s)++ = '\0';
  1091. }
  1092. else
  1093. *__s = NULL;
  1094. }
  1095. return __retval;
  1096. }
  1097. __STRING_INLINE char *__strsep_g (char **__s, __const char *__reject);
  1098. __STRING_INLINE char *
  1099. __strsep_g (char **__s, __const char *__reject)
  1100. {
  1101. register char *__retval = *__s;
  1102. if (__retval == NULL)
  1103. return NULL;
  1104. if ((*__s = strpbrk (__retval, __reject)) != NULL)
  1105. *(*__s)++ = '\0';
  1106. return __retval;
  1107. }
  1108. # ifdef __USE_BSD
  1109. # define strsep(s, reject) __strsep ((s), (reject))
  1110. # endif
  1111. #endif
  1112. /* We need the memory allocation functions for inline strdup().
  1113. Referring to stdlib.h (even minimally) is not allowed
  1114. in any of the tight standards compliant modes. */
  1115. #ifdef __USE_MISC
  1116. # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
  1117. # define __need_malloc_and_calloc
  1118. # include <stdlib.h>
  1119. # endif
  1120. # ifndef _HAVE_STRING_ARCH_strdup
  1121. # define __strdup(s) \
  1122. (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
  1123. ? (((__const char *) (s))[0] == '\0' \
  1124. ? (char *) calloc (1, 1) \
  1125. : ({ size_t __len = strlen (s) + 1; \
  1126. char *__retval = (char *) malloc (__len); \
  1127. if (__retval != NULL) \
  1128. __retval = (char *) memcpy (__retval, s, __len); \
  1129. __retval; })) \
  1130. : __strdup (s)))
  1131. # if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
  1132. # define strdup(s) __strdup (s)
  1133. # endif
  1134. # endif
  1135. #endif /* Use misc. or use GNU. */
  1136. #ifndef _FORCE_INLINES
  1137. # undef __STRING_INLINE
  1138. #endif
  1139. #endif /* No string inlines. */