9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

getfields.c (501B)


      1 #include <lib9.h>
      2 
      3 int
      4 getfields(char *str, char **args, int max, int mflag, char *set)
      5 {
      6 	Rune r;
      7 	int nr, intok, narg;
      8 
      9 	if(max <= 0)
     10 		return 0;
     11 
     12 	narg = 0;
     13 	args[narg] = str;
     14 	if(!mflag)
     15 		narg++;
     16 	intok = 0;
     17 	for(;; str += nr) {
     18 		nr = chartorune(&r, str);
     19 		if(r == 0)
     20 			break;
     21 		if(utfrune(set, r)) {
     22 			if(narg >= max)
     23 				break;
     24 			*str = 0;
     25 			intok = 0;
     26 			args[narg] = str + nr;
     27 			if(!mflag)
     28 				narg++;
     29 		} else {
     30 			if(!intok && mflag)
     31 				narg++;
     32 			intok = 1;
     33 		}
     34 	}
     35 	return narg;
     36 }