File: dash-search.c

package info (click to toggle)
cruft 0.9.31
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 35,520 kB
  • ctags: 129
  • sloc: ansic: 811; sh: 324; perl: 247; python: 75; makefile: 69
file content (70 lines) | stat: -rw-r--r-- 1,617 bytes parent folder | download | duplicates (4)
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
#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

int shellexp(char*, char*);

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

    dpkg_info = opendir(dir);
    if ( dpkg_info == NULL ) {
	perror(dir);
	exit(EXIT_FAILURE);
    }

    if (chdir(dir))
        return EXIT_FAILURE;
    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 );
		    if (printf("%s/%s: %s\n", dir, filename, argv[i]) < 0)
		    	return EXIT_FAILURE; 
		}
	    }
	}
	fclose(f);
    }
    closedir( dpkg_info );
    if (fclose(stdout) != 0)
    	return EXIT_FAILURE;
    return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
	int ret;

	/*
	if (( ret = dash_search("/usr/lib/cruft/filters-broken_symlinks", argc, argv)) != EXIT_SUCCESS)
		return ret;
	if (( ret = dash_search("/usr/lib/cruft/filters-frbn", argc, argv)) != EXIT_SUCCESS)
		return ret;
	if (( ret = dash_search("/usr/lib/cruft/filters-miss", argc, argv)) != EXIT_SUCCESS)
		return ret;
	*/
	if (( ret = dash_search("/usr/lib/cruft/filters-unex", argc, argv)) != EXIT_SUCCESS)
		return ret;
	
	return ret;
}