summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/clients/net-snk/app/netapps/args.c
blob: 52215e6a7dbe8b1739f5a456bda32108d1eff644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <stdint.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "args.h"

/**
 * Returns pointer of the n'th argument within a string.
 *
 * @param  arg_str    string with arguments, separated with ','
 * @param  index      index of the requested arguments within arg_str
 * @return            pointer of argument[index] on success
 *                    NULL if index is out of range
 */
const char *
get_arg_ptr(const char *arg_str, unsigned int index)
{
	unsigned int i;

	for (i = 0; i < index; ++i) {
		for (; *arg_str != ',' && *arg_str != 0; ++arg_str);
		if (*arg_str == 0)
			return 0;
		++arg_str;
	}
	return arg_str;
}

/**
 * Returns number of arguments within a string.
 *
 * @param  arg_str    string with arguments, separated with ','
 * @return            number of arguments
 */
unsigned int
get_args_count(const char *arg_str)
{
	unsigned int count = 1;

	while ((arg_str = get_arg_ptr(arg_str, 1)) != 0)
		++count;
	return count;
}

/**
 * Returns the length of the first argument.
 *
 * @param  arg_str    string with arguments, separated with ','
 * @return            length of first argument
 */
unsigned int
get_arg_length(const char *arg_str)
{
	unsigned int i;

	for (i = 0; *arg_str != ',' && *arg_str != 0; ++i)
		++arg_str;
	return i;
}

/**
 * Copy the n'th argument within a string into a buffer in respect
 * to a limited buffer size
 *
 * @param  arg_str    string with arguments, separated with ','
 * @param  index      index of the requested arguments within arg_str
 * @param  buffer     pointer to the buffer
 * @param  length     size of the buffer
 * @return            pointer of buffer on success
 *                    NULL if index is out of range.
 */
char *
argncpy(const char *arg_str, unsigned int index, char *buffer,
	unsigned int length)
{
	const char *ptr = get_arg_ptr(arg_str, index);
	unsigned int len;

	if (!ptr)
		return 0;
	len = get_arg_length(ptr);
	if (!strncpy(buffer, ptr, length))
		return 0;
	buffer[len] = 0;
	return buffer;
}

/**
 * Converts "255.255.255.255" -> char[4] = { 0xff, 0xff, 0xff, 0xff }
 *
 * @param  str        string to be converted
 * @param  ip         in case of SUCCESS - 32-bit long IP
                      in case of FAULT - zero
 * @return            TRUE - IP converted successfully;
 *                    FALSE - error condition occurs (e.g. bad format)
 */
int
strtoip(const char *str, char ip[4])
{
	char octet[10];
	int res;
	unsigned int i = 0, len;

	while (*str != 0) {
		if (i > 3 || !isdigit(*str))
			return 0;
		if (strstr(str, ".") != NULL) {
			len = (int16_t) (strstr(str, ".") - str);
			if (len >= 10)
				return 0;
			strncpy(octet, str, len);
			octet[len] = 0;
			str += len;
		} else {
			strncpy(octet, str, 9);
			octet[9] = 0;
			str += strlen(octet);
		}
		res = strtol(octet, NULL, 10);
		if ((res > 255) || (res < 0))
			return 0;
		ip[i] = (char) res;
		i++;
		if (*str == '.')
			str++;
	}

	if (i != 4)
		return 0;
	return -1;
}