source: trunk/athena/bin/delete/shell_regexp.c @ 2172

Revision 2172, 3.1 KB checked in by jik, 35 years ago (diff)
Initial revision
Line 
1/*
2 * $Source: /afs/dev.mit.edu/source/repository/athena/bin/delete/shell_regexp.c,v $
3 * $Author: jik $
4 *
5 * This program is part of a package including delete, undelete,
6 * lsdel, expunge and purge.  The software suite is meant as a
7 * replacement for rm which allows for file recovery.
8 *
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file "mit-copyright.h."
11 */
12
13#if (!defined(lint) && !defined(SABER))
14     static char rcsid_shell_regexp_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/shell_regexp.c,v 1.1 1989-10-23 13:31:15 jik Exp $";
15#endif
16
17#include <com_err.h>
18#include "shell_regexp.h"
19#include "delete_errs.h"
20#include "errors.h"
21
22static 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
33int reg_cmp(pattern, filename)
34char *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
43static int real_cmp(pattern, filename)
44char *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}
Note: See TracBrowser for help on using the repository browser.