File: match.c

package info (click to toggle)
afio 2.5.1.20160103+gitc8e4317-1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, buster, sid, stretch
  • size: 864 kB
  • ctags: 647
  • sloc: ansic: 4,903; sh: 139; makefile: 83; awk: 19
file content (266 lines) | stat: -rw-r--r-- 4,957 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include "patchlevel.h"

#ifdef HAVEFNMATCH

#include <fnmatch.h>

#else

/* A limited fnmatch replacement. Only accepts patterns
   in the form "ccccc" or "ccccc*". (These were the patterns accepted
   by older afio versions.)

*/

int fnmatch(const char *p, const char *s, int flag)
{
  if(p[strlen(p)-1]=='*')
      return strncmp(p,s,strlen(p)-1);
  else
      return strcmp(p,s);
}

#endif

#ifdef linux
#include <utime.h>
#endif

#include "afio.h"

STATIC int ignoreslash=1;

/*
 * Pathnames to (or not to) be processed.
 */
typedef struct pattern
{
  struct pattern *p_forw;	/* Forward chain */
  char *p_str;			/* String */
  int p_len;			/* Length of string */
  int p_ptype;			/* Pattern type */
                                /* types are: PATTYPE_MATCH, _NOMATCH, _EXT. */
  int p_noglob;                 /* influenced by used of -7 flag */
} Pattern;

STATIC Pattern *pattern;	/* Pathname matching patterns */

/*
 * nameadd()
 *
 * Add a name to the pattern list.
 */
STATIC void
nameadd (name, ptype)
     reg char *name;
     int ptype;
{
  reg Pattern *px;

  px = (Pattern *) memget (sizeof (Pattern));
  px->p_forw = pattern;
  px->p_str = strdup(name);
   if(px->p_str==NULL) fatal(name,"out of memory.");
  px->p_len = strlen (name);
  px->p_ptype = ptype;
  px->p_noglob = noglob;
  pattern = px;

  /* fprintf(stderr,"--%s added\n",name); */
}

/*
 * nameaddfile()
 *
 * Add a file of patterns to the pattern list.
 * Returns 0 on failure, 1 on success.
*/

STATIC int
nameaddfile(fname,ptype,parsewith0)
     char *fname;
     int ptype;
     int parsewith0;
{
 FILE *infile;
 char pat[PATHSIZE+1];
 int c,i;

 infile=fopen(fname,"r");
 if(infile==0) return 0;

 if(parsewith0)
 {
     i=0;
     while((c = fgetc(infile))!=EOF)
     {
         if(i > sizeof(pat))
         fatal(pat,"Path name too long");

         pat[i]=c;

         if(c == '\0')
         {
              if(i>0) nameadd(pat,ptype);
              i=0;
              continue;
         }
         i++;
     }
     /* handle case where the last entry in the file does not end in \0 */
     if(i>0 && i<sizeof(pat))
       {
	 pat[i]=0;
         nameadd(pat,ptype);
       }
 }
 else
 {
 while(fgets(pat,PATHSIZE,infile)!=NULL)
   {
    /* remove \n */
     pat[strlen(pat)-1]='\0';

     if(strlen(pat)==0) continue; /* skip empty lines in file */

     if(pat[strlen(pat)-1]==' ')
       {
        warn(pat,"warning: trailing space in this pattern.");
       }

     nameadd(pat,ptype);
   }
 }

 fclose(infile);
 return 1;
}


/*
 * namecmp()
 *
 * Compare a pathname with the pattern list. Returns 0 for
 * a match, -1 otherwise.
 *
 * This new version uses the GCC library function fnmatch()
 * to provide real pattern matching.
 *
 * Also, this implementation assures that if a file matches a pattern
 * in the `not to process' list, it is never processed.
 * In the old version, this depended on several implementation details
 * and the names in the `to process' list.
 *
 * Control files always match.
 */
STATIC int
namecmp (name, asb)
     reg char *name;
     Stat *asb;
{
  reg Pattern *px;
  int existpospat; /* there are `to process' patterns in the list */
  char *namedot,*p,*n;
  char namec[PATHSIZE];

  /* fprintf(stderr,"name:\"%s\" rdev:%d\n",name,
     asb->sb_rdev);
  */

  if(asb && ISCONTROL(asb)) return 0;

  strcpy(namec,name);
  namedot = strrchr (namec, '.');
  if (Zflag && asb && (asb->sb_mode & S_IFMT) == S_IFREG
      && asb->sb_rdev == 0
      && namedot && namedot[1] == 'z' && !namedot[2]
     )
      *namedot = '\0';
    else
      namedot = 0;

  n=namec;
  if(ignoreslash && (n[0]=='/')) n++;

  for(px=pattern; px; px=px->p_forw)
   if((px->p_ptype)==PATTYPE_NOMATCH)
     {
       p=px->p_str;
       if(ignoreslash && (p[0]=='/')) p++;

       if (px->p_noglob)
	 {
	   if(strcmp(p,n)==0) return -1;
	 }
       else
	 {
	   if(fnmatch(p,n,0)==0) return -1;
	 }
     }

  existpospat=0;

  for(px=pattern; px; px=px->p_forw)
   if((px->p_ptype)==PATTYPE_MATCH)
     {
       existpospat=1;

       p=px->p_str;
       if(ignoreslash && (p[0]=='/')) p++;

       if (px->p_noglob)
	 {
	   if(strcmp(p,n)==0) return 0;
	 }
       else
	 {
	   if(fnmatch(p,n,0)==0) return 0;
	 }
     }

  if(!existpospat) return 0; else return -1;

}


/*
 * namecmp_ext()
 *
 * Compare a pathname with the extensions list. Returns 0 for
 * a match, -1 otherwise.
 *
 */
STATIC int
namecmp_ext (name)
     reg char *name;
{
  reg Pattern *px;
  char *p,*n;

  n=name;
  if(ignoreslash && (n[0]=='/')) n++;

  for(px=pattern; px; px=px->p_forw)
   if((px->p_ptype)==PATTYPE_EXT)
     {
       p=px->p_str;
       if(ignoreslash && (p[0]=='/')) p++;

       if(fnmatch(p,n,0)==0) return 0;
     }

  return -1;
}