|
|
1.1 root 1: /******************************************************************************
2: * Copyright (c) 2004, 2008 IBM Corporation
3: * All rights reserved.
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
8: *
9: * Contributors:
10: * IBM Corporation - initial implementation
11: *****************************************************************************/
12:
1.1.1.2 root 13: #include <stdint.h>
1.1 root 14: #include <ctype.h>
15: #include <stdlib.h>
16: #include <string.h>
1.1.1.3 ! root 17: #include "args.h"
1.1 root 18:
19: /**
20: * Returns pointer of the n'th argument within a string.
21: *
22: * @param arg_str string with arguments, seperated with ','
23: * @param index index of the requested arguments whithin arg_str
24: * @return pointer of argument[index] on success
25: * NULL if index is out of range
26: */
27: const char *
28: get_arg_ptr(const char *arg_str, unsigned int index)
29: {
30: unsigned int i;
31:
32: for (i = 0; i < index; ++i) {
33: for (; *arg_str != ',' && *arg_str != 0; ++arg_str);
34: if (*arg_str == 0)
35: return 0;
36: ++arg_str;
37: }
38: return arg_str;
39: }
40:
41: /**
42: * Returns number of arguments within a string.
43: *
44: * @param arg_str string with arguments, seperated with ','
45: * @return number of arguments
46: */
47: unsigned int
48: get_args_count(const char *arg_str)
49: {
50: unsigned int count = 1;
51:
52: while ((arg_str = get_arg_ptr(arg_str, 1)) != 0)
53: ++count;
54: return count;
55: }
56:
57: /**
58: * Returns the length of the first argument.
59: *
60: * @param arg_str string with arguments, seperated with ','
61: * @return length of first argument
62: */
63: unsigned int
64: get_arg_length(const char *arg_str)
65: {
66: unsigned int i;
67:
68: for (i = 0; *arg_str != ',' && *arg_str != 0; ++i)
69: ++arg_str;
70: return i;
71: }
72:
73: /**
74: * Copy the n'th argument within a string into a buffer in respect
75: * to a limited buffer size
76: *
77: * @param arg_str string with arguments, seperated with ','
78: * @param index index of the requested arguments whithin 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.
83: */
84: char *
85: argncpy(const char *arg_str, unsigned int index, char *buffer,
86: unsigned int length)
87: {
88: const char *ptr = get_arg_ptr(arg_str, index);
89: unsigned int len;
90:
91: if (!ptr)
92: return 0;
93: len = get_arg_length(ptr);
94: if (!strncpy(buffer, ptr, length))
95: return 0;
96: buffer[len] = 0;
97: return buffer;
98: }
99:
100: /**
101: * Converts "255.255.255.255" -> char[4] = { 0xff, 0xff, 0xff, 0xff }
102: *
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)
108: */
109: int
110: strtoip(const char *str, char ip[4])
111: {
112: char octet[10];
113: int res;
114: unsigned int i = 0, len;
115:
116: while (*str != 0) {
117: if (i > 3 || !isdigit(*str))
118: return 0;
119: if (strstr(str, ".") != NULL) {
120: len = (int16_t) (strstr(str, ".") - str);
121: if (len >= 10)
122: return 0;
123: strncpy(octet, str, len);
124: octet[len] = 0;
125: str += len;
126: } else {
127: strncpy(octet, str, 9);
128: octet[9] = 0;
129: str += strlen(octet);
130: }
131: res = strtol(octet, NULL, 10);
132: if ((res > 255) || (res < 0))
133: return 0;
134: ip[i] = (char) res;
135: i++;
136: if (*str == '.')
137: str++;
138: }
139:
140: if (i != 4)
141: return 0;
142: return -1;
143: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.