diff options
author | Enyew Tan <enyew@tegile.com> | 2014-08-23 17:55:22 -0700 |
---|---|---|
committer | Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org> | 2022-05-23 10:02:28 +0200 |
commit | 29219719c034367724cbf77434175b3c4e681e43 (patch) | |
tree | 76ce2e79fd09f06e3f590f9f7bff203894caa77a | |
parent | 5b0d53307d70a828ad7aef4dc6d8a3ad7d5c231b (diff) | |
download | illumos-joyent-29219719c034367724cbf77434175b3c4e681e43.tar.gz |
14687 need in-kernel strtok_r()
Reviewed by: Robert Mustacchi <rm+illumos@fingolfin.org>
Reviewed by: Jason King <jason.brian.king@gmail.com>
Reviewed by: Yuri Pankov <ypankov@tintri.com>
Reviewed by: Andy Fiddaman <andy@omnios.org>
Approved by: Dan McDonald <danmcd@mnx.io>
-rw-r--r-- | usr/src/common/util/string.c | 36 | ||||
-rw-r--r-- | usr/src/man/man9f/string.9f | 32 | ||||
-rw-r--r-- | usr/src/uts/common/io/scsi/targets/sd.c | 55 | ||||
-rw-r--r-- | usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c | 43 | ||||
-rw-r--r-- | usr/src/uts/common/sys/sunddi.h | 3 |
5 files changed, 79 insertions, 90 deletions
diff --git a/usr/src/common/util/string.c b/usr/src/common/util/string.c index 8e1247f381..c44dc4b402 100644 --- a/usr/src/common/util/string.c +++ b/usr/src/common/util/string.c @@ -26,7 +26,7 @@ /* * Copyright (c) 2016 by Delphix. All rights reserved. - * Copyright 2018 Nexenta Systems, Inc. + * Copyright 2022 Tintri by DDN, Inc. All rights reserved. */ /* @@ -709,6 +709,40 @@ strsep(char **stringp, const char *delim) } /* + * strtok_r + * + * uses strpbrk and strspn to break string into tokens on + * sequentially subsequent calls. returns NULL when no + * non-separator characters remain. + * `subsequent' calls are calls with first argument NULL. + */ +char * +strtok_r(char *string, const char *sepset, char **lasts) +{ + char *q, *r; + + /* first or subsequent call */ + if (string == NULL) + string = *lasts; + + if (string == NULL) /* return if no tokens remaining */ + return (NULL); + + q = string + strspn(string, sepset); /* skip leading separators */ + + if (*q == '\0') /* return if no tokens remaining */ + return (NULL); + + if ((r = strpbrk(q, sepset)) == NULL) { /* move past token */ + *lasts = NULL; /* indicate this is last token */ + } else { + *r = '\0'; + *lasts = r + 1; + } + return (q); +} + +/* * Unless mentioned otherwise, all of the routines below should be added to * the Solaris DDI as necessary. For now, only provide them to standalone. */ diff --git a/usr/src/man/man9f/string.9f b/usr/src/man/man9f/string.9f index 6ed0687b0a..221355137d 100644 --- a/usr/src/man/man9f/string.9f +++ b/usr/src/man/man9f/string.9f @@ -1,13 +1,14 @@ '\" te .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved. +.\" Copyright 2022 Tintri by DDN, Inc. All rights reserved. .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] -.TH STRING 9F "Mar 14, 2016" +.TH STRING 9F "May 20, 2022" .SH NAME string, strcasecmp, strncasecmp, strcat, strncat, strlcat, strchr, strrchr, strcmp, strncmp, strcpy, strncpy, strlcpy, strfree, strspn, -strdup, ddi_strdup, strlen, strnlen \- string operations +strdup, ddi_strdup, strlen, strnlen, strtok_r \- string operations .SH SYNOPSIS .nf #include <sys/ddi.h> @@ -101,13 +102,19 @@ strdup, ddi_strdup, strlen, strnlen \- string operations \fBsize_t\fR \fBstrnlen\fR(\fBconst char *\fR\fIs\fR, \fBsize_t\fR \fIn\fR); .fi +.LP +.nf +\fBchar *\fR\fBstrtok_r\fR(\fBchar *\fR\fIs1\fR, \fBconst char *\fR\fIs2\fR, \fBchar **\fR\fIlasts\fR); +.fi + .SH INTERFACE LEVEL illumos DDI specific (illumos DDI). .SH DESCRIPTION The arguments \fIs\fR, \fIs1\fR, and \fIs2\fR point to strings (arrays of characters terminated by a null character). The \fBstrcat()\fR, \fBstrncat()\fR, \fBstrlcat()\fR, \fBstrcpy()\fR, \fBstrncpy()\fR, -\fBstrlcpy()\fR, and \fBstrfree()\fR functions all alter their first argument. +\fBstrlcpy()\fR, \fBstrfree()\fR, and \fBstrtok_r()\R functions all alter their +first argument. Additionally, the \fBstrcpy()\fR function does not check for overflow of the array. .SS "\fBstrcasecmp()\fR, \fBstrncasecmp()\fR" @@ -218,6 +225,25 @@ The \fBstrnlen()\fR function returns the smaller of \fIn\fR or the number of bytes in \fIs\fR, not including the terminating null character. The \fBstrnlen()\fR function never examines more than \fIn\fR bytes of the string pointed to by \fIs\fR. +.SS "\fBstrtok_r()\fR" +The \fBstrtok_r()\fR function considers the null-terminated string \fIs1\fR as +a sequence of zero or more text tokens separated by spans of one or more +characters from the separator string \fIs2\fR. The argument \fIlasts\fR points +to a user-provided pointer which points to stored information necessary for +\fBstrtok_r()\fR to continue scanning the same string. +.LP +In the first call to \fBstrtok_r()\fR, \fIs1\fR points to a null-terminated +string, \fIs2\fR to a null-terminated string of separator characters, and the +value pointed to by \fIlasts\fR is ignored. The \fBstrtok_r()\fR function +returns a pointer to the first character of the first token, writes a null +character into \fIs1\fR immediately following the returned token, and updates +the pointer to which \fIlasts\fR points. +.LP +In subsequent calls, \fIs1\fR is a null pointer and \fIlasts\fR is unchanged +from the previous call so that subsequent calls move through the string +\fIs1\fR, returning successive tokens until no tokens remain. The separator +string \fIs2\fR can be different from call to call. When no token remains in +\fIs1\fR, a null pointer is returned. .SH CONTEXT The \fBstrdup()\fR and \fBddi_strdup()\fR functions can be called from user or kernel context. diff --git a/usr/src/uts/common/io/scsi/targets/sd.c b/usr/src/uts/common/io/scsi/targets/sd.c index 982e360f48..a144318420 100644 --- a/usr/src/uts/common/io/scsi/targets/sd.c +++ b/usr/src/uts/common/io/scsi/targets/sd.c @@ -27,9 +27,9 @@ * Copyright (c) 2012, 2016 by Delphix. All rights reserved. * Copyright 2012 DEY Storage Systems, Inc. All rights reserved. * Copyright 2019 Joyent, Inc. - * Copyright 2017 Nexenta Systems, Inc. * Copyright 2019 Racktop Systems * Copyright 2022 OmniOS Community Edition (OmniOSce) Association. + * Copyright 2022 Tintri by DDN, Inc. All rights reserved. */ /* * Copyright 2011 cyril.galibern@opensvc.com @@ -1054,7 +1054,6 @@ static int sd_pm_idletime = 1; #define sd_get_media_info_ext ssd_get_media_info_ext #define sd_dkio_ctrl_info ssd_dkio_ctrl_info #define sd_nvpair_str_decode ssd_nvpair_str_decode -#define sd_strtok_r ssd_strtok_r #define sd_set_properties ssd_set_properties #define sd_get_tunables_from_conf ssd_get_tunables_from_conf #define sd_setup_next_xfer ssd_setup_next_xfer @@ -1234,7 +1233,6 @@ static void sd_set_mmc_caps(sd_ssc_t *ssc); static void sd_read_unit_properties(struct sd_lun *un); static int sd_process_sdconf_file(struct sd_lun *un); static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str); -static char *sd_strtok_r(char *string, const char *sepset, char **lasts); static void sd_set_properties(struct sd_lun *un, char *name, char *value); static void sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list, sd_tunables *values); @@ -3885,9 +3883,9 @@ sd_process_sdconf_file(struct sd_lun *un) * data-property-name-list * setting the properties for each. */ - for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t", + for (dataname_ptr = strtok_r(dnlist_ptr, " \t", &dataname_lasts); dataname_ptr != NULL; - dataname_ptr = sd_strtok_r(NULL, " \t", + dataname_ptr = strtok_r(NULL, " \t", &dataname_lasts)) { int version; @@ -3961,12 +3959,12 @@ sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) char *nv, *name, *value, *token; char *nv_lasts, *v_lasts, *x_lasts; - for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; - nv = sd_strtok_r(NULL, ",", &nv_lasts)) { - token = sd_strtok_r(nv, ":", &v_lasts); - name = sd_strtok_r(token, " \t", &x_lasts); - token = sd_strtok_r(NULL, ":", &v_lasts); - value = sd_strtok_r(token, " \t", &x_lasts); + for (nv = strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL; + nv = strtok_r(NULL, ",", &nv_lasts)) { + token = strtok_r(nv, ":", &v_lasts); + name = strtok_r(token, " \t", &x_lasts); + token = strtok_r(NULL, ":", &v_lasts); + value = strtok_r(token, " \t", &x_lasts); if (name == NULL || value == NULL) { SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_nvpair_str_decode: " @@ -3978,41 +3976,6 @@ sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str) } /* - * Function: sd_strtok_r() - * - * Description: This function uses strpbrk and strspn to break - * string into tokens on sequentially subsequent calls. Return - * NULL when no non-separator characters remain. The first - * argument is NULL for subsequent calls. - */ -static char * -sd_strtok_r(char *string, const char *sepset, char **lasts) -{ - char *q, *r; - - /* First or subsequent call */ - if (string == NULL) - string = *lasts; - - if (string == NULL) - return (NULL); - - /* Skip leading separators */ - q = string + strspn(string, sepset); - - if (*q == '\0') - return (NULL); - - if ((r = strpbrk(q, sepset)) == NULL) { - *lasts = NULL; - } else { - *r = '\0'; - *lasts = r + 1; - } - return (q); -} - -/* * Function: sd_set_properties() * * Description: Set device properties based on the improved diff --git a/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c b/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c index e70ae4376e..04b558de39 100644 --- a/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c +++ b/usr/src/uts/common/io/usb/scsa2usb/scsa2usb.c @@ -25,6 +25,7 @@ * Copyright 2016 Joyent, Inc. * Copyright (c) 2016 by Delphix. All rights reserved. * Copyright 2019 Joshua M. Clulow <josh@sysmgr.org> + * Copyright 2022 Tintri by DDN, Inc. All rights reserved. */ @@ -92,7 +93,6 @@ static void scsa2usb_override(scsa2usb_state_t *); static int scsa2usb_parse_input_str(char *, scsa2usb_ov_t *, scsa2usb_state_t *); static void scsa2usb_override_error(char *, scsa2usb_state_t *); -static char *scsa2usb_strtok_r(char *, char *, char **); /* PANIC callback handling */ @@ -1576,11 +1576,11 @@ scsa2usb_parse_input_str(char *str, scsa2usb_ov_t *ovp, u_longlong_t value; /* parse all the input pairs in the string */ - for (input_field = scsa2usb_strtok_r(str, "=", &lasts); + for (input_field = strtok_r(str, "=", &lasts); input_field != NULL; - input_field = scsa2usb_strtok_r(lasts, "=", &lasts)) { + input_field = strtok_r(lasts, "=", &lasts)) { - if ((input_value = scsa2usb_strtok_r(lasts, " ", &lasts)) == + if ((input_value = strtok_r(lasts, " ", &lasts)) == NULL) { scsa2usb_override_error("format", scsa2usbp); @@ -1702,41 +1702,6 @@ scsa2usb_override_error(char *input_field, scsa2usb_state_t *scsa2usbp) "invalid %s in scsa2usb.conf file entry", input_field); } -/* - * scsa2usb_strtok_r: - * parse a list of tokens - */ -static char * -scsa2usb_strtok_r(char *p, char *sep, char **lasts) -{ - char *e; - char *tok = NULL; - - if (p == 0 || *p == 0) { - - return (NULL); - } - - e = p+strlen(p); - - do { - if (strchr(sep, *p) != NULL) { - if (tok != NULL) { - *p = 0; - *lasts = p+1; - - return (tok); - } - } else if (tok == NULL) { - tok = p; - } - } while (++p < e); - - *lasts = NULL; - - return (tok); -} - /* * Some devices are not complete or compatible implementations of the USB mass diff --git a/usr/src/uts/common/sys/sunddi.h b/usr/src/uts/common/sys/sunddi.h index e7f55d00c0..442595289f 100644 --- a/usr/src/uts/common/sys/sunddi.h +++ b/usr/src/uts/common/sys/sunddi.h @@ -23,8 +23,8 @@ * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved. * Copyright (c) 2012 by Delphix. All rights reserved. - * Copyright 2016 Nexenta Systems, Inc. All rights reserved. * Copyright 2019 Joyent, Inc. + * Copyright 2022 Tintri by DDN, Inc. All rights reserved. */ #ifndef _SYS_SUNDDI_H @@ -479,6 +479,7 @@ extern size_t strlcpy(char *, const char *, size_t); extern size_t strspn(const char *, const char *); extern size_t strcspn(const char *, const char *); extern char *strsep(char **, const char *); +extern char *strtok_r(char *, const char *, char **); extern int bcmp(const void *, const void *, size_t) __PURE; extern int stoi(char **); extern void numtos(ulong_t, char *); |