1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
20 * Returns pointer of the n'th argument within a string.
22 * @param arg_str string with arguments, separated with ','
23 * @param index index of the requested arguments within arg_str
24 * @return pointer of argument[index] on success
25 * NULL if index is out of range
28 get_arg_ptr(const char *arg_str, unsigned int index)
32 for (i = 0; i < index; ++i) {
33 for (; *arg_str != ',' && *arg_str != 0; ++arg_str);
42 * Returns number of arguments within a string.
44 * @param arg_str string with arguments, separated with ','
45 * @return number of arguments
48 get_args_count(const char *arg_str)
50 unsigned int count = 1;
52 while ((arg_str = get_arg_ptr(arg_str, 1)) != 0)
58 * Returns the length of the first argument.
60 * @param arg_str string with arguments, separated with ','
61 * @return length of first argument
64 get_arg_length(const char *arg_str)
68 for (i = 0; *arg_str != ',' && *arg_str != 0; ++i)
74 * Copy the n'th argument within a string into a buffer in respect
75 * to a limited buffer size
77 * @param arg_str string with arguments, separated with ','
78 * @param index index of the requested arguments within arg_str
79 * @param buffer pointer to the buffer
80 * @param length size of the buffer
81 * @return pointer of buffer on success
82 * NULL if index is out of range.
85 argncpy(const char *arg_str, unsigned int index, char *buffer,
88 const char *ptr = get_arg_ptr(arg_str, index);
93 len = get_arg_length(ptr);
94 if (!strncpy(buffer, ptr, length))
101 * Converts "255.255.255.255" -> char[4] = { 0xff, 0xff, 0xff, 0xff }
103 * @param str string to be converted
104 * @param ip in case of SUCCESS - 32-bit long IP
105 in case of FAULT - zero
106 * @return TRUE - IP converted successfully;
107 * FALSE - error condition occurs (e.g. bad format)
110 strtoip(const char *str, char ip[4])
114 unsigned int i = 0, len;
117 if (i > 3 || !isdigit(*str))
119 if (strstr(str, ".") != NULL) {
120 len = (int16_t) (strstr(str, ".") - str);
123 strncpy(octet, str, len);
127 strncpy(octet, str, 9);
129 str += strlen(octet);
131 res = strtol(octet, NULL, 10);
132 if ((res > 255) || (res < 0))