4
0

exclude.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* exclude.c -- exclude file names
  2. Copyright 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING.
  13. If not, write to the Free Software Foundation,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by Paul Eggert <[email protected]> */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <errno.h>
  20. #ifndef errno
  21. extern int errno;
  22. #endif
  23. #include <exclude.h>
  24. #include <fnmatch.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. void *xmalloc PARAMS ((size_t));
  28. void *xrealloc PARAMS ((void *, size_t));
  29. /* Keep track of excluded file name patterns. */
  30. struct exclude
  31. {
  32. char const **exclude;
  33. int exclude_alloc;
  34. int exclude_count;
  35. };
  36. struct exclude *
  37. new_exclude (void)
  38. {
  39. struct exclude *ex = (struct exclude *) xmalloc (sizeof (struct exclude));
  40. ex->exclude_count = 0;
  41. ex->exclude_alloc = 64;
  42. ex->exclude = (char const **) xmalloc (ex->exclude_alloc * sizeof (char *));
  43. return ex;
  44. }
  45. int
  46. excluded_filename (struct exclude const *ex, char const *f)
  47. {
  48. char const * const *exclude = ex->exclude;
  49. int exclude_count = ex->exclude_count;
  50. int i;
  51. for (i = 0; i < exclude_count; i++)
  52. if (fnmatch (exclude[i], f, 0) == 0)
  53. return 1;
  54. return 0;
  55. }
  56. void
  57. add_exclude (struct exclude *ex, char const *pattern)
  58. {
  59. if (ex->exclude_alloc <= ex->exclude_count)
  60. ex->exclude = (char const **) xrealloc (ex->exclude,
  61. ((ex->exclude_alloc *= 2)
  62. * sizeof (char *)));
  63. ex->exclude[ex->exclude_count++] = pattern;
  64. }
  65. int
  66. add_exclude_file (struct exclude *ex, char const *filename, char line_end)
  67. {
  68. int use_stdin = filename[0] == '-' && !filename[1];
  69. FILE *in;
  70. char *buf;
  71. char *p;
  72. char const *pattern;
  73. char const *lim;
  74. size_t buf_alloc = 1024;
  75. size_t buf_count = 0;
  76. int c;
  77. int e = 0;
  78. if (use_stdin)
  79. in = stdin;
  80. else if (! (in = fopen (filename, "r")))
  81. return -1;
  82. buf = xmalloc (buf_alloc);
  83. while ((c = getc (in)) != EOF)
  84. {
  85. buf[buf_count++] = c;
  86. if (buf_count == buf_alloc)
  87. buf = xrealloc (buf, buf_alloc *= 2);
  88. }
  89. buf = xrealloc (buf, buf_count + 1);
  90. if (ferror (in))
  91. e = errno;
  92. if (!use_stdin && fclose (in) != 0)
  93. e = errno;
  94. for (pattern = p = buf, lim = buf + buf_count; p <= lim; p++)
  95. if (p < lim ? *p == line_end : buf < p && p[-1])
  96. {
  97. *p = '\0';
  98. add_exclude (ex, pattern);
  99. pattern = p + 1;
  100. }
  101. errno = e;
  102. return e ? -1 : 0;
  103. }