1 /***************************************************************************/
2 /* */
3 /* Fast Webpage Exchanger - an FTP client for updating webpages */
4 /* Copyright (C) 1999-2000 Yuuki NINOMIYA <gm@debian.or.jp> */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation; either version 2, or (at your option) */
9 /* any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with this program; if not, write to the */
18 /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
19 /* Boston, MA 02111-1307, USA. */
20 /* */
21 /***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifdef HAVE_GNUREGEX_H
31 # include <gnuregex.h>
32 #else
33 # include <regex.h>
34 #endif
35
36 #include "intl.h"
37 #include "strlib.h"
38 #include "variable.h"
39 #include "proto.h"
40
41
42 /* --------------------------------------------------
43 NAME n_atol
44 FUNCTION convert string to long(the maximum bytes is n)
45 INPUT str ... converted string
46 n ... the maximum length of string
47 OUTPUT result value
48 -------------------------------------------------- */
n_atol(char * str,int n)49 long n_atol(char *str,int n)
50 {
51 char *temp;
52 long result;
53
54 temp=str_malloc(n+1);
55 strncpy(temp,str,n);
56 temp[n]='\0';
57
58 result=atol(temp);
59
60 free(temp);
61 return(result);
62 }
63
64
65 /* --------------------------------------------------
66 NAME cmp_file_with_wildcard
67 FUNCTION compare raw file name with file name with wild card
68 INPUT read_name ... raw file name
69 wild_name ... file name with wild card
70 OUTPUT return 0 if read_name is equal to wild_name otherwise return -1
71 -------------------------------------------------- */
cmp_file_with_wildcard(char * real_name,char * wild_name)72 int cmp_file_with_wildcard(char *real_name,char *wild_name)
73 {
74 char *temp;
75 regex_t re;
76 int res;
77
78 temp=cnvregexp(wild_name);
79
80 if(regcomp(&re,temp,REG_EXTENDED)!=0){
81 fprintf(stderr,_("Invalid wildcard `%s'.\n"),wild_name);
82 exit(1);
83 }
84 res=regexec(&re,real_name,0,NULL,0);
85 regfree(&re);
86 free(temp);
87 if(res==0){
88 return(0);
89 }else{
90 return(-1);
91 }
92 }
93
94
95 /* --------------------------------------------------
96 NAME cnvregexp
97 FUNCTION convert shell wildcards to regular expressions
98 INPUT str ... shell wildcards
99 OUTPUT pointer to converted to regular expressions
100 -------------------------------------------------- */
cnvregexp(char * str)101 char *cnvregexp(char *str)
102 {
103 char *cp, *pattern;
104 int i;
105 int flag = 0;
106
107 pattern = str_malloc(1 + strlen(str) * 2 + 3);
108 i = 0;
109 pattern[i++] = '^';
110 for (cp = str; *cp; cp++) {
111 if (flag) {
112 if (*cp == ']') flag = 0;
113 pattern[i++] = *cp;
114 continue;
115 }
116 switch (*cp) {
117 case '\\':
118 if (!*(cp + 1)) break;
119 pattern[i++] = *(cp++);
120 pattern[i++] = *cp;
121 break;
122 case '?':
123 pattern[i++] = '.';
124 break;
125 case '*':
126 pattern[i++] = '.';
127 pattern[i++] = '*';
128 break;
129 case '[':
130 flag = 1;
131 pattern[i++] = *cp;
132 break;
133 case '^':
134 case '$':
135 case '.':
136 pattern[i++] = '\\';
137 default:
138 pattern[i++] = *cp;
139 break;
140 }
141 }
142 pattern[i++] = '$';
143 pattern[i++] = '\0';
144 pattern = str_realloc(pattern, i);
145
146 return(pattern);
147 }
148
149
150
151