#include <stdio.h>
//#include <stdlib.h>
#include <string.h>
//#include <unistd.h>
//#include <sys/errno.h>
#include <sys/stat.h>
#include <dirent.h>
#include <iconv.h>

iconv_t cd;

void recode (const char *from, size_t fl,char * to, size_t tl){//перекодирует
    //strncpy(to,from,strlen(from));//заглушка
    iconv(cd, (char **) &from, &fl, (char **) &to, &tl);
}

void mkln(char *from,char *where){//перекодирует и  делает  симлинку на from в папке to
    return;//заглушка
}
//----------------------------------------------------
void list(char *from,char *to){
  //int i,j;
  DIR *d=NULL;
  struct dirent *de=NULL;
  struct stat entry_info;
  char buf[10000],newitem[5000],tmp[1000];
  
  
  if((d=opendir(from))==NULL)return;
  while(de=readdir(d)){
    if((strcmp("..",de->d_name)==0)||(strcmp(".",de->d_name)==0))continue;
    memset(buf,0,sizeof(buf));
    sprintf(buf,"%s/%s",from,de->d_name);//полное имя элемента
    if(stat(buf,&entry_info))continue;//если ошибка - пропускаем
    
    memset(newitem,0,sizeof(newitem));
    memset(tmp,0,sizeof(tmp));
    
    recode((char *)&(de->d_name),strlen(de->d_name),tmp,sizeof(tmp));
    sprintf(newitem,"%s/%s",to,tmp);
    if(S_ISREG(entry_info.st_mode)){
	//printf("f\t%s\n",buf)
	//printf("symlink from %s to %s\n",buf,newitem);
	symlink(buf,newitem);
	/*
	if(symlink(buf,newitem)){
	    switch(errno){
	    case EPERM:printf("EPERM\n");
	    case EFAULT:printf("EFAULT\n");
	    case EACCES:printf("EACCES\n");
	    case ENAMETOOLONG:printf("ENAMETOOLONG\n");
	    case ENOENT:printf("ENOENT\n");
	    case ENOTDIR:printf("ENOTDIR\n");
	    case ENOMEM:printf("ENOMEM\n");
	    case EROFS:printf("EROFS\n");
	    case EEXIST:printf("EEXIST\n");
	    case ELOOP:printf("ELOOP\n");
	    case ENOSPC:printf("ENOSPC\n");
	    }
	}*/
	continue;
    }
    if(S_ISDIR(entry_info.st_mode)){
	//printf("d\t%s\n",buf);
	mkdir(newitem,entry_info.st_mode);
	list(buf,(char *)&newitem);
	continue;
    }
    printf("%s\n",buf);
    
  }
  closedir(d);
}
//====================================================
int main(int argc, char *argv[], char *env[])
{

  char *from,*to;
  
  if(argc<2){
    return 0;
  }
  from=argv[1];
  to=argv[2];
  cd = iconv_open("cp1251","koi8-r");
  list(from,to);
  iconv_close(cd);
  return 0;
}
