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