#include <dirent.h>
#include <unistd.h> //symlink
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> // lstat

void make_link(){
    DIR * dir = opendir("./");
    if(dir){
        dirent * file =NULL;
        while((file=readdir(dir))!= NULL){
            char buff[256] = {0};
            strcpy(buff, file->d_name);
            char * pChar = buff;
            int isOK = 0;
            while(*pChar != 0){
                if(*pChar == '.'){
		    if(0 != strncmp(pChar, ".so.", 4)){
		        ++pChar;
                	continue;
            	    }
                    isOK = 1;
                    break;
                }
                ++ pChar;
            }
            if(0 == isOK){
                continue;
            }
           
            pChar += 4;
            isOK = 0;
            while(*pChar != 0){
                if(*pChar == '.'){
                    isOK = 1;
                    break;
                }
                ++ pChar;
            }
            if(1 == isOK){
                *pChar = 0;
                int a = symlink(file->d_name, buff);
            }
        }
        //symlink("libc.so.6","libstdc++.so.6.0.25");
        //symlink("libc-2.10.1.so","libc.so.6"); // another special one.
    }
    closedir(dir);
    dir = NULL;
}
void remove_link(){
    DIR * dir = opendir("./");
    if(dir){
        dirent * file =NULL;
        while((file=readdir(dir))!= NULL){
	    struct stat buf;
	    if(0 == lstat(file->d_name, &buf)){
		if(S_ISLNK(buf.st_mode)){
		    //printf("unlink: %s\n", file->d_name);
		    unlink(file->d_name);
                }
	    }
        }
        //unlink("libc.so.6");
    }
    closedir(dir);
    dir = NULL;
}
int main(int argc, char * argv[]){
    if(argc < 2){
	printf("usage: %s [-m|-r]\n", argv[0]);
	printf(" -m for make link, -r form remove the linkfiles\n");
	printf("press any key to quit.\n");
	getchar();
	return 0;
    }
    if(0 == strcmp(argv[1], "-m")){
	make_link();
    }else if(0 == strcmp(argv[1], "-r")){
	remove_link();
    }
    return 0;
}
