9base

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

match.c (783B)


      1 #include	"mk.h"
      2 
      3 int
      4 match(char *name, char *template, char *stem, Shell *sh)
      5 {
      6 	Rune r;
      7 	int n;
      8 
      9 	while(*name && *template){
     10 		n = chartorune(&r, template);
     11 		if (PERCENT(r))
     12 			break;
     13 		while (n--)
     14 			if(*name++ != *template++)
     15 				return 0;
     16 	}
     17 	if(!PERCENT(*template))
     18 		return 0;
     19 	n = strlen(name)-strlen(template+1);
     20 	if (n < 0)
     21 		return 0;
     22 	if (strcmp(template+1, name+n))
     23 		return 0;
     24 	strncpy(stem, name, n);
     25 	stem[n] = 0;
     26 	if(*template == '&')
     27 		return !sh->charin(stem, "./");
     28 	return 1;
     29 }
     30 
     31 void
     32 subst(char *stem, char *template, char *dest)
     33 {
     34 	Rune r;
     35 	char *s;
     36 	int n;
     37 
     38 	while(*template){
     39 		n = chartorune(&r, template);
     40 		if (PERCENT(r)) {
     41 			template += n;
     42 			for (s = stem; *s; s++)
     43 				*dest++ = *s;
     44 		} else
     45 			while (n--)
     46 				*dest++ = *template++;
     47 	}
     48 	*dest = 0;
     49 }