2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/util/samba_util.h"
26 #include "system/locale.h"
27 #include "smb_strtox.h"
33 * @brief String utilities.
37 * Parse a string containing a boolean value.
39 * val will be set to the read value.
41 * @retval true if a boolean value was parsed, false otherwise.
43 _PUBLIC_
bool conv_str_bool(const char * str
, bool * val
)
48 if (str
== NULL
|| *str
== '\0') {
52 lval
= strtol(str
, &end
, 10 /* base */);
53 if (end
== NULL
|| *end
!= '\0' || end
== str
) {
54 return set_boolean(str
, val
);
57 *val
= (lval
) ? true : false;
62 * Convert a size specification like 16K into an integral number of bytes.
64 _PUBLIC_
bool conv_str_size_error(const char * str
, uint64_t * val
)
67 unsigned long long lval
;
70 if (str
== NULL
|| *str
== '\0') {
74 lval
= smb_strtoull(str
, &end
, 10, &error
, SMB_STR_STANDARD
);
80 if (strwicmp(end
, "K") == 0) {
82 } else if (strwicmp(end
, "M") == 0) {
83 lval
*= (1024ULL * 1024ULL);
84 } else if (strwicmp(end
, "G") == 0) {
85 lval
*= (1024ULL * 1024ULL * 1024ULL);
86 } else if (strwicmp(end
, "T") == 0) {
87 lval
*= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
88 } else if (strwicmp(end
, "P") == 0) {
89 lval
*= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
95 *val
= (uint64_t)lval
;
100 * Parse a uint64_t value from a string
102 * val will be set to the value read.
104 * @retval true if parsing was successful, false otherwise
106 _PUBLIC_
bool conv_str_u64(const char * str
, uint64_t * val
)
108 unsigned long long lval
;
111 if (str
== NULL
|| *str
== '\0') {
115 lval
= smb_strtoull(str
, NULL
, 10, &error
, SMB_STR_FULL_STR_CONV
);
120 *val
= (uint64_t)lval
;
127 * @note The comparison is case-insensitive.
129 _PUBLIC_
bool strequal(const char *s1
, const char *s2
)
136 return strcasecmp_m(s1
,s2
) == 0;
141 * @brief String utilities.
144 static bool next_token_internal_talloc(TALLOC_CTX
*ctx
,
163 /* default to simple separators */
168 /* find the first non sep char, if left-trimming is requested */
170 while (*s
&& strchr_m(sep
,*s
)) {
180 /* When restarting we need to go from here. */
183 /* Work out the length needed. */
184 for (quoted
= false; *s
&&
185 (quoted
|| !strchr_m(sep
,*s
)); s
++) {
193 /* We started with len = 1 so we have space for the nul. */
194 *pp_buff
= talloc_array(ctx
, char, len
);
199 /* copy over the token */
202 for (quoted
= false; *s
&&
203 (quoted
|| !strchr_m(sep
,*s
)); s
++) {
211 *ptr
= (*s
) ? s
+1 : s
;
217 bool next_token_talloc(TALLOC_CTX
*ctx
,
222 return next_token_internal_talloc(ctx
, ptr
, pp_buff
, sep
, true);
226 * Get the next token from a string, return false if none found. Handles
227 * double-quotes. This version does not trim leading separator characters
228 * before looking for a token.
231 bool next_token_no_ltrim_talloc(TALLOC_CTX
*ctx
,
236 return next_token_internal_talloc(ctx
, ptr
, pp_buff
, sep
, false);
240 * Get the next token from a string, return False if none found.
241 * Handles double-quotes.
243 * Based on a routine by GJC@VILLAGE.COM.
244 * Extensively modified by Andrew.Tridgell@anu.edu.au
246 _PUBLIC_
bool next_token(const char **ptr
,char *buff
, const char *sep
, size_t bufsize
)
257 /* default to simple separators */
261 /* find the first non sep char */
262 while (*s
&& strchr_m(sep
,*s
))
269 /* copy over the token */
270 for (quoted
= false; len
< bufsize
&& *s
&& (quoted
|| !strchr_m(sep
,*s
)); s
++) {
279 *ptr
= (*s
) ? s
+1 : s
;
286 Set a boolean variable from the text value stored in the passed string.
287 Returns true in success, false if the passed string does not correctly
291 _PUBLIC_
bool set_boolean(const char *boolean_string
, bool *boolean
)
293 if (strwicmp(boolean_string
, "yes") == 0 ||
294 strwicmp(boolean_string
, "true") == 0 ||
295 strwicmp(boolean_string
, "on") == 0 ||
296 strwicmp(boolean_string
, "1") == 0) {
299 } else if (strwicmp(boolean_string
, "no") == 0 ||
300 strwicmp(boolean_string
, "false") == 0 ||
301 strwicmp(boolean_string
, "off") == 0 ||
302 strwicmp(boolean_string
, "0") == 0) {