summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ctm/ctm_scan/ctm_scan.c
blob: cf6e38e04a1750fbe1e15cd74dd40f39ecff17a3 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <dirent.h>
#include <md5.h>

int barf[256];

int 
pstrcmp(char **p, char **q)
{
    return strcmp(*p,*q);
}

int
Do(char *path)
{
    DIR *d;
    struct dirent *de;
    struct stat st;
    int ret=0;
    u_char buf[BUFSIZ];
    u_char data[BUFSIZ],*q;
    int bufp;
    MD5_CTX ctx;
    int fd,i,j,k,l,npde,nde=0;
    char **pde;
 
    npde = 1;
    pde = malloc(sizeof *pde * (npde+1));
    d = opendir(path);
    if(!d) { perror(path); return 2; }
    if(!strcmp(path,".")) {
	*buf = 0;
    } else {
	strcpy(buf,path);
	if(buf[strlen(buf)-1] != '/')
	    strcat(buf,"/");
    }
    bufp = strlen(buf);
    while((de=readdir(d))) {
	if(!strcmp(de->d_name,".")) continue;
	if(!strcmp(de->d_name,"..")) continue;
	if(nde >= npde) {
	    npde *= 2;
	    pde = realloc(pde,sizeof *pde * (npde+1));
	}
	strcpy(buf+bufp,de->d_name);
	if(stat(buf,&st)) {
	    ret |= 1;
	    continue;
	}
	if((st.st_mode & S_IFMT) == S_IFDIR) {
	    strcat(buf,"/");
	}
	pde[nde] = malloc(strlen(buf+bufp)+1);
        strcpy(pde[nde++],buf+bufp);
    }
    closedir(d);
    if(!nde) return 0;
    qsort(pde,nde,sizeof *pde,pstrcmp);
    for(k=0;k<nde;k++) {
	strcpy(buf+bufp,pde[k]);
        free(pde[k]);
	if(stat(buf,&st)) {
	    ret |= 1;
	    continue;
	}
	switch(st.st_mode & S_IFMT) {
	    case S_IFDIR:
		i = printf("d %s %o %d %d - - -\n",	
		    buf,st.st_mode & (~S_IFMT),st.st_uid,st.st_gid);
		if(!i) 
		    exit(-1);
		ret |= Do(buf);
		break;
	    case S_IFREG:
		fd = open(buf,O_RDONLY);
		if(fd < 0) {
		    ret |= 1;
		    continue;
		}
		MD5Init(&ctx);
		l = j = 0;
		while(0 < (i = read(fd,data,sizeof data))) {
		    l = (data[i-1] == '\n');
		    MD5Update(&ctx,data,i);
		    for(q=data;i && !j;i--)
			if(barf[*q++])
			    j=1;
		}
		if(!l)
		    j=1;
		close(fd);
		i = printf("f %s %o %d %d %d %d %s\n",
		    buf,st.st_mode & (~S_IFMT),st.st_uid,st.st_gid,
		    j,st.st_size,MD5End(&ctx));
		if(!i) 
		    exit(-1);
		break;
	    default:
		fprintf(stderr,"%s: type 0%o\n",buf, st.st_mode & S_IFMT);
		ret |= 4;
		break;
	}
    }
    free(pde);
    return ret;
}

int
main(int argc, char **argv)
{
    /*
     * Initialize barf[], characters diff/patch will not appreciate.
     */

    barf[0x00] = 1;
    barf[0x7f] = 1;
    barf[0x80] = 1;
    barf[0xff] = 1;

    /*
     * First argument, if any, is where to do the work.
     */
    if (argc > 1) {
	if(chdir(argv[1])) {
	    perror(argv[1]);
	    return 2;
	}
    }

    /* 
     * Scan the directories recursively.
     */
    return Do(".");
}
OpenPOWER on IntegriCloud