Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 12b29f3 in mainline


Ignore:
Timestamp:
2014-08-29T07:50:09Z (7 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master
Children:
293703e
Parents:
69c664e
Message:

Reintroduce strtok to libposix (needed by GCC & Python)

Implementation taken from the original libc implementation before
revision 2122 (where it was changed to str_tok()).

Location:
uspace/lib/posix
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/include/posix/string.h

    r69c664e r12b29f3  
    6464 * forward declarations ought to be enough.
    6565 */
    66 /* From str.h. */
    67 extern char * strtok_r(char *, const char *, char **);
    68 extern char * strtok(char *, const char *);
    6966
    7067/* From mem.h */
     
    10198extern char *__POSIX_DEF__(strstr)(const char *haystack, const char *needle);
    10299
     100/* Tokenization functions. */
     101extern char *__POSIX_DEF__(strtok_r)(char *, const char *, char **);
     102extern char *__POSIX_DEF__(strtok)(char *, const char *);
     103
     104
    103105/* Collation Functions */
    104106extern int __POSIX_DEF__(strcoll)(const char *s1, const char *s2);
  • uspace/lib/posix/source/string.c

    r69c664e r12b29f3  
    508508       
    509509        return NULL;
     510}
     511
     512/** Split string by delimiters.
     513 *
     514 * @param s             String to be tokenized. May not be NULL.
     515 * @param delim         String with the delimiters.
     516 * @return              Pointer to the prefix of @a s before the first
     517 *                      delimiter character. NULL if no such prefix
     518 *                      exists.
     519 */
     520char *posix_strtok(char *s, const char *delim)
     521{
     522        static char *next;
     523
     524        return posix_strtok_r(s, delim, &next);
     525}
     526
     527
     528/** Split string by delimiters.
     529 *
     530 * @param s             String to be tokenized. May not be NULL.
     531 * @param delim         String with the delimiters.
     532 * @param next          Variable which will receive the pointer to the
     533 *                      continuation of the string following the first
     534 *                      occurrence of any of the delimiter characters.
     535 *                      May be NULL.
     536 * @return              Pointer to the prefix of @a s before the first
     537 *                      delimiter character. NULL if no such prefix
     538 *                      exists.
     539 */
     540char *posix_strtok_r(char *s, const char *delim, char **next)
     541{
     542        char *start, *end;
     543
     544        if (s == NULL)
     545                s = *next;
     546
     547        /* Skip over leading delimiters. */
     548        while (*s && (posix_strchr(delim, *s) != NULL)) ++s;
     549        start = s;
     550
     551        /* Skip over token characters. */
     552        while (*s && (posix_strchr(delim, *s) == NULL)) ++s;
     553        end = s;
     554        *next = (*s ? s + 1 : s);
     555
     556        if (start == end) {
     557                return NULL;    /* No more tokens. */
     558        }
     559
     560        /* Overwrite delimiter with NULL terminator. */
     561        *end = '\0';
     562        return start;
    510563}
    511564
Note: See TracChangeset for help on using the changeset viewer.