Skip to content

Commit

Permalink
tree-wide: convert open calls to remove spaces to skip_spaces() lib f…
Browse files Browse the repository at this point in the history
…unction

Makes use of skip_spaces() defined in lib/string.c for removing leading
spaces from strings all over the tree.

It decreases lib.a code size by 47 bytes and reuses the function tree-wide:
   text    data     bss     dec     hex filename
  64688     584     592   65864   10148 (TOTALS-BEFORE)
  64641     584     592   65817   10119 (TOTALS-AFTER)

Also, while at it, if we see (*str && isspace(*str)), we can be sure to
remove the first condition (*str) as the second one (isspace(*str)) also
evaluates to 0 whenever *str == 0, making it redundant. In other words,
"a char equals zero is never a space".

Julia Lawall tried the semantic patch (http://coccinelle.lip6.fr) below,
and found occurrences of this pattern on 3 more files:
    drivers/leds/led-class.c
    drivers/leds/ledtrig-timer.c
    drivers/video/output.c

@@
expression str;
@@

( // ignore skip_spaces cases
while (*str &&  isspace(*str)) { \(str++;\|++str;\) }
|
- *str &&
isspace(*str)
)

Signed-off-by: André Goddard Rosa <[email protected]>
Cc: Julia Lawall <[email protected]>
Cc: Martin Schwidefsky <[email protected]>
Cc: Jeff Dike <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Richard Purdie <[email protected]>
Cc: Neil Brown <[email protected]>
Cc: Kyle McMartin <[email protected]>
Cc: Henrique de Moraes Holschuh <[email protected]>
Cc: David Howells <[email protected]>
Cc: <[email protected]>
Cc: Samuel Ortiz <[email protected]>
Cc: Patrick McHardy <[email protected]>
Cc: Takashi Iwai <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
andre-rosa authored and torvalds committed Dec 15, 2009
1 parent 84c95c9 commit e7d2860
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 115 deletions.
3 changes: 2 additions & 1 deletion arch/s390/kernel/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/sysctl.h>
#include <asm/uaccess.h>
#include <linux/module.h>
Expand Down Expand Up @@ -1178,7 +1179,7 @@ debug_get_uint(char *buf)
{
int rc;

for(; isspace(*buf); buf++);
buf = skip_spaces(buf);
rc = simple_strtoul(buf, &buf, 10);
if(*buf){
rc = -EINVAL;
Expand Down
16 changes: 7 additions & 9 deletions arch/um/drivers/mconsole_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <linux/console.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/mm.h>
Expand Down Expand Up @@ -131,7 +132,7 @@ void mconsole_proc(struct mc_request *req)
char *ptr = req->request.data, *buf;

ptr += strlen("proc");
while (isspace(*ptr)) ptr++;
ptr = skip_spaces(ptr);

proc = get_fs_type("proc");
if (proc == NULL) {
Expand Down Expand Up @@ -212,8 +213,7 @@ void mconsole_proc(struct mc_request *req)
char *ptr = req->request.data;

ptr += strlen("proc");
while (isspace(*ptr))
ptr++;
ptr = skip_spaces(ptr);
snprintf(path, sizeof(path), "/proc/%s", ptr);

fd = sys_open(path, 0, 0);
Expand Down Expand Up @@ -560,8 +560,7 @@ void mconsole_config(struct mc_request *req)
int err;

ptr += strlen("config");
while (isspace(*ptr))
ptr++;
ptr = skip_spaces(ptr);
dev = mconsole_find_dev(ptr);
if (dev == NULL) {
mconsole_reply(req, "Bad configuration option", 1, 0);
Expand All @@ -588,7 +587,7 @@ void mconsole_remove(struct mc_request *req)
int err, start, end, n;

ptr += strlen("remove");
while (isspace(*ptr)) ptr++;
ptr = skip_spaces(ptr);
dev = mconsole_find_dev(ptr);
if (dev == NULL) {
mconsole_reply(req, "Bad remove option", 1, 0);
Expand Down Expand Up @@ -712,7 +711,7 @@ void mconsole_sysrq(struct mc_request *req)
char *ptr = req->request.data;

ptr += strlen("sysrq");
while (isspace(*ptr)) ptr++;
ptr = skip_spaces(ptr);

/*
* With 'b', the system will shut down without a chance to reply,
Expand Down Expand Up @@ -757,8 +756,7 @@ void mconsole_stack(struct mc_request *req)
*/

ptr += strlen("stack");
while (isspace(*ptr))
ptr++;
ptr = skip_spaces(ptr);

/*
* Should really check for multiple pids or reject bad args here
Expand Down
11 changes: 4 additions & 7 deletions arch/x86/kernel/cpu/mtrr/if.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/init.h>

#define LINE_SIZE 80
Expand Down Expand Up @@ -133,23 +134,19 @@ mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
return -EINVAL;

base = simple_strtoull(line + 5, &ptr, 0);
while (isspace(*ptr))
ptr++;
ptr = skip_spaces(ptr);

if (strncmp(ptr, "size=", 5))
return -EINVAL;

size = simple_strtoull(ptr + 5, &ptr, 0);
if ((base & 0xfff) || (size & 0xfff))
return -EINVAL;
while (isspace(*ptr))
ptr++;
ptr = skip_spaces(ptr);

if (strncmp(ptr, "type=", 5))
return -EINVAL;
ptr += 5;
while (isspace(*ptr))
ptr++;
ptr = skip_spaces(ptr + 5);

for (i = 0; i < MTRR_NUM_TYPES; ++i) {
if (strcmp(ptr, mtrr_strings[i]))
Expand Down
2 changes: 1 addition & 1 deletion drivers/leds/led-class.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static ssize_t led_brightness_store(struct device *dev,
unsigned long state = simple_strtoul(buf, &after, 10);
size_t count = after - buf;

if (*after && isspace(*after))
if (isspace(*after))
count++;

if (count == size) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/leds/ledtrig-timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static ssize_t led_delay_on_store(struct device *dev,
unsigned long state = simple_strtoul(buf, &after, 10);
size_t count = after - buf;

if (*after && isspace(*after))
if (isspace(*after))
count++;

if (count == size) {
Expand Down Expand Up @@ -127,7 +127,7 @@ static ssize_t led_delay_off_store(struct device *dev,
unsigned long state = simple_strtoul(buf, &after, 10);
size_t count = after - buf;

if (*after && isspace(*after))
if (isspace(*after))
count++;

if (count == size) {
Expand Down
6 changes: 2 additions & 4 deletions drivers/md/dm-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <linux/blkdev.h>
#include <linux/namei.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/mutex.h>
Expand Down Expand Up @@ -600,11 +601,8 @@ int dm_split_args(int *argc, char ***argvp, char *input)
return -ENOMEM;

while (1) {
start = end;

/* Skip whitespace */
while (*start && isspace(*start))
start++;
start = skip_spaces(end);

if (!*start)
break; /* success, we hit the end */
Expand Down
4 changes: 2 additions & 2 deletions drivers/md/md.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <linux/buffer_head.h> /* for invalidate_bdev */
#include <linux/poll.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/hdreg.h>
#include <linux/proc_fs.h>
#include <linux/random.h>
Expand Down Expand Up @@ -3439,8 +3440,7 @@ bitmap_store(mddev_t *mddev, const char *buf, size_t len)
}
if (*end && !isspace(*end)) break;
bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
buf = end;
while (isspace(*buf)) buf++;
buf = skip_spaces(end);
}
bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
out:
Expand Down
9 changes: 3 additions & 6 deletions drivers/parisc/pdc_stable.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,9 @@ static ssize_t pdcs_auto_write(struct kobject *kobj,
read_unlock(&pathentry->rw_lock);

DPRINTK("%s: flags before: 0x%X\n", __func__, flags);

temp = in;

while (*temp && isspace(*temp))
temp++;


temp = skip_spaces(in);

c = *temp++ - '0';
if ((c != 0) && (c != 1))
goto parse_error;
Expand Down
7 changes: 2 additions & 5 deletions drivers/platform/x86/thinkpad_acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,8 @@ static int parse_strtoul(const char *buf,
{
char *endp;

while (*buf && isspace(*buf))
buf++;
*value = simple_strtoul(buf, &endp, 0);
while (*endp && isspace(*endp))
endp++;
*value = simple_strtoul(skip_spaces(buf), &endp, 0);
endp = skip_spaces(endp);
if (*endp || *value > max)
return -EINVAL;

Expand Down
36 changes: 10 additions & 26 deletions drivers/pnp/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ static ssize_t pnp_set_current_resources(struct device *dmdev,
goto done;
}

while (isspace(*buf))
++buf;
buf = skip_spaces(buf);
if (!strnicmp(buf, "disable", 7)) {
retval = pnp_disable_dev(dev);
goto done;
Expand Down Expand Up @@ -353,54 +352,39 @@ static ssize_t pnp_set_current_resources(struct device *dmdev,
pnp_init_resources(dev);
mutex_lock(&pnp_res_mutex);
while (1) {
while (isspace(*buf))
++buf;
buf = skip_spaces(buf);
if (!strnicmp(buf, "io", 2)) {
buf += 2;
while (isspace(*buf))
++buf;
buf = skip_spaces(buf + 2);
start = simple_strtoul(buf, &buf, 0);
while (isspace(*buf))
++buf;
buf = skip_spaces(buf);
if (*buf == '-') {
buf += 1;
while (isspace(*buf))
++buf;
buf = skip_spaces(buf + 1);
end = simple_strtoul(buf, &buf, 0);
} else
end = start;
pnp_add_io_resource(dev, start, end, 0);
continue;
}
if (!strnicmp(buf, "mem", 3)) {
buf += 3;
while (isspace(*buf))
++buf;
buf = skip_spaces(buf + 3);
start = simple_strtoul(buf, &buf, 0);
while (isspace(*buf))
++buf;
buf = skip_spaces(buf);
if (*buf == '-') {
buf += 1;
while (isspace(*buf))
++buf;
buf = skip_spaces(buf + 1);
end = simple_strtoul(buf, &buf, 0);
} else
end = start;
pnp_add_mem_resource(dev, start, end, 0);
continue;
}
if (!strnicmp(buf, "irq", 3)) {
buf += 3;
while (isspace(*buf))
++buf;
buf = skip_spaces(buf + 3);
start = simple_strtoul(buf, &buf, 0);
pnp_add_irq_resource(dev, start, 0);
continue;
}
if (!strnicmp(buf, "dma", 3)) {
buf += 3;
while (isspace(*buf))
++buf;
buf = skip_spaces(buf + 3);
start = simple_strtoul(buf, &buf, 0);
pnp_add_dma_resource(dev, start, 0);
continue;
Expand Down
5 changes: 3 additions & 2 deletions drivers/s390/block/dasd_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define KMSG_COMPONENT "dasd"

#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/seq_file.h>
#include <linux/vmalloc.h>
#include <linux/proc_fs.h>
Expand Down Expand Up @@ -272,10 +273,10 @@ dasd_statistics_write(struct file *file, const char __user *user_buf,
DBF_EVENT(DBF_DEBUG, "/proc/dasd/statictics: '%s'\n", buffer);

/* check for valid verbs */
for (str = buffer; isspace(*str); str++);
str = skip_spaces(buffer);
if (strncmp(str, "set", 3) == 0 && isspace(str[3])) {
/* 'set xxx' was given */
for (str = str + 4; isspace(*str); str++);
str = skip_spaces(str + 4);
if (strcmp(str, "on") == 0) {
/* switch on statistics profiling */
dasd_profile_level = DASD_PROFILE_ON;
Expand Down
4 changes: 2 additions & 2 deletions drivers/video/backlight/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static ssize_t lcd_store_power(struct device *dev,
int power = simple_strtoul(buf, &endp, 0);
size_t size = endp - buf;

if (*endp && isspace(*endp))
if (isspace(*endp))
size++;
if (size != count)
return -EINVAL;
Expand Down Expand Up @@ -140,7 +140,7 @@ static ssize_t lcd_store_contrast(struct device *dev,
int contrast = simple_strtoul(buf, &endp, 0);
size_t size = endp - buf;

if (*endp && isspace(*endp))
if (isspace(*endp))
size++;
if (size != count)
return -EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion drivers/video/display/display-sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static ssize_t display_store_contrast(struct device *dev,
contrast = simple_strtoul(buf, &endp, 0);
size = endp - buf;

if (*endp && isspace(*endp))
if (isspace(*endp))
size++;

if (size != count)
Expand Down
2 changes: 1 addition & 1 deletion drivers/video/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static ssize_t video_output_store_state(struct device *dev,
int request_state = simple_strtoul(buf,&endp,0);
size_t size = endp - buf;

if (*endp && isspace(*endp))
if (isspace(*endp))
size++;
if (size != count)
return -EINVAL;
Expand Down
4 changes: 2 additions & 2 deletions fs/cachefiles/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <linux/mount.h>
#include <linux/statfs.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/fs_struct.h>
#include "internal.h"

Expand Down Expand Up @@ -257,8 +258,7 @@ static ssize_t cachefiles_daemon_write(struct file *file,
if (args == data)
goto error;
*args = '\0';
for (args++; isspace(*args); args++)
continue;
args = skip_spaces(++args);
}

/* run the appropriate command handler */
Expand Down
7 changes: 2 additions & 5 deletions fs/ext4/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -2137,11 +2137,8 @@ static int parse_strtoul(const char *buf,
{
char *endp;

while (*buf && isspace(*buf))
buf++;
*value = simple_strtoul(buf, &endp, 0);
while (*endp && isspace(*endp))
endp++;
*value = simple_strtoul(skip_spaces(buf), &endp, 0);
endp = skip_spaces(endp);
if (*endp || *value > max)
return -EINVAL;

Expand Down
Loading

0 comments on commit e7d2860

Please sign in to comment.