lib/string_helpers.c
Source file repositories/reference/linux-study-clean/lib/string_helpers.c
File Facts
- System
- Linux kernel
- Corpus path
lib/string_helpers.c- Extension
.c- Size
- 25180 bytes
- Lines
- 1048
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bug.hlinux/kernel.hlinux/math64.hlinux/export.hlinux/ctype.hlinux/device.hlinux/errno.hlinux/fs.hlinux/hex.hlinux/limits.hlinux/mm.hlinux/slab.hlinux/string.hlinux/string_helpers.hkunit/test.hkunit/test-bug.h
Detected Declarations
struct strarrayfunction Copyrightfunction parse_int_arrayfunction parse_int_array_userfunction unescape_spacefunction unescape_octalfunction unescape_hexfunction unescape_specialfunction string_unescapefunction escape_passthroughfunction escape_spacefunction escape_specialfunction escape_nullfunction escape_octalfunction escape_hexfunction flagsfunction kfree_strarrayfunction kfree_strarrayfunction devm_kfree_strarrayfunction sysfs_streqfunction match_stringfunction __sysfs_match_stringfunction memcpy_and_padfunction __read_overflow2_fieldfunction __write_overflow_fieldfunction __fortify_reportfunction __fortify_panicexport string_get_sizeexport parse_int_arrayexport parse_int_array_userexport string_unescapeexport string_escape_memexport kstrdup_quotableexport kstrdup_quotable_cmdlineexport kstrdup_quotable_fileexport kstrdup_and_replaceexport kasprintf_strarrayexport kfree_strarrayexport devm_kasprintf_strarrayexport skip_spacesexport strimexport sysfs_streqexport match_stringexport __sysfs_match_stringexport strreplaceexport memcpy_and_padexport __read_overflow2_fieldexport __write_overflow_field
Annotated Snippet
struct strarray {
char **array __counted_by_ptr(n);
size_t n;
};
static void devm_kfree_strarray(struct device *dev, void *res)
{
struct strarray *array = res;
kfree_strarray(array->array, array->n);
}
char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
{
struct strarray *ptr;
ptr = devres_alloc(devm_kfree_strarray, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return ERR_PTR(-ENOMEM);
ptr->n = n;
ptr->array = kasprintf_strarray(GFP_KERNEL, prefix, n);
if (!ptr->array) {
devres_free(ptr);
return ERR_PTR(-ENOMEM);
}
devres_add(dev, ptr);
return ptr->array;
}
EXPORT_SYMBOL_GPL(devm_kasprintf_strarray);
/**
* skip_spaces - Removes leading whitespace from @str.
* @str: The string to be stripped.
*
* Returns a pointer to the first non-whitespace character in @str.
*/
char *skip_spaces(const char *str)
{
while (isspace(*str))
++str;
return (char *)str;
}
EXPORT_SYMBOL(skip_spaces);
/**
* strim - Removes leading and trailing whitespace from @s.
* @s: The string to be stripped.
*
* Note that the first trailing whitespace is replaced with a %NUL-terminator
* in the given string @s. Returns a pointer to the first non-whitespace
* character in @s.
*/
char *strim(char *s)
{
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';
return skip_spaces(s);
}
EXPORT_SYMBOL(strim);
/**
* sysfs_streq - return true if strings are equal, modulo trailing newline
* @s1: one string
* @s2: another string
*
* This routine returns true iff two strings are equal, treating both
* NUL and newline-then-NUL as equivalent string terminations. It's
* geared for use with sysfs input strings, which generally terminate
* with newlines but are compared against values without newlines.
*/
bool sysfs_streq(const char *s1, const char *s2)
{
while (*s1 && *s1 == *s2) {
s1++;
s2++;
}
Annotation
- Immediate include surface: `linux/bug.h`, `linux/kernel.h`, `linux/math64.h`, `linux/export.h`, `linux/ctype.h`, `linux/device.h`, `linux/errno.h`, `linux/fs.h`.
- Detected declarations: `struct strarray`, `function Copyright`, `function parse_int_array`, `function parse_int_array_user`, `function unescape_space`, `function unescape_octal`, `function unescape_hex`, `function unescape_special`, `function string_unescape`, `function escape_passthrough`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.