File: str.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (54 lines) | stat: -rw-r--r-- 1,239 bytes parent folder | download
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
/** \file shared/str.c
 * Commmand / argument parsing functions (for use in clients).
 */

/*-
 * This file is part of LCDproc.
 *
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "report.h"
#include "str.h"

/** Split elements of a string into an array of strings.
 * Elements are typically commands and arguments.
 * \param **argv    Pointer to the array which will store the arguments
 * \param *str      The string to be parsed
 * \param max_args  Number of arguments to parse (typically the size of argv)
 * \retval <0       Error.
 * \retval >=0      The number of arguments parsed.
 */
int
get_args (char **argv, char *str, int max_args)
{
	char *delimiters = " \n";
	char *item;
	int i = 0;

	if (!argv)
		return -1;
	if (!str)
		return 0;
	if (max_args < 1)
		return 0;

	debug(RPT_DEBUG, "get_args(%i): string=%s", max_args, str);

	/* Parse the command line... */
	for (item = strtok (str, delimiters); item; item = strtok (NULL, delimiters)) {
		debug(RPT_DEBUG, "get_args: item=%s", item);
		if (i < max_args) {
			argv[i] = item;
			i++;
		} else
			return i;
	}

	return i;
}