diff options
author | Warner Losh <imp@FreeBSD.org> | 2016-03-28 20:16:29 +0000 |
---|---|---|
committer | Warner Losh <imp@FreeBSD.org> | 2016-03-28 20:16:29 +0000 |
commit | cb49b6548156943031d9f343bae1b83f07fe0f9c (patch) | |
tree | cebdea9244d6d12a07e2e230059121682f58db19 /sys/kern/subr_bus.c | |
parent | c8edf4bc2aef3df69b519d533bccabc429aa5bd3 (diff) | |
download | src-cb49b6548156943031d9f343bae1b83f07fe0f9c.tar.gz src-cb49b6548156943031d9f343bae1b83f07fe0f9c.zip |
Move pccard_safe_quote() up to subr_bus.c and rename to
devctl_safe_quote() so it can be used more generally.
Notes
Notes:
svn path=/head/; revision=297365
Diffstat (limited to 'sys/kern/subr_bus.c')
-rw-r--r-- | sys/kern/subr_bus.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 8daa9f2bc6d8..caf92025c7d5 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -839,6 +839,38 @@ sysctl_devctl_queue(SYSCTL_HANDLER_ARGS) return (0); } +/** + * @brief safely quotes strings that might have double quotes in them. + * + * The devctl protocol relies on quoted strings having matching quotes. + * This routine quotes any internal quotes so the resulting string + * is safe to pass to snprintf to construct, for example pnp info strings. + * Strings are always terminated with a NUL, but may be truncated if longer + * than @p len bytes after quotes. + * + * @param dst Buffer to hold the string. Must be at least @p len bytes long + * @param src Original buffer. + * @param len Length of buffer pointed to by @dst, including trailing NUL + */ +void +devctl_safe_quote(char *dst, const char *src, size_t len) +{ + char *walker = dst, *ep = dst + len - 1; + + if (len == 0) + return; + while (src != NULL && walker < ep) + { + if (*src == '"') { + if (ep - walker < 2) + break; + *walker++ = '\\'; + } + *walker++ = *src++; + } + *walker = '\0'; +} + /* End of /dev/devctl code */ static TAILQ_HEAD(,device) bus_data_devices; |