match.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "mk.h"
  10. int
  11. match(char *name, char *template, char *stem)
  12. {
  13. Rune r;
  14. int n;
  15. while(*name && *template){
  16. n = chartorune(&r, template);
  17. if (PERCENT(r))
  18. break;
  19. while (n--)
  20. if(*name++ != *template++)
  21. return 0;
  22. }
  23. if(!PERCENT(*template))
  24. return 0;
  25. n = strlen(name)-strlen(template+1);
  26. if (n < 0)
  27. return 0;
  28. if (strcmp(template+1, name+n))
  29. return 0;
  30. strncpy(stem, name, n);
  31. stem[n] = 0;
  32. if(*template == '&')
  33. return !charin(stem, "./");
  34. return 1;
  35. }
  36. void
  37. subst(char *stem, char *template, char *dest, int dlen)
  38. {
  39. Rune r;
  40. char *s, *e;
  41. int n;
  42. e = dest+dlen-1;
  43. while(*template){
  44. n = chartorune(&r, template);
  45. if (PERCENT(r)) {
  46. template += n;
  47. for (s = stem; *s; s++)
  48. if(dest < e)
  49. *dest++ = *s;
  50. } else
  51. while (n--){
  52. if(dest < e)
  53. *dest++ = *template;
  54. template++;
  55. }
  56. }
  57. *dest = 0;
  58. }