679
|
1 module cn.kuehne.uniquefile;
|
|
2
|
|
3 private import std.file;
|
|
4 private import std.stdio;
|
|
5 private import std.path;
|
|
6
|
|
7 private char[][char[]] known;
|
|
8 private uint doppelgaenger;
|
|
9
|
|
10 private char[] lastPathElem(char[] path){
|
|
11 size_t end=path.length-1;
|
|
12 size_t start;
|
|
13
|
|
14 for(end = path.length-1; path[end]==std.path.sep[0]; end--){
|
|
15 if(end==0){
|
|
16 return std.path.sep;
|
|
17 }
|
|
18 }
|
|
19
|
|
20 for(start = end-1; start<start.max; start--){
|
|
21 if(path[start]==std.path.sep[0]){
|
|
22 break;
|
|
23 }
|
|
24 }
|
|
25
|
|
26 if(start==start.max){
|
|
27 start=0;
|
|
28 }else{
|
|
29 start+=1;
|
|
30 }
|
|
31 return path[start .. end+1];
|
|
32 }
|
|
33
|
|
34 private void add(char[] file){
|
|
35 char[] base = lastPathElem(file);
|
|
36
|
|
37 if(isfile(file)){
|
|
38 char[]* tmp = base in known;
|
|
39 if(tmp){
|
703
|
40 fwritef(stdout, "%s :\n\t%s\n\t%s\n", base, file, *tmp);
|
679
|
41 doppelgaenger++;
|
|
42 }else{
|
|
43 known[base] = file;
|
|
44 }
|
|
45 }
|
|
46
|
|
47 if(isdir(file) && (base[0]!='.')){
|
|
48 foreach(char[] entry; listdir(file)){
|
|
49 add(file~std.path.sep~entry);
|
|
50 }
|
|
51 }
|
|
52 }
|
|
53
|
|
54 int main(char[][] args){
|
|
55 if(args.length<2){
|
|
56 fwritef(stderr, "at least one directory name is required as argument\n");
|
|
57 return -1;
|
|
58 }
|
|
59
|
|
60 doppelgaenger = 0;
|
|
61
|
|
62 foreach(char[] file; args[1 .. args.length]){
|
|
63 if(file=="."){
|
|
64 file=getcwd();
|
|
65 }
|
|
66 add(file);
|
|
67 }
|
696
|
68
|
|
69 version(verbose){
|
|
70 foreach(char[] entry; known.keys.sort){
|
707
|
71 fwritef(stderr, "\t%s\n", known[entry]);
|
696
|
72 }
|
|
73 }
|
679
|
74
|
703
|
75 fwritef(stdout, "hits : %s\n", doppelgaenger);
|
679
|
76
|
|
77 return doppelgaenger > 0;
|
703
|
78 }
|