1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
7 * \file compat_string.c
8 * \brief Useful string-processing functions that some platforms don't
12 #include "lib/string/compat_string.h"
13 #include "lib/err/torerr.h"
15 /* Inline the strl functions if the platform doesn't have them. */
17 #include "ext/strlcpy.c"
20 #include "ext/strlcat.c"
26 /** Helper for tor_strtok_r_impl: Advances cp past all characters in
27 * <b>sep</b>, and returns its new value. */
29 strtok_helper(char *cp
, const char *sep
)
32 while (*cp
&& strchr(sep
, *cp
))
35 while (*cp
&& *cp
== *sep
)
41 /** Implementation of strtok_r for platforms whose coders haven't figured out
42 * how to write one. Hey, retrograde libc developers! You can use this code
45 tor_strtok_r_impl(char *str
, const char *sep
, char **lasts
)
50 str
= strtok_helper(str
, sep
);
53 start
= cp
= *lasts
= str
;
54 } else if (!*lasts
|| !**lasts
) {
61 while (*cp
&& !strchr(sep
, *cp
))
64 cp
= strchr(cp
, *sep
);
71 *lasts
= strtok_helper(cp
, sep
);