1 | /* |
---|
2 | * $Id: shell_regexp.c,v 1.4 1999-01-22 23:09:04 ghudson Exp $ |
---|
3 | * |
---|
4 | * This program is part of a package including delete, undelete, |
---|
5 | * lsdel, expunge and purge. The software suite is meant as a |
---|
6 | * replacement for rm which allows for file recovery. |
---|
7 | * |
---|
8 | * Copyright (c) 1989 by the Massachusetts Institute of Technology. |
---|
9 | * For copying and distribution information, see the file "mit-copying.h." |
---|
10 | */ |
---|
11 | |
---|
12 | #if (!defined(lint) && !defined(SABER)) |
---|
13 | static char rcsid_shell_regexp_c[] = "$Id: shell_regexp.c,v 1.4 1999-01-22 23:09:04 ghudson Exp $"; |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <com_err.h> |
---|
17 | #include "shell_regexp.h" |
---|
18 | #include "delete_errs.h" |
---|
19 | #include "errors.h" |
---|
20 | #include "mit-copying.h" |
---|
21 | |
---|
22 | static int real_cmp(); |
---|
23 | |
---|
24 | /* |
---|
25 | * This is a simple pattern matcher that takes a pattern string and |
---|
26 | * another string (theoretically a filename) and checks if the second |
---|
27 | * string matches the pattern string using shell special characters |
---|
28 | * (i.e. it recognizes \, ?, *, [, ]). It also special cases dot |
---|
29 | * files (i.e. * doesn't match files that start with periods, and |
---|
30 | * neither will ?*, and neither will [.]*). |
---|
31 | */ |
---|
32 | |
---|
33 | int reg_cmp(pattern, filename) |
---|
34 | char *pattern, *filename; |
---|
35 | { |
---|
36 | /* First, dot file special cases */ |
---|
37 | if ((*filename == '.') && (*pattern != '.')) |
---|
38 | return REGEXP_NO_MATCH; |
---|
39 | |
---|
40 | return real_cmp(pattern, filename); |
---|
41 | } |
---|
42 | |
---|
43 | static int real_cmp(pattern, filename) |
---|
44 | char *pattern, *filename; |
---|
45 | { |
---|
46 | if (*pattern == '\0') { |
---|
47 | if (*filename == '\0') |
---|
48 | return REGEXP_MATCH; |
---|
49 | else |
---|
50 | return REGEXP_NO_MATCH; |
---|
51 | } |
---|
52 | |
---|
53 | if (*pattern == '*') { |
---|
54 | int retval; |
---|
55 | char *ptr; |
---|
56 | |
---|
57 | if (*(pattern + 1) == '\0') |
---|
58 | /* asterisk by itself matches anything */ |
---|
59 | return REGEXP_MATCH; |
---|
60 | for (ptr = filename; *ptr; ptr++) |
---|
61 | if ((retval = real_cmp(pattern + 1, ptr)) != REGEXP_NO_MATCH) |
---|
62 | return retval; |
---|
63 | return REGEXP_NO_MATCH; |
---|
64 | } |
---|
65 | |
---|
66 | if (*filename == '\0') |
---|
67 | return REGEXP_NO_MATCH; |
---|
68 | |
---|
69 | if (*pattern == '?') |
---|
70 | return real_cmp(pattern + 1, filename + 1); |
---|
71 | |
---|
72 | if (*pattern == '\\') { |
---|
73 | if (*(pattern + 1) == '\0') { |
---|
74 | set_error(REGEXP_MISSING_QUOTED_CHAR); |
---|
75 | return -1; |
---|
76 | } |
---|
77 | if (*(pattern + 1) == *filename) |
---|
78 | return real_cmp(pattern + 2, filename + 1); |
---|
79 | else |
---|
80 | return REGEXP_NO_MATCH; |
---|
81 | } |
---|
82 | |
---|
83 | if (*pattern == '[') { |
---|
84 | char *ptr, *end_ptr; |
---|
85 | |
---|
86 | for (end_ptr = pattern + 1; (*end_ptr != '\0') && (*end_ptr != ']'); |
---|
87 | end_ptr++) ; |
---|
88 | if (*end_ptr == '\0') { |
---|
89 | set_error(REGEXP_MISSING_BRACE); |
---|
90 | return -1; |
---|
91 | } |
---|
92 | if (end_ptr == pattern + 1) { |
---|
93 | set_error(REGEXP_EMPTY_BRACES); |
---|
94 | return -1; |
---|
95 | } |
---|
96 | for (ptr = pattern + 1; ptr < end_ptr; ptr++) { |
---|
97 | if ((*(ptr + 1) == '-') && (*(ptr + 2) != ']')) { |
---|
98 | if ((*ptr <= *filename) && (*(ptr + 2) >= *filename)) |
---|
99 | return real_cmp(end_ptr + 1, filename + 1); |
---|
100 | else { |
---|
101 | ptr += 2; |
---|
102 | continue; |
---|
103 | } |
---|
104 | } |
---|
105 | if (*ptr == *filename) |
---|
106 | return real_cmp(end_ptr + 1, filename + 1); |
---|
107 | } |
---|
108 | |
---|
109 | return REGEXP_NO_MATCH; |
---|
110 | } |
---|
111 | |
---|
112 | if (*pattern == *filename) |
---|
113 | return real_cmp(pattern + 1, filename + 1); |
---|
114 | else |
---|
115 | return REGEXP_NO_MATCH; |
---|
116 | } |
---|