| 1 |
tdb |
1.1 |
/* ssplit.c - split a string into a vector of arguments */ |
| 2 |
|
|
|
| 3 |
|
|
/* Copyright 1991 Mark Russell, University of Kent at Canterbury. |
| 4 |
|
|
* |
| 5 |
|
|
* You can do what you like with this source code as long as |
| 6 |
|
|
* you don't try to make money out of it and you include an |
| 7 |
|
|
* unaltered copy of this message (including the copyright). |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
char ukcprog_ssplit_sccsid[] = "$Id: ssplit.c,v 1.7 1993/05/30 18:15:15 gjap Exp $ UKC"; |
| 11 |
|
|
|
| 12 |
|
|
#include <string.h> |
| 13 |
|
|
#include <stdlib.h> |
| 14 |
|
|
|
| 15 |
|
|
#include "ukcprog.h" |
| 16 |
|
|
|
| 17 |
|
|
/* Split a string into a NULL terminated vector of words. |
| 18 |
|
|
* The original string is not modified. |
| 19 |
|
|
* |
| 20 |
|
|
* Words in the given string are delimited by any character in |
| 21 |
|
|
* the delimiters argument. |
| 22 |
|
|
* |
| 23 |
|
|
* By default leading delimiters are ignored and multiple adjacent |
| 24 |
|
|
* delimiters are treated as a single delimiter. If the delimiters |
| 25 |
|
|
* string starts with a NUL then each occurence of a delimiter |
| 26 |
|
|
* indicates a field and zero length fields are possible. Thus to |
| 27 |
|
|
* split a password file line, use ssplit(line, "\0:"). |
| 28 |
|
|
* |
| 29 |
|
|
* The returned vector is malloc'ed storage - when it is finished with |
| 30 |
|
|
* it should be passed to free() to release its storage. |
| 31 |
|
|
*/ |
| 32 |
|
|
char ** |
| 33 |
|
|
ssplit(line, delimiters) |
| 34 |
|
|
const char *line, *delimiters; |
| 35 |
|
|
{ |
| 36 |
|
|
int ncomp; |
| 37 |
|
|
bool want_null_fields; |
| 38 |
|
|
const char *cp; |
| 39 |
|
|
char **vecp, *buf; |
| 40 |
|
|
char *mem; |
| 41 |
|
|
|
| 42 |
|
|
want_null_fields = *delimiters == '\0'; |
| 43 |
|
|
if (want_null_fields) |
| 44 |
|
|
++delimiters; |
| 45 |
|
|
|
| 46 |
|
|
ncomp = 1; |
| 47 |
|
|
if (!want_null_fields) { |
| 48 |
|
|
while (*line != '\0' && strchr(delimiters, *line) != NULL) |
| 49 |
|
|
++line; |
| 50 |
|
|
} |
| 51 |
|
|
for (cp = line; *cp != '\0'; cp++) |
| 52 |
|
|
if (strchr(delimiters, *cp) != NULL) |
| 53 |
|
|
++ncomp; |
| 54 |
|
|
|
| 55 |
|
|
/* We need ncomp+1 char* sized slots for the string pointers |
| 56 |
|
|
* and terminating NULL, plus space for a copy of the string |
| 57 |
|
|
* including the terminating NUL. |
| 58 |
|
|
*/ |
| 59 |
|
|
mem = e_malloc((ncomp + 1) * sizeof(char *) + strlen(line) + 1); |
| 60 |
|
|
|
| 61 |
|
|
vecp = (char **)mem; |
| 62 |
|
|
buf = mem + (ncomp + 1) * sizeof(char *); |
| 63 |
|
|
strcpy(buf, line); |
| 64 |
|
|
|
| 65 |
|
|
for (;;) { |
| 66 |
|
|
if (!want_null_fields) { |
| 67 |
|
|
while (*buf != '\0' && |
| 68 |
|
|
strchr(delimiters, *buf) != NULL) |
| 69 |
|
|
++buf; |
| 70 |
|
|
if (*buf == '\0') |
| 71 |
|
|
break; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
*vecp++ = buf; |
| 75 |
|
|
|
| 76 |
|
|
if (*buf == '\0') |
| 77 |
|
|
break; |
| 78 |
|
|
|
| 79 |
|
|
while (*buf != '\0' && strchr(delimiters, *buf) == NULL) |
| 80 |
|
|
buf++; |
| 81 |
|
|
|
| 82 |
|
|
if (*buf == '\0') |
| 83 |
|
|
break; |
| 84 |
|
|
*buf++ = '\0'; |
| 85 |
|
|
} |
| 86 |
|
|
*vecp = NULL; |
| 87 |
|
|
|
| 88 |
|
|
return (char **)mem; |
| 89 |
|
|
} |