File: match.c

package info (click to toggle)
ytree 1.94-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 836 kB
  • ctags: 1,510
  • sloc: ansic: 15,271; makefile: 208
file content (209 lines) | stat: -rw-r--r-- 3,500 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *
 * $Header: /usr/local/cvsroot/utils/ytree/match.c,v 1.18 2007/04/01 13:33:36 werner Exp $
 *
 * Behandlung reg. Ausdruecke fuer Dateinamen
 *
 ***************************************************************************/


#include "ytree.h"

#if defined( sun ) || defined( linux ) || defined( __NeXT__ ) || defined( OSF1 ) ||  defined( __OpenBSD__ ) || defined(__NetBSD__) || defined( __FreeBSD__ ) || defined( __GNU__ )
#define HAS_REGEX
#endif

#ifdef linux
/* To avoid warning about re_comp / re_exec */
/* Maybe this would work on other architectures too */
#define _REGEX_RE_COMP
#endif

#if defined( hpux ) || defined ( __APPLE__ )
#define HAS_REGCOMP
#endif


#ifdef HAS_REGEX
#ifdef sun
#define INIT   		register char *sp = "";
#define GETC() 		(*sp++)
#define PEEKC()     	(*sp)
#define UNGETC(c)   	(--sp)
#define RETURN(c)   	return;
#define ERROR(c)    	
#include <regexp.h>
#else
#include <regex.h>
#endif /* sun */
#else
extern char *regcmp();
extern char *regex();
static char *file_spec_cmp = NULL;

#endif /* HAS_REGEX */

#if defined( HAS_REGCOMP )
#include <regex.h>
static regex_t	re;
static BOOL	re_flag = FALSE;
#endif





int SetMatchSpec(char *new_spec)
{
  char *result;
  char *buffer;
  char *b_ptr;
  BOOL meta_flag = FALSE;

  if( ( buffer = (char *)malloc( strlen( new_spec ) * 2 + 4 ) ) == NULL )
  {
    ERROR_MSG( "Malloc failed*ABORT" );
    exit( 1 );
  }

  b_ptr = buffer;
  *b_ptr++ = '^';

  for(; *new_spec; new_spec++)
  {
    if( meta_flag ) 
    {
      *b_ptr++ = *new_spec;
      meta_flag = FALSE;
    }
    else if( *new_spec == '\\' ) meta_flag = TRUE;
    else if( *new_spec == '?' ) *b_ptr++ = '.';
    else if( *new_spec == '.' )
    {
      *b_ptr++ = '\\';
      *b_ptr++ = '.';
    }
    else if( *new_spec == '*' )
    {
      *b_ptr++ = '.';
      *b_ptr++ = '*';
    }
    else
      *b_ptr++ = *new_spec;
  }

  *b_ptr++ = '$';
  *b_ptr = '\0';

#if defined ( HAS_REGEX )

  if( ( result = (char *) re_comp( buffer ) ) )
  {
    free( buffer );
    return( 1 );
  }

#else

#if defined( HAS_REGCOMP )

  if(re_flag) 
  {
    regfree(&re);
    re_flag = FALSE;
  }

  if( regcomp(&re, buffer, REG_NOSUB) )
  {
    free( buffer );
    return( 1 );
  }
  free( buffer );
  re_flag = TRUE;

#else

#if (!(defined(WIN32) || defined(__DJGPP__)))


  if( ( result = regcmp( buffer, (char *) 0 ) ) == NULL )
  {
    free( buffer );
    return( 1 );
  }

  if( file_spec_cmp ) free( file_spec_cmp );
  free( buffer );

  file_spec_cmp = result;

#else

  /* WIN32 */

  /* z.Z. nicht unterstuetzt */

  free( buffer );
  result = 0;

#endif /* WIN32 */
#endif /* HAS_REGCOMP */
#endif /* HAS_REGEX */

  return( 0 );
}



BOOL Match(char *file_name)
{
#if defined ( HAS_REGEX )
  
  if( re_exec( file_name ) ) return( TRUE );
  else                       return( FALSE );

#else
#if defined( HAS_REGCOMP )

  if(re_flag == FALSE)
    return( TRUE );

  if( ( regexec(&re, file_name, (size_t) 0, NULL, 0 ) ) == 0 )
  {
    return( TRUE );
  }
  else 
  {
    return( FALSE );
  }

#else

#if (!(defined(WIN32) || defined(__DJGPP__)))


  char match_part[PATH_LENGTH + 1];

  if( !file_spec_cmp )
    return( TRUE );

  if( regex( file_spec_cmp, file_name, match_part ) == NULL )
    return( FALSE );

  return( TRUE );

#else

  /* WIN32 */

  /* z.Z. nicht unterstuetzt */

  return( TRUE );

#endif /* WIN32 */
#endif /* HAS_REGCOMP */
#endif /* HAS_REGEX */
}