smbrep.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "headers.h"
  10. int
  11. smbmatch(char *name, Reprog *rep)
  12. {
  13. Resub sub;
  14. sub.sp = nil;
  15. sub.ep = nil;
  16. if (regexec(rep, name, &sub, 1) && sub.sp == name && *sub.ep == 0)
  17. return 1;
  18. return 0;
  19. }
  20. Reprog *
  21. smbmkrep(char *pattern)
  22. {
  23. Reprog *r;
  24. int l;
  25. char *tmp, *p, *q, *t;
  26. l = strlen(pattern);
  27. tmp = smbemalloc(l * 5 + 1);
  28. t = tmp;
  29. p = pattern;
  30. while (*p) {
  31. if (*p == '*') {
  32. if (p[1] == '.') {
  33. strcpy(t, "[^.]*");
  34. t += 5;
  35. p++;
  36. }
  37. else {
  38. *t++ = '.';
  39. *t++ = '*';
  40. p++;
  41. }
  42. }
  43. else if (*p == '?') {
  44. for (q = p + 1; *q && *q == '?'; q++)
  45. ;
  46. if (*q == 0 || *q == '.') {
  47. /* at most n copies */
  48. strcpy(t, "[^.]?");
  49. t += 5;
  50. p++;
  51. }
  52. else {
  53. /* exactly n copies */
  54. strcpy(t, "[^.]");
  55. t += 4;
  56. p++;
  57. }
  58. }
  59. else if (strchr(".+{}()|\\^$", *p) != 0) {
  60. /* regexp meta */
  61. *t++ = '\\';
  62. *t++ = *p++;
  63. }
  64. else
  65. *t++ = *p++;
  66. }
  67. *t = 0;
  68. smblogprintif(smbglobals.log.rep, "%s => %s\n", pattern, tmp);
  69. r = regcomp(tmp);
  70. free(tmp);
  71. return r;
  72. }