No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / argv_split.c
blob1f478331b67ff8061cfe248631047f9634b4d55a
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* argv_split 3
6 /* SUMMARY
7 /* string array utilities
8 /* SYNOPSIS
9 /* #include <argv.h>
11 /* ARGV *argv_split(string, delim)
12 /* const char *string;
14 /* ARGV *argv_split_count(string, delim, count)
15 /* const char *string;
16 /* ssize_t count;
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.
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 /*--*/
49 /* System libraries. */
51 #include <sys_defs.h>
52 #include <string.h>
54 /* Application-specific. */
56 #include "mymalloc.h"
57 #include "stringops.h"
58 #include "argv.h"
59 #include "msg.h"
61 /* argv_split - split string into token array */
63 ARGV *argv_split(const char *string, const char *delim)
65 ARGV *argvp = argv_alloc(1);
66 char *saved_string = mystrdup(string);
67 char *bp = saved_string;
68 char *arg;
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);
77 /* argv_split_count - split string into token array */
79 ARGV *argv_split_count(const char *string, const char *delim, ssize_t count)
81 ARGV *argvp = argv_alloc(1);
82 char *saved_string = mystrdup(string);
83 char *bp = saved_string;
84 char *arg;
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);
99 /* argv_split_append - split string into token array, append to array */
101 ARGV *argv_split_append(ARGV *argvp, const char *string, const char *delim)
103 char *saved_string = mystrdup(string);
104 char *bp = saved_string;
105 char *arg;
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);