1 /*	$NetBSD: argv_split.c,v 1.1.1.1 2009/06/23 10:08:58 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	argv_split 3
6 /* SUMMARY
7 /*	string array utilities
8 /* SYNOPSIS
9 /*	#include <argv.h>
10 /*
11 /*	ARGV	*argv_split(string, delim)
12 /*	const char *string;
13 /*
14 /*	ARGV	*argv_split_count(string, delim, count)
15 /*	const char *string;
16 /*	ssize_t	count;
17 /*
18 /*	ARGV	*argv_split_append(argv, string, delim)
19 /*	ARGV	*argv;
20 /*	const char *string;
21 /*	const char *delim;
22 /* DESCRIPTION
23 /*	argv_split() breaks up \fIstring\fR into tokens according
24 /*	to the delimiters specified in \fIdelim\fR. The result is
25 /*	a null-terminated string array.
26 /*
27 /*	argv_split_count() is like argv_split() but stops splitting
28 /*	input after at most \fIcount\fR -1 times and leaves the
29 /*	remainder, if any, in the last array element. It is an error
30 /*	to specify a count < 1.
31 /*
32 /*	argv_split_append() performs the same operation as argv_split(),
33 /*	but appends the result to an existing string array.
34 /* SEE ALSO
35 /*	mystrtok(), safe string splitter.
36 /* DIAGNOSTICS
37 /*	Fatal errors: memory allocation problem.
38 /* LICENSE
39 /* .ad
40 /* .fi
41 /*	The Secure Mailer license must be distributed with this software.
42 /* AUTHOR(S)
43 /*	Wietse Venema
44 /*	IBM T.J. Watson Research
45 /*	P.O. Box 704
46 /*	Yorktown Heights, NY 10598, USA
47 /*--*/
48 
49 /* System libraries. */
50 
51 #include <sys_defs.h>
52 #include <string.h>
53 
54 /* Application-specific. */
55 
56 #include "mymalloc.h"
57 #include "stringops.h"
58 #include "argv.h"
59 #include "msg.h"
60 
61 /* argv_split - split string into token array */
62 
63 ARGV   *argv_split(const char *string, const char *delim)
64 {
65     ARGV   *argvp = argv_alloc(1);
66     char   *saved_string = mystrdup(string);
67     char   *bp = saved_string;
68     char   *arg;
69 
70     while ((arg = mystrtok(&bp, delim)) != 0)
71 	argv_add(argvp, arg, (char *) 0);
72     argv_terminate(argvp);
73     myfree(saved_string);
74     return (argvp);
75 }
76 
77 /* argv_split_count - split string into token array */
78 
79 ARGV   *argv_split_count(const char *string, const char *delim, ssize_t count)
80 {
81     ARGV   *argvp = argv_alloc(1);
82     char   *saved_string = mystrdup(string);
83     char   *bp = saved_string;
84     char   *arg;
85 
86     if (count < 1)
87 	msg_panic("argv_split_count: bad count: %ld", (long) count);
88     while (count-- > 1 && (arg = mystrtok(&bp, delim)) != 0)
89 	argv_add(argvp, arg, (char *) 0);
90     if (*bp)
91 	bp += strspn(bp, delim);
92     if (*bp)
93 	argv_add(argvp, bp, (char *) 0);
94     argv_terminate(argvp);
95     myfree(saved_string);
96     return (argvp);
97 }
98 
99 /* argv_split_append - split string into token array, append to array */
100 
101 ARGV   *argv_split_append(ARGV *argvp, const char *string, const char *delim)
102 {
103     char   *saved_string = mystrdup(string);
104     char   *bp = saved_string;
105     char   *arg;
106 
107     while ((arg = mystrtok(&bp, delim)) != 0)
108 	argv_add(argvp, arg, (char *) 0);
109     argv_terminate(argvp);
110     myfree(saved_string);
111     return (argvp);
112 }
113