File: dash-search.c

package info (click to toggle)
cruft 0.9.6-0.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 648 kB
  • ctags: 34
  • sloc: ansic: 642; sh: 179; makefile: 66; perl: 64
file content (124 lines) | stat: -rw-r--r-- 2,954 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>

#define FALSE 0
#define TRUE  1

/* 0 on no match, non-zero on match */
int shellexp( char* string, char* pattern ) {
    /*  printf( "...matching( \"%s\", \"%s\" )\n", string, pattern ); */
    
    switch( pattern[0] ) {
    case '\0':
	return string[0] == '\0';
    case '?':
	switch( string[0] ) {
	case '\0':
	case '/':
	    return FALSE;
	default:
	    return shellexp( string+1, pattern+1 );
	}
    case '/':
	if ( pattern[1] == '*' && pattern[2] == '*' ) {
	    char* pch = string;
	    if ( *pch != '/' ) return FALSE;                
	    if ( pattern[3] == '\0' ) return TRUE;
	    if ( pattern[3] != '/' ) {
		fprintf( stderr, "Bad expression.\n" );
		return -1;
	    }
	    while ( *pch != '\0' ) {
		if ( *pch == '/' ) {
		    if ( shellexp( pch, pattern + 3 ) ) return TRUE;
		}
		pch++;
	    }
	    return FALSE;
	}
	return string[0] == '/' && shellexp( string+1, pattern+1 );
    case '*':
	if ( string[0] == '/' ) return shellexp( string, pattern+1 );
	return shellexp( string, pattern+1 ) || 
	    (string[0] != '\0' ? shellexp( string + 1, pattern ) : FALSE );
    case '[': 
	if ( string[0] == '\0' ) return FALSE;
	{
	    int not = FALSE;
	    int okay = FALSE;
	    pattern++;
	    if ( pattern[0] == '!' || pattern[0] == '^' ) {
		not = TRUE;
		pattern++;
	    }

	    if ( pattern[0] == ']' ) {
		if ( string[0] == ']' ) { okay = TRUE; }
		pattern++;
	    }
	    
	    while( pattern[0] != ']' && pattern[0] != '\0' ) {
		if ( pattern[0] == string[0] ) {
		    okay = TRUE;
		} else if ( pattern[1] == '-' && pattern[2] != ']' ) {
		    if ( pattern[0] <= string[0] && string[0] <= pattern[2] ) {
			okay = TRUE;
		    }
		    pattern += 2;
		}
		pattern++;
	    }
	    
	    if ( pattern[0] == '\0' ) {
		fprintf( stderr, "Bad shell expression\n" );
		return -1;
	    }
	    
	    return (not ? !okay : okay) && shellexp( string + 1, pattern + 1 );
	}
    default:
	return pattern[0] == string[0] && shellexp( string + 1, pattern + 1 );
    }
}



int main(int argc, char **argv) {
    char filename[1000];
    char buffer[1000];
    int i;
    DIR* dpkg_info;
    struct dirent* pde;

    dpkg_info = opendir("/usr/lib/cruft/filters/" );
    if ( dpkg_info == NULL ) {
	perror( "/usr/lib/cruft/filters" );
	exit(EXIT_FAILURE);
    }

    chdir("/usr/lib/cruft/filters");
    while( (pde = readdir( dpkg_info )) ) {
	FILE* f;
	f = fopen( pde->d_name, "r" );
	if ( !f ) continue;
	while( fgets( buffer, 1000, f ) ) {
	    if ( buffer[strlen(buffer)-1] == '\n' )
		buffer[strlen(buffer)-1] = '\0';
	    
	    for ( i = 1; i < argc; i++ ) {
		/* printf( "shellexp( \"%s\", \"%s\" )\n", argv[i], buffer );*/
		if ( shellexp( argv[i], buffer ) ) {
		    strcpy( filename, pde->d_name );
		    printf( "%s: %s\n", filename, argv[i] ); 
		}
	    }
	}
	fclose(f);
    }
    closedir( dpkg_info );
    return 0;
}