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

Revision 4505, 3.1 KB checked in by jik, 34 years ago (diff)
Shorten mit-copyright.h to mit-copying.h so it'll work on systems with 14-char filenames.
RevLine 
[2172]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.
[4505]10 * For copying and distribution information, see the file "mit-copying.h."
[2172]11 */
12
13#if (!defined(lint) && !defined(SABER))
[4505]14     static char rcsid_shell_regexp_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/shell_regexp.c,v 1.3 1991-02-28 18:43:43 jik Exp $";
[2172]15#endif
16
17#include <com_err.h>
18#include "shell_regexp.h"
19#include "delete_errs.h"
20#include "errors.h"
[4505]21#include "mit-copying.h"
[2172]22
23static int real_cmp();
24
25/*
26 * This is a simple pattern matcher that takes a pattern string and
27 * another string (theoretically a filename) and checks if the second
28 * string matches the pattern string using shell special characters
29 * (i.e. it recognizes \, ?, *, [, ]).  It also special cases dot
30 * files (i.e. * doesn't match files that start with periods, and
31 * neither will ?*, and neither will [.]*).
32 */
33
34int reg_cmp(pattern, filename)
35char *pattern, *filename;
36{
37     /* First, dot file special cases */
38     if ((*filename == '.') && (*pattern != '.'))
39          return REGEXP_NO_MATCH;
40
41     return real_cmp(pattern, filename);
42}
43
44static int real_cmp(pattern, filename)
45char *pattern, *filename;
46{
47     if (*pattern == '\0') {
48          if (*filename == '\0')
49               return REGEXP_MATCH;
50          else
51               return REGEXP_NO_MATCH;
52     }
53     
54     if (*pattern == '*') {
55          int retval;
56          char *ptr;
57         
58          if (*(pattern + 1) == '\0')
59               /* asterisk by itself matches anything */
60               return REGEXP_MATCH;
61          for (ptr = filename; *ptr; ptr++)
62               if ((retval = real_cmp(pattern + 1, ptr)) != REGEXP_NO_MATCH)
63                    return retval;
64          return REGEXP_NO_MATCH;
65     }
66
67     if (*filename == '\0')
68          return REGEXP_NO_MATCH;
69     
70     if (*pattern == '?')
71          return real_cmp(pattern + 1, filename + 1);
72
73     if (*pattern == '\\') {
74          if (*(pattern + 1) == '\0') {
75               set_error(REGEXP_MISSING_QUOTED_CHAR);
76               return -1;
77          }
78          if (*(pattern + 1) == *filename)
79               return real_cmp(pattern + 2, filename + 1);
80          else
81               return REGEXP_NO_MATCH;
82     }
83
84     if (*pattern == '[') {
85          char *ptr, *end_ptr;
86
87          for (end_ptr = pattern + 1; (*end_ptr != '\0') && (*end_ptr != ']');
88               end_ptr++) ;
89          if (*end_ptr == '\0') {
90               set_error(REGEXP_MISSING_BRACE);
91               return -1;
92          }
93          if (end_ptr == pattern + 1) {
94               set_error(REGEXP_EMPTY_BRACES);
95               return -1;
96          }
97          for (ptr = pattern + 1; ptr < end_ptr; ptr++) {
98               if ((*(ptr + 1) == '-') && (*(ptr + 2) != ']')) {
99                    if ((*ptr <= *filename) && (*(ptr + 2) >= *filename))
100                         return real_cmp(end_ptr + 1, filename + 1);
101                    else {
102                         ptr += 2;
103                         continue;
104                    }
105               }
106               if (*ptr == *filename)
107                    return real_cmp(end_ptr + 1, filename + 1);
108          }
109
110          return REGEXP_NO_MATCH;
111     }
112                   
113     if (*pattern == *filename)
114          return real_cmp(pattern + 1, filename + 1);
115     else
116          return REGEXP_NO_MATCH;
117}
Note: See TracBrowser for help on using the repository browser.