1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * String manipulation functions.
38 * libcfs/libcfs/libcfs_string.c
40 * Author: Nathan Rutman <nathan.rutman@sun.com>
44 # define EXPORT_SYMTAB
47 #include <libcfs/libcfs.h>
49 /* non-0 = don't match */
50 static int libcfs_strncasecmp(const char *s1, const char *s2, size_t n)
52 if (s1 == NULL || s2 == NULL)
58 while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
59 if (n == 0 || *s1 == '\0' || *s2 == '\0')
65 return tolower(*(unsigned char *)s1) - tolower(*(unsigned char *)s2);
68 /* Convert a text string to a bitmask */
69 int libcfs_str2mask(const char *str, const char *(*bit2str)(int bit),
70 int *oldmask, int minmask, int allmask)
73 int newmask = minmask, i, len, found = 0;
76 /* <str> must be a list of tokens separated by whitespace
77 * and optionally an operator ('+' or '-'). If an operator
78 * appears first in <str>, '*oldmask' is used as the starting point
79 * (relative), otherwise minmask is used (absolute). An operator
80 * applies to all following tokens up to the next operator. */
86 if (*str == '+' || *str == '-') {
89 /* only if first token is relative */
93 if (*str == 0) /* trailing op */
97 /* find token length */
98 for (len = 0; str[len] != 0 && !isspace(str[len]) &&
99 str[len] != '+' && str[len] != '-'; len++);
103 for (i = 0; i < 32; i++) {
104 if (libcfs_strncasecmp(str, bit2str(i), len) == 0) {
106 newmask &= ~(1 << i);
113 if (!found && (libcfs_strncasecmp(str, "ALL", len) == 0)) {
121 CWARN("unknown mask '%.*s'.\n"
122 "mask usage: [+|-]<all|type> ...\n", len, str);
132 /* Duplicate a string in a platform-independent way */
133 char *libcfs_strdup(const char *str, u_int32_t flags)
135 size_t lenz; /* length of str + zero byte */
138 lenz = strlen(str) + 1;
140 dup_str = cfs_alloc(lenz, flags);
144 memcpy(dup_str, str, lenz);
149 EXPORT_SYMBOL(libcfs_str2mask);
150 EXPORT_SYMBOL(libcfs_strdup);