Initial revision
[rsync/rsync.git] / exclude.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*
21   a lot of this stuff was derived from GNU tar
22   */
23
24 #include "rsync.h"
25
26 extern int verbose;
27
28 static char **exclude_list = NULL;
29
30 static int is_regex(char *str)
31 {
32   return strchr(str, '*') || strchr(str, '[') || strchr(str, '?');
33 }
34
35
36 static int check_one_exclude(char *name,char *pattern)
37 {
38   char *str;
39   char *p;
40
41   if (!strchr(pattern,'/') && (p=strrchr(name,'/')))
42     name = p+1;
43
44   if (!name[0]) return 0;
45
46   if (is_regex(pattern)) {
47     if (fnmatch(pattern, name, 0) == 0)
48       return 1;
49   } else {
50     int l1 = strlen(name);
51     int l2 = strlen(pattern);
52     if (l2 <= l1 && 
53         strcmp(name+(l1-l2),pattern) == 0 &&
54         (l1==l2 || name[l1-(l2+1)] == '/'))
55       return 1;
56   }
57
58   return 0;
59 }
60
61
62 int check_exclude(char *name,char **local_exclude_list)
63 {
64   int n;
65
66   if (exclude_list) {
67     for (n=0; exclude_list[n]; n++)
68       if (check_one_exclude(name,exclude_list[n]))
69         return 1;
70   }
71
72   if (local_exclude_list) {
73     for (n=0; local_exclude_list[n]; n++)
74       if (check_one_exclude(name,local_exclude_list[n]))
75         return 1;      
76   }
77
78   return 0;
79 }
80
81
82 void add_exclude_list(char *pattern,char ***list)
83 {
84   int len=0;
85   if (list && *list)
86     for (; (*list)[len]; len++) ;
87
88   if (strcmp(pattern,"!") == 0) {
89     if (verbose > 2)
90       fprintf(stderr,"clearing exclude list\n");
91     while ((len)--) 
92       free((*list)[len]);
93     free((*list));
94     *list = NULL;
95     return;
96   }
97
98   if (!*list) {
99     *list = (char **)malloc(sizeof(char *)*2);
100   } else {
101     *list = (char **)realloc(*list,sizeof(char *)*(len+2));
102   }
103
104   if (!*list || !((*list)[len] = strdup(pattern)))
105     out_of_memory("add_exclude");
106
107   if (verbose > 2)
108     fprintf(stderr,"add_exclude(%s)\n",pattern);
109   
110   (*list)[len+1] = NULL;
111 }
112
113 void add_exclude(char *pattern)
114 {
115   add_exclude_list(pattern,&exclude_list);
116 }
117
118 char **make_exclude_list(char *fname,char **list1,int fatal)
119 {
120   char **list=list1;
121   FILE *f = fopen(fname,"r");
122   char line[MAXPATHLEN];
123   if (!f) {
124     if (fatal) {
125       fprintf(stderr,"%s : %s\n",fname,strerror(errno));
126       exit(1);
127     }
128     return list;
129   }
130
131   while (fgets(line,MAXPATHLEN,f)) {
132     int l = strlen(line);
133     if (l && line[l-1] == '\n') l--;
134     line[l] = 0;
135     if (line[0]) add_exclude_list(line,&list);
136   }
137   fclose(f);
138   return list;
139 }
140
141
142 void add_exclude_file(char *fname,int fatal)
143 {
144   exclude_list = make_exclude_list(fname,exclude_list,fatal);
145 }
146
147
148 void send_exclude_list(int f)
149 {
150   int i;
151   if (exclude_list) 
152     for (i=0;exclude_list[i];i++) {
153       int l = strlen(exclude_list[i]);
154       if (l == 0) continue;
155       write_int(f,l);
156       write_buf(f,exclude_list[i],l);
157     }    
158   write_int(f,0);
159 }
160
161
162 void recv_exclude_list(int f)
163 {
164   char line[MAXPATHLEN];
165   int l;
166   while ((l=read_int(f))) {
167     read_buf(f,line,l);
168     line[l] = 0;
169     add_exclude(line);
170   }
171 }
172
173
174 static char *cvs_ignore_list[] = {
175   "RCS","SCCS","CVS","CVS.adm","RCSLOG","cvslog.*",
176   "tags","TAGS",".make.state",".nse_depinfo",
177   "*~", "#*", ".#*", ",*", "*.old", "*.bak", "*.BAK", "*.orig",
178   "*.rej", ".del-*", "*.a", "*.o", "*.obj", "*.so", "*.Z", "*.elc", "*.ln",
179   "core",NULL};
180
181
182
183 void add_cvs_excludes(void)
184 {
185   char fname[MAXPATHLEN];
186   char *p;
187   int i;
188   
189   for (i=0; cvs_ignore_list[i]; i++)
190     add_exclude(cvs_ignore_list[i]);
191
192   if ((p=getenv("HOME"))) {
193     sprintf(fname,"%s/.cvsignore",p);
194     add_exclude_file(fname,0);
195   }
196
197   if ((p=getenv("CVSIGNORE"))) {
198     char *tok;
199     for (tok=strtok(p," "); tok; tok=strtok(NULL," "))
200       add_exclude(tok);
201   }
202 }