Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
509 giacomo 1
/*
2
 * linux/lib/cmdline.c
3
 * Helper functions generally used for parsing kernel command line
4
 * and module options.
5
 *
6
 * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
7
 *
8
 * This source code is licensed under the GNU General Public License,
9
 * Version 2.  See the file COPYING for more details.
10
 *
11
 * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
12
 *
13
 */
14
 
15
#include <linuxcomp.h>
16
 
17
#include <linux/module.h>
18
#include <linux/kernel.h>
19
#include <linux/string.h>
20
 
21
 
22
/**
23
 *      get_option - Parse integer from an option string
24
 *      @str: option string
25
 *      @pint: (output) integer value parsed from @str
26
 *
27
 *      Read an int from an option string; if available accept a subsequent
28
 *      comma as well.
29
 *
30
 *      Return values:
31
 *      0 : no int in string
32
 *      1 : int found, no subsequent comma
33
 *      2 : int found including a subsequent comma
34
 */
35
 
36
int get_option (char **str, int *pint)
37
{
38
        char *cur = *str;
39
 
40
        if (!cur || !(*cur))
41
                return 0;
42
        *pint = simple_strtol (cur, str, 0);
43
        if (cur == *str)
44
                return 0;
45
        if (**str == ',') {
46
                (*str)++;
47
                return 2;
48
        }
49
 
50
        return 1;
51
}
52
 
53
/**
54
 *      get_options - Parse a string into a list of integers
55
 *      @str: String to be parsed
56
 *      @nints: size of integer array
57
 *      @ints: integer array
58
 *
59
 *      This function parses a string containing a comma-separated
60
 *      list of integers.  The parse halts when the array is
61
 *      full, or when no more numbers can be retrieved from the
62
 *      string.
63
 *
64
 *      Return value is the character in the string which caused
65
 *      the parse to end (typically a null terminator, if @str is
66
 *      completely parseable).
67
 */
68
 
69
char *get_options(const char *str, int nints, int *ints)
70
{
71
        int res, i = 1;
72
 
73
        while (i < nints) {
74
                res = get_option ((char **)&str, ints + i);
75
                if (res == 0)
76
                        break;
77
                i++;
78
                if (res == 1)
79
                        break;
80
        }
81
        ints[0] = i - 1;
82
        return (char *)str;
83
}
84
 
85
/**
86
 *      memparse - parse a string with mem suffixes into a number
87
 *      @ptr: Where parse begins
88
 *      @retptr: (output) Pointer to next char after parse completes
89
 *
90
 *      Parses a string into a number.  The number stored at @ptr is
91
 *      potentially suffixed with %K (for kilobytes, or 1024 bytes),
92
 *      %M (for megabytes, or 1048576 bytes), or %G (for gigabytes, or
93
 *      1073741824).  If the number is suffixed with K, M, or G, then
94
 *      the return value is the number multiplied by one kilobyte, one
95
 *      megabyte, or one gigabyte, respectively.
96
 */
97
 
98
unsigned long long memparse (char *ptr, char **retptr)
99
{
100
        unsigned long long ret = simple_strtoull (ptr, retptr, 0);
101
 
102
        switch (**retptr) {
103
        case 'G':
104
        case 'g':
105
                ret <<= 10;
106
        case 'M':
107
        case 'm':
108
                ret <<= 10;
109
        case 'K':
110
        case 'k':
111
                ret <<= 10;
112
                (*retptr)++;
113
        default:
114
                break;
115
        }
116
        return ret;
117
}
118
 
119
 
120
EXPORT_SYMBOL(memparse);
121
EXPORT_SYMBOL(get_option);
122
EXPORT_SYMBOL(get_options);