* [PATCH v5 1/7] lib: string: add functions to case-convert strings
[not found] ` <1470863097-21138-1-git-send-email-mmayer-dY08KVG/lbpWk0Htik3J/w@public.gmane.org>
@ 2016-08-10 21:04 ` Markus Mayer
2016-08-10 21:04 ` [PATCH v5 2/7] drm/nouveau/core: make use of new strlcpytolower() function Markus Mayer
2016-08-10 21:04 ` [PATCH v5 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function Markus Mayer
2 siblings, 0 replies; 4+ messages in thread
From: Markus Mayer @ 2016-08-10 21:04 UTC (permalink / raw)
To: Andrew Morton, Al Viro, Rasmus Villemoes, Chris Metcalf,
Kees Cook
Cc: devel-gWbeCf7V1WCQmaza687I9mD2FQJk+8+b,
linux-scsi-u79uwXL29TY76Z2rM5mHXA,
linux-pm-u79uwXL29TY76Z2rM5mHXA,
nouveau-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
speakup-UPO/6gOIxNZglr+F8WMZYdi2O/JbrIOy,
linux-kernel-u79uwXL29TY76Z2rM5mHXA,
dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
linux-acpi-u79uwXL29TY76Z2rM5mHXA,
target-devel-u79uwXL29TY76Z2rM5mHXA
Add a collection of generic functions to convert strings to lowercase
or uppercase.
Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.
The new functions are
int strlcpytoupper(char *dst, const char *src, size_t len);
int strlcpytolower(char *dst, const char *src, size_t len);
void strcpytoupper(char *dst, const char *src);
void strcpytolower(char *dst, const char *src);
void strtoupper(char *s);
void strtolower(char *s);
The "str[l]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strlcpyto*" versions additionally
take a length argument like strlcpy() itself. Lastly, the strto*
functions take a single string argument and modify the passed-in string.
strlcpytoupper() and strlcpytolower() return the number of characters
copied or -E2BIG if the destination string was truncated.
Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
termination of the destination string.
Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
lib/string.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..257d797 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
#endif
void *memchr_inv(const void *s, int c, size_t n);
char *strreplace(char *s, char old, char new);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
extern void kfree_const(const void *x);
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
}
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+ strlcpytoupper(dst, src, ~(size_t)0);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+ strlcpytolower(dst, src, ~(size_t)0);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+ strlcpytoupper(s, s, ~(size_t)0);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+ strlcpytolower(s, s, ~(size_t)0);
+}
+
#endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..d36d5fb2 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,49 @@ char *strreplace(char *s, char old, char new)
return s;
}
EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytoupper(char *dst, const char *src, size_t len)
+{
+ size_t i;
+
+ if (!len)
+ return -E2BIG;
+
+ for (i = 0; i < len && src[i]; ++i)
+ dst[i] = toupper(src[i]);
+ dst[i < len ? i : i - 1] = '\0';
+
+ return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytolower(char *dst, const char *src, size_t len)
+{
+ size_t i;
+
+ if (!len)
+ return -E2BIG;
+
+ for (i = 0; i < len && src[i]; ++i)
+ dst[i] = tolower(src[i]);
+ dst[i < len ? i : i - 1] = '\0';
+
+ return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytolower);
--
2.7.4
_______________________________________________
Nouveau mailing list
Nouveau@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/nouveau
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v5 2/7] drm/nouveau/core: make use of new strlcpytolower() function
[not found] ` <1470863097-21138-1-git-send-email-mmayer-dY08KVG/lbpWk0Htik3J/w@public.gmane.org>
2016-08-10 21:04 ` [PATCH v5 1/7] " Markus Mayer
@ 2016-08-10 21:04 ` Markus Mayer
2016-08-10 21:04 ` [PATCH v5 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function Markus Mayer
2 siblings, 0 replies; 4+ messages in thread
From: Markus Mayer @ 2016-08-10 21:04 UTC (permalink / raw)
To: Ben Skeggs, David Airlie, Alexandre Courbot
Cc: nouveau-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
linux-kernel-u79uwXL29TY76Z2rM5mHXA
Call strlcpytolower() rather than copying the string explicitly and
then walking it to convert it to lowercase.
Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c
index 34ecd4a..982601e 100644
--- a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c
+++ b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c
@@ -36,16 +36,9 @@ nvkm_firmware_get(struct nvkm_device *device, const char *fwname,
{
char f[64];
char cname[16];
- int i;
/* Convert device name to lowercase */
- strncpy(cname, device->chip->name, sizeof(cname));
- cname[sizeof(cname) - 1] = '\0';
- i = strlen(cname);
- while (i) {
- --i;
- cname[i] = tolower(cname[i]);
- }
+ strlcpytolower(cname, device->chip->name, sizeof(cname));
snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname);
return request_firmware(fw, f, device->dev);
--
2.7.4
_______________________________________________
Nouveau mailing list
Nouveau@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/nouveau
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v5 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
[not found] ` <1470863097-21138-1-git-send-email-mmayer-dY08KVG/lbpWk0Htik3J/w@public.gmane.org>
2016-08-10 21:04 ` [PATCH v5 1/7] " Markus Mayer
2016-08-10 21:04 ` [PATCH v5 2/7] drm/nouveau/core: make use of new strlcpytolower() function Markus Mayer
@ 2016-08-10 21:04 ` Markus Mayer
2 siblings, 0 replies; 4+ messages in thread
From: Markus Mayer @ 2016-08-10 21:04 UTC (permalink / raw)
To: Ben Skeggs, David Airlie, Alexandre Courbot
Cc: nouveau-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
linux-kernel-u79uwXL29TY76Z2rM5mHXA
Call strcpytoupper() rather than copying the string explicitly and then
walking it to convert it to uppercase.
Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c
index 103c0af..24f4c45 100644
--- a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c
+++ b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c
@@ -332,10 +332,7 @@ gk104_fifo_intr_fault(struct gk104_fifo *fifo, int unit)
enum nvkm_devidx engidx = nvkm_top_fault(device, unit);
if (engidx < NVKM_SUBDEV_NR) {
const char *src = nvkm_subdev_name[engidx];
- char *dst = en;
- do {
- *dst++ = toupper(*src++);
- } while(*src);
+ strcpytoupper(en, src);
engine = nvkm_device_engine(device, engidx);
}
} else {
--
2.7.4
_______________________________________________
Nouveau mailing list
Nouveau@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/nouveau
^ permalink raw reply related [flat|nested] 4+ messages in thread