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

Revision 24908, 2.9 KB checked in by ghudson, 14 years ago (diff)
In delete: * Patches from Jonathan Kamens: - The "-f" flag to delete should suppress nonexistent file errors but not other errors. - When the "-v" flag is specified to expunge, the correct totals should be reported. Previously, the totals were incorrect. - Code cleanup.
RevLine 
[2172]1/*
[12350]2 * $Id: shell_regexp.c,v 1.4 1999-01-22 23:09:04 ghudson Exp $
[2172]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.
[4505]9 * For copying and distribution information, see the file "mit-copying.h."
[2172]10 */
11
12#include <com_err.h>
13#include "shell_regexp.h"
14#include "delete_errs.h"
15#include "errors.h"
[4505]16#include "mit-copying.h"
[24908]17#include "util.h"
[2172]18
19static int real_cmp();
20
21/*
22 * This is a simple pattern matcher that takes a pattern string and
23 * another string (theoretically a filename) and checks if the second
24 * string matches the pattern string using shell special characters
25 * (i.e. it recognizes \, ?, *, [, ]).  It also special cases dot
26 * files (i.e. * doesn't match files that start with periods, and
27 * neither will ?*, and neither will [.]*).
28 */
29
30int reg_cmp(pattern, filename)
31char *pattern, *filename;
32{
33     /* First, dot file special cases */
34     if ((*filename == '.') && (*pattern != '.'))
35          return REGEXP_NO_MATCH;
36
37     return real_cmp(pattern, filename);
38}
39
40static int real_cmp(pattern, filename)
41char *pattern, *filename;
42{
43     if (*pattern == '\0') {
44          if (*filename == '\0')
45               return REGEXP_MATCH;
46          else
47               return REGEXP_NO_MATCH;
48     }
49     
50     if (*pattern == '*') {
51          int retval;
52          char *ptr;
53         
54          if (*(pattern + 1) == '\0')
55               /* asterisk by itself matches anything */
56               return REGEXP_MATCH;
57          for (ptr = filename; *ptr; ptr++)
58               if ((retval = real_cmp(pattern + 1, ptr)) != REGEXP_NO_MATCH)
59                    return retval;
60          return REGEXP_NO_MATCH;
61     }
62
63     if (*filename == '\0')
64          return REGEXP_NO_MATCH;
65     
66     if (*pattern == '?')
67          return real_cmp(pattern + 1, filename + 1);
68
69     if (*pattern == '\\') {
70          if (*(pattern + 1) == '\0') {
71               set_error(REGEXP_MISSING_QUOTED_CHAR);
72               return -1;
73          }
74          if (*(pattern + 1) == *filename)
75               return real_cmp(pattern + 2, filename + 1);
76          else
77               return REGEXP_NO_MATCH;
78     }
79
80     if (*pattern == '[') {
81          char *ptr, *end_ptr;
82
83          for (end_ptr = pattern + 1; (*end_ptr != '\0') && (*end_ptr != ']');
84               end_ptr++) ;
85          if (*end_ptr == '\0') {
86               set_error(REGEXP_MISSING_BRACE);
87               return -1;
88          }
89          if (end_ptr == pattern + 1) {
90               set_error(REGEXP_EMPTY_BRACES);
91               return -1;
92          }
93          for (ptr = pattern + 1; ptr < end_ptr; ptr++) {
94               if ((*(ptr + 1) == '-') && (*(ptr + 2) != ']')) {
95                    if ((*ptr <= *filename) && (*(ptr + 2) >= *filename))
96                         return real_cmp(end_ptr + 1, filename + 1);
97                    else {
98                         ptr += 2;
99                         continue;
100                    }
101               }
102               if (*ptr == *filename)
103                    return real_cmp(end_ptr + 1, filename + 1);
104          }
105
106          return REGEXP_NO_MATCH;
107     }
108                   
109     if (*pattern == *filename)
110          return real_cmp(pattern + 1, filename + 1);
111     else
112          return REGEXP_NO_MATCH;
113}
Note: See TracBrowser for help on using the repository browser.