File: split.c

package info (click to toggle)
minit 0.10-5
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 224 kB
  • ctags: 139
  • sloc: ansic: 1,655; makefile: 129; sh: 49; python: 33
file content (27 lines) | stat: -rw-r--r-- 624 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
#include <stdlib.h>

/* split buf into n strings that are separated by c.  return n as *len.
 * Allocate plus more slots and leave the first ofs of them alone. */
char **split(char *buf,int c,int *len,int plus,int ofs) {
  int n=1;
  char **v=0;
  char **w;
  /* step 1: count tokens */
  char *s;
  for (s=buf; *s; s++) if (*s==c) n++;
  /* step 2: allocate space for pointers */
  v=(char **)malloc((n+plus)*sizeof(char*));
  if (!v) return 0;
  w=v+ofs;
  *w++=buf;
  for (s=buf; ; s++) {
    while (*s && *s!=c) s++;
    if (*s==0) break;
    if (*s==c) {
      *s=0;
      *w++=s+1;
    }
  }
  *len=w-v;
  return v;
}