#include <windows.h>
#include <stdio.h>
#include <time.h>

#include "resource.h"

#define mxDirs 10000
#define mxFiles 64000
#define mxFileL 1024*512
#define mxQBuff 102400
#define mxQl1 32
#define mxQl2 64

// FILE_ATTRIBUTE_DIRECTORY extension
#define FADE_USERS 17
#define FADE_HID 18
#define FADE_SYSHID 22
#define FADE_UNKNOWN 48
#define FADE_RECOVERY 8214
#define FADE_DOCSETS 9238
#define FADE_WIN 65552
#define FADE_PROGFILES 65553

int l, lDir, iC=10, qL, qL2, cp866up_ar [256], cp866low_ar [256], cp1251to866_ar [256],
    cp866to1251_ar [256], cp1251low_ar [256], cp1251up_ar [256],
    lowUTF8b1_ar [256], lowUTF8b2_ar [256], upUTF8b1_ar [256], upUTF8b2_ar [256],
    fC [mxFiles], rC [mxFiles], rCs [mxFiles];

char cp [256], query [mxQl1], ql [mxQl1], qu [mxQl1],
     query1251 [mxQl1], ql1251 [mxQl1], qu1251 [mxQl1], qlUTF8 [mxQl2], quUTF8 [mxQl2],
     files [mxFiles] [_MAX_PATH], dir [mxDirs] [_MAX_PATH],
     filesR [mxFiles] [_MAX_PATH], filesRs [mxFiles] [_MAX_PATH];

unsigned long long fSize, vl=0, tl=0;

bool once=false;

void cp1251to866 ()
{
 int i;

 for (i=0; i<strlen (cp); i++)
 {
  if (256+cp [i]<256)
  {
   if (cp1251to866_ar [256+cp [i]])
    cp [i]=cp1251to866_ar [256+cp [i]];
  }
 }

 printf ("%s", cp);
}

void cp866low ()
{
 int i;

 for (i=0; i<strlen (ql); i++)
 {
  if (256+ql [i]<256)
  {
   if (cp866low_ar [256+ql [i]])
    ql [i]=cp866low_ar [256+ql [i]];
  }
 }
}

void cp866up ()
{
 int i;

 for (i=0; i<strlen (qu); i++)
 {
  if (256+qu [i]<256)
  {
   if (cp866up_ar [256+qu [i]])
    qu [i]=cp866up_ar [256+qu [i]];
  }
 }
}

void cp866to1251 ()
{
 int i;

 for (i=0; i<strlen (query1251); i++)
 {
  if (256+query1251 [i]<256)
  {
   if (cp866to1251_ar [256+query1251 [i]])
    query1251 [i]=cp866to1251_ar [256+query1251 [i]];
  }
 }
}

void cp1251low ()
{
 int i;

 for (i=0; i<strlen (ql1251); i++)
 {
  if (256+ql1251 [i]<256)
  {
   if (cp1251low_ar [256+ql1251 [i]])
    ql1251 [i]=cp1251low_ar [256+ql1251 [i]];
  }
 }
}

void cp1251up ()
{
 int i;

 for (i=0; i<strlen (qu1251); i++)
 {
  if (256+qu1251 [i]<256)
  {
   if (cp1251up_ar [256+qu1251 [i]])
    qu1251 [i]=cp1251up_ar [256+qu1251 [i]];
  }
 }
}

void cp866toUTF8 ()
{
 int i, p=0;
 memset (qlUTF8, 0, mxQl2);
 memset (quUTF8, 0, mxQl2);

 for (i=0; i<strlen (qu); i++)
 {
  qlUTF8 [p]=ql [i];
  quUTF8 [p]=qu [i];

  if (256+qu [i]<256)
  {
   if (upUTF8b1_ar [256+qu [i]])
   {
    qlUTF8 [p]=lowUTF8b1_ar [256+ql [i]];
    quUTF8 [p]=upUTF8b1_ar [256+qu [i]];
    p++;
    qlUTF8 [p]=lowUTF8b2_ar [256+ql [i]];
    quUTF8 [p]=upUTF8b2_ar [256+qu [i]];
   }
  }
  p++;
 }

 qL2=strlen (quUTF8);
}

int number (int tn)
{
 char t1 [16], t2 [2];
 memset (t2, 0, 2);

 sprintf (t1, "%d", tn);
 t2 [0]=t1 [strlen (t1)-1];
 tn=atoi (t2);

 if (strlen (t1)>1 && t1 [strlen (t1)-2]=='1' && tn>0 && tn<10)
  return 2;
 else if (tn==1)
  return 0;
 else if (tn>1 && tn<5)
  return 1;
 else
  return 2;
}

bool exists (char fPath [_MAX_PATH])
{
 char tPath [_MAX_PATH];
 WIN32_FIND_DATA FindFileData;
 HANDLE hf;

 sprintf (tPath, "%s", fPath);

 if (strlen (tPath)<2)
  return false;
 else if (strlen (tPath)<4)
 {
  if (tPath [strlen (tPath)-1]=='\\')
   strcat (tPath, "*");
  else
   strcat (tPath, "\\*");
 }
 else if (tPath [strlen (tPath)-1]=='\\')
  tPath [strlen (tPath)-1]='\0';

 hf=FindFirstFile (tPath, &FindFileData);

 if (hf!=INVALID_HANDLE_VALUE)
  return true;
 else
  return false;
}

void load ()
{
 int i;
 char path [_MAX_PATH];
 l=0;
 lDir=1;

 WIN32_FIND_DATA FindFileData;
 HANDLE hf;

 for (i=0; i<lDir && i<mxDirs; i++)
 {
  sprintf (path, "%s%s", dir [i], "*.*");
  hf=FindFirstFile (path, &FindFileData);

  if (hf!=INVALID_HANDLE_VALUE)
  {
   while (true)
   {
    if (FindFileData.dwFileAttributes!=FADE_SYSHID
     && FindFileData.dwFileAttributes!=FADE_RECOVERY
     && FindFileData.dwFileAttributes!=FADE_DOCSETS
     && FindFileData.dwFileAttributes!=FADE_WIN)
    {
     if (FindFileData.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY
      && FindFileData.dwFileAttributes!=FADE_USERS
      && FindFileData.dwFileAttributes!=FADE_HID
      && FindFileData.dwFileAttributes!=FADE_UNKNOWN
      && FindFileData.dwFileAttributes!=FADE_PROGFILES)
     {
      sprintf (files [l], "%s%s", dir [i], FindFileData.cFileName);
      // more 4GB
      tl+=(FindFileData.nFileSizeHigh*21474836470/10*2+FindFileData.nFileSizeLow)/1024;//...0/10?, (MAXDWORD+1)
      l++;
     }
     else if (strcmp (FindFileData.cFileName, ".") && strcmp (FindFileData.cFileName, ".."))
     {
      sprintf (dir [lDir], "%s%s\\", dir [i], FindFileData.cFileName);
      lDir++;
     }
    }

    if (FindNextFile(hf, &FindFileData)==0 || lDir>=mxDirs || l>=mxFiles)
     break;
   }
  }
 }

 sprintf (cp, " Всего файлов: %d, директорий: %d", l, lDir);
 cp1251to866 ();
}

int main (int argc, char *argv[])
{
 char AppPath [_MAX_PATH], cfgPath [_MAX_PATH], ask [10];
 int i, ap_l=0, iL, i2, i3, c, s, rL=0, mx, rLmx, cL, timeS;
 FILE *file;

 // Generated with PHP
 for (i=0; i<256; i++)
  lowUTF8b1_ar [i]=0;
 for (i=0; i<256; i++)
  lowUTF8b2_ar [i]=0;

 lowUTF8b1_ar [240]=209; lowUTF8b2_ar [240]=145; lowUTF8b1_ar [241]=209; lowUTF8b2_ar [241]=145; lowUTF8b1_ar [128]=208; lowUTF8b2_ar [128]=176;
 lowUTF8b1_ar [129]=208; lowUTF8b2_ar [129]=177; lowUTF8b1_ar [130]=208; lowUTF8b2_ar [130]=178; lowUTF8b1_ar [131]=208; lowUTF8b2_ar [131]=179;
 lowUTF8b1_ar [132]=208; lowUTF8b2_ar [132]=180; lowUTF8b1_ar [133]=208; lowUTF8b2_ar [133]=181; lowUTF8b1_ar [134]=208; lowUTF8b2_ar [134]=182;
 lowUTF8b1_ar [135]=208; lowUTF8b2_ar [135]=183; lowUTF8b1_ar [136]=208; lowUTF8b2_ar [136]=184; lowUTF8b1_ar [137]=208; lowUTF8b2_ar [137]=185;
 lowUTF8b1_ar [138]=208; lowUTF8b2_ar [138]=186; lowUTF8b1_ar [139]=208; lowUTF8b2_ar [139]=187; lowUTF8b1_ar [140]=208; lowUTF8b2_ar [140]=188;
 lowUTF8b1_ar [141]=208; lowUTF8b2_ar [141]=189; lowUTF8b1_ar [142]=208; lowUTF8b2_ar [142]=190; lowUTF8b1_ar [143]=208; lowUTF8b2_ar [143]=191;
 lowUTF8b1_ar [144]=209; lowUTF8b2_ar [144]=128; lowUTF8b1_ar [145]=209; lowUTF8b2_ar [145]=129; lowUTF8b1_ar [146]=209; lowUTF8b2_ar [146]=130;
 lowUTF8b1_ar [147]=209; lowUTF8b2_ar [147]=131; lowUTF8b1_ar [148]=209; lowUTF8b2_ar [148]=132; lowUTF8b1_ar [149]=209; lowUTF8b2_ar [149]=133;
 lowUTF8b1_ar [150]=209; lowUTF8b2_ar [150]=134; lowUTF8b1_ar [151]=209; lowUTF8b2_ar [151]=135; lowUTF8b1_ar [152]=209; lowUTF8b2_ar [152]=136;
 lowUTF8b1_ar [153]=209; lowUTF8b2_ar [153]=137; lowUTF8b1_ar [154]=209; lowUTF8b2_ar [154]=138; lowUTF8b1_ar [155]=209; lowUTF8b2_ar [155]=139;
 lowUTF8b1_ar [156]=209; lowUTF8b2_ar [156]=140; lowUTF8b1_ar [157]=209; lowUTF8b2_ar [157]=141; lowUTF8b1_ar [158]=209; lowUTF8b2_ar [158]=142;
 lowUTF8b1_ar [159]=209; lowUTF8b2_ar [159]=143; lowUTF8b1_ar [160]=208; lowUTF8b2_ar [160]=176; lowUTF8b1_ar [161]=208; lowUTF8b2_ar [161]=177;
 lowUTF8b1_ar [162]=208; lowUTF8b2_ar [162]=178; lowUTF8b1_ar [163]=208; lowUTF8b2_ar [163]=179; lowUTF8b1_ar [164]=208; lowUTF8b2_ar [164]=180;
 lowUTF8b1_ar [165]=208; lowUTF8b2_ar [165]=181; lowUTF8b1_ar [166]=208; lowUTF8b2_ar [166]=182; lowUTF8b1_ar [167]=208; lowUTF8b2_ar [167]=183;
 lowUTF8b1_ar [168]=208; lowUTF8b2_ar [168]=184; lowUTF8b1_ar [169]=208; lowUTF8b2_ar [169]=185; lowUTF8b1_ar [170]=208; lowUTF8b2_ar [170]=186;
 lowUTF8b1_ar [171]=208; lowUTF8b2_ar [171]=187; lowUTF8b1_ar [172]=208; lowUTF8b2_ar [172]=188; lowUTF8b1_ar [173]=208; lowUTF8b2_ar [173]=189;
 lowUTF8b1_ar [174]=208; lowUTF8b2_ar [174]=190; lowUTF8b1_ar [175]=208; lowUTF8b2_ar [175]=191; lowUTF8b1_ar [224]=209; lowUTF8b2_ar [224]=128;
 lowUTF8b1_ar [225]=209; lowUTF8b2_ar [225]=129; lowUTF8b1_ar [226]=209; lowUTF8b2_ar [226]=130; lowUTF8b1_ar [227]=209; lowUTF8b2_ar [227]=131;
 lowUTF8b1_ar [228]=209; lowUTF8b2_ar [228]=132; lowUTF8b1_ar [229]=209; lowUTF8b2_ar [229]=133; lowUTF8b1_ar [230]=209; lowUTF8b2_ar [230]=134;
 lowUTF8b1_ar [231]=209; lowUTF8b2_ar [231]=135; lowUTF8b1_ar [232]=209; lowUTF8b2_ar [232]=136; lowUTF8b1_ar [233]=209; lowUTF8b2_ar [233]=137;
 lowUTF8b1_ar [234]=209; lowUTF8b2_ar [234]=138; lowUTF8b1_ar [235]=209; lowUTF8b2_ar [235]=139; lowUTF8b1_ar [236]=209; lowUTF8b2_ar [236]=140;
 lowUTF8b1_ar [237]=209; lowUTF8b2_ar [237]=141; lowUTF8b1_ar [238]=209; lowUTF8b2_ar [238]=142; lowUTF8b1_ar [239]=209; lowUTF8b2_ar [239]=143;

 for (i=0; i<256; i++)
  upUTF8b1_ar [i]=0;
 for (i=0; i<256; i++)
  upUTF8b2_ar [i]=0;

 upUTF8b1_ar [240]=208; upUTF8b2_ar [240]=129; upUTF8b1_ar [241]=208; upUTF8b2_ar [241]=129; upUTF8b1_ar [128]=208; upUTF8b2_ar [128]=144;
 upUTF8b1_ar [129]=208; upUTF8b2_ar [129]=145; upUTF8b1_ar [130]=208; upUTF8b2_ar [130]=146; upUTF8b1_ar [131]=208; upUTF8b2_ar [131]=147;
 upUTF8b1_ar [132]=208; upUTF8b2_ar [132]=148; upUTF8b1_ar [133]=208; upUTF8b2_ar [133]=149; upUTF8b1_ar [134]=208; upUTF8b2_ar [134]=150;
 upUTF8b1_ar [135]=208; upUTF8b2_ar [135]=151; upUTF8b1_ar [136]=208; upUTF8b2_ar [136]=152; upUTF8b1_ar [137]=208; upUTF8b2_ar [137]=153;
 upUTF8b1_ar [138]=208; upUTF8b2_ar [138]=154; upUTF8b1_ar [139]=208; upUTF8b2_ar [139]=155; upUTF8b1_ar [140]=208; upUTF8b2_ar [140]=156;
 upUTF8b1_ar [141]=208; upUTF8b2_ar [141]=157; upUTF8b1_ar [142]=208; upUTF8b2_ar [142]=158; upUTF8b1_ar [143]=208; upUTF8b2_ar [143]=159;
 upUTF8b1_ar [144]=208; upUTF8b2_ar [144]=160; upUTF8b1_ar [145]=208; upUTF8b2_ar [145]=161; upUTF8b1_ar [146]=208; upUTF8b2_ar [146]=162;
 upUTF8b1_ar [147]=208; upUTF8b2_ar [147]=163; upUTF8b1_ar [148]=208; upUTF8b2_ar [148]=164; upUTF8b1_ar [149]=208; upUTF8b2_ar [149]=165;
 upUTF8b1_ar [150]=208; upUTF8b2_ar [150]=166; upUTF8b1_ar [151]=208; upUTF8b2_ar [151]=167; upUTF8b1_ar [152]=208; upUTF8b2_ar [152]=168;
 upUTF8b1_ar [153]=208; upUTF8b2_ar [153]=169; upUTF8b1_ar [154]=208; upUTF8b2_ar [154]=170; upUTF8b1_ar [155]=208; upUTF8b2_ar [155]=171;
 upUTF8b1_ar [156]=208; upUTF8b2_ar [156]=172; upUTF8b1_ar [157]=208; upUTF8b2_ar [157]=173; upUTF8b1_ar [158]=208; upUTF8b2_ar [158]=174;
 upUTF8b1_ar [159]=208; upUTF8b2_ar [159]=175; upUTF8b1_ar [160]=208; upUTF8b2_ar [160]=144; upUTF8b1_ar [161]=208; upUTF8b2_ar [161]=145;
 upUTF8b1_ar [162]=208; upUTF8b2_ar [162]=146; upUTF8b1_ar [163]=208; upUTF8b2_ar [163]=147; upUTF8b1_ar [164]=208; upUTF8b2_ar [164]=148;
 upUTF8b1_ar [165]=208; upUTF8b2_ar [165]=149; upUTF8b1_ar [166]=208; upUTF8b2_ar [166]=150; upUTF8b1_ar [167]=208; upUTF8b2_ar [167]=151;
 upUTF8b1_ar [168]=208; upUTF8b2_ar [168]=152; upUTF8b1_ar [169]=208; upUTF8b2_ar [169]=153; upUTF8b1_ar [170]=208; upUTF8b2_ar [170]=154;
 upUTF8b1_ar [171]=208; upUTF8b2_ar [171]=155; upUTF8b1_ar [172]=208; upUTF8b2_ar [172]=156; upUTF8b1_ar [173]=208; upUTF8b2_ar [173]=157;
 upUTF8b1_ar [174]=208; upUTF8b2_ar [174]=158; upUTF8b1_ar [175]=208; upUTF8b2_ar [175]=159; upUTF8b1_ar [224]=208; upUTF8b2_ar [224]=160;
 upUTF8b1_ar [225]=208; upUTF8b2_ar [225]=161; upUTF8b1_ar [226]=208; upUTF8b2_ar [226]=162; upUTF8b1_ar [227]=208; upUTF8b2_ar [227]=163;
 upUTF8b1_ar [228]=208; upUTF8b2_ar [228]=164; upUTF8b1_ar [229]=208; upUTF8b2_ar [229]=165; upUTF8b1_ar [230]=208; upUTF8b2_ar [230]=166;
 upUTF8b1_ar [231]=208; upUTF8b2_ar [231]=167; upUTF8b1_ar [232]=208; upUTF8b2_ar [232]=168; upUTF8b1_ar [233]=208; upUTF8b2_ar [233]=169;
 upUTF8b1_ar [234]=208; upUTF8b2_ar [234]=170; upUTF8b1_ar [235]=208; upUTF8b2_ar [235]=171; upUTF8b1_ar [236]=208; upUTF8b2_ar [236]=172;
 upUTF8b1_ar [237]=208; upUTF8b2_ar [237]=173; upUTF8b1_ar [238]=208; upUTF8b2_ar [238]=174; upUTF8b1_ar [239]=208; upUTF8b2_ar [239]=175;

 for (i=0; i<256; i++)
  cp866to1251_ar [i]=0;

 cp866to1251_ar [240]=168; cp866to1251_ar [241]=184; cp866to1251_ar [128]=192; cp866to1251_ar [129]=193; cp866to1251_ar [130]=194;
 cp866to1251_ar [131]=195; cp866to1251_ar [132]=196; cp866to1251_ar [133]=197; cp866to1251_ar [134]=198; cp866to1251_ar [135]=199;
 cp866to1251_ar [136]=200; cp866to1251_ar [137]=201; cp866to1251_ar [138]=202; cp866to1251_ar [139]=203; cp866to1251_ar [140]=204;
 cp866to1251_ar [141]=205; cp866to1251_ar [142]=206; cp866to1251_ar [143]=207; cp866to1251_ar [144]=208; cp866to1251_ar [145]=209;
 cp866to1251_ar [146]=210; cp866to1251_ar [147]=211; cp866to1251_ar [148]=212; cp866to1251_ar [149]=213; cp866to1251_ar [150]=214;
 cp866to1251_ar [151]=215; cp866to1251_ar [152]=216; cp866to1251_ar [153]=217; cp866to1251_ar [154]=218; cp866to1251_ar [155]=219;
 cp866to1251_ar [156]=220; cp866to1251_ar [157]=221; cp866to1251_ar [158]=222; cp866to1251_ar [159]=223; cp866to1251_ar [160]=224;
 cp866to1251_ar [161]=225; cp866to1251_ar [162]=226; cp866to1251_ar [163]=227; cp866to1251_ar [164]=228; cp866to1251_ar [165]=229;
 cp866to1251_ar [166]=230; cp866to1251_ar [167]=231; cp866to1251_ar [168]=232; cp866to1251_ar [169]=233; cp866to1251_ar [170]=234;
 cp866to1251_ar [171]=235; cp866to1251_ar [172]=236; cp866to1251_ar [173]=237; cp866to1251_ar [174]=238; cp866to1251_ar [175]=239;
 cp866to1251_ar [224]=240; cp866to1251_ar [225]=241; cp866to1251_ar [226]=242; cp866to1251_ar [227]=243; cp866to1251_ar [228]=244;
 cp866to1251_ar [229]=245; cp866to1251_ar [230]=246; cp866to1251_ar [231]=247; cp866to1251_ar [232]=248; cp866to1251_ar [233]=249;
 cp866to1251_ar [234]=250; cp866to1251_ar [235]=251; cp866to1251_ar [236]=252; cp866to1251_ar [237]=253; cp866to1251_ar [238]=254;
 cp866to1251_ar [239]=255; 

 for (i=0; i<256; i++)
  cp1251low_ar [i]=0;

 cp1251low_ar [168]=184; cp1251low_ar [192]=224; cp1251low_ar [193]=225; cp1251low_ar [194]=226; cp1251low_ar [195]=227;
 cp1251low_ar [196]=228; cp1251low_ar [197]=229; cp1251low_ar [198]=230; cp1251low_ar [199]=231; cp1251low_ar [200]=232;
 cp1251low_ar [201]=233; cp1251low_ar [202]=234; cp1251low_ar [203]=235; cp1251low_ar [204]=236; cp1251low_ar [205]=237;
 cp1251low_ar [206]=238; cp1251low_ar [207]=239; cp1251low_ar [208]=240; cp1251low_ar [209]=241; cp1251low_ar [210]=242;
 cp1251low_ar [211]=243; cp1251low_ar [212]=244; cp1251low_ar [213]=245; cp1251low_ar [214]=246; cp1251low_ar [215]=247;
 cp1251low_ar [216]=248; cp1251low_ar [217]=249; cp1251low_ar [218]=250; cp1251low_ar [219]=251; cp1251low_ar [220]=252;
 cp1251low_ar [221]=253; cp1251low_ar [222]=254; cp1251low_ar [223]=255; 

 for (i=0; i<256; i++)
  cp1251up_ar [i]=0;

 cp1251up_ar [184]=168; cp1251up_ar [224]=192; cp1251up_ar [225]=193; cp1251up_ar [226]=194; cp1251up_ar [227]=195;
 cp1251up_ar [228]=196; cp1251up_ar [229]=197; cp1251up_ar [230]=198; cp1251up_ar [231]=199; cp1251up_ar [232]=200;
 cp1251up_ar [233]=201; cp1251up_ar [234]=202; cp1251up_ar [235]=203; cp1251up_ar [236]=204; cp1251up_ar [237]=205;
 cp1251up_ar [238]=206; cp1251up_ar [239]=207; cp1251up_ar [240]=208; cp1251up_ar [241]=209; cp1251up_ar [242]=210;
 cp1251up_ar [243]=211; cp1251up_ar [244]=212; cp1251up_ar [245]=213; cp1251up_ar [246]=214; cp1251up_ar [247]=215;
 cp1251up_ar [248]=216; cp1251up_ar [249]=217; cp1251up_ar [250]=218; cp1251up_ar [251]=219; cp1251up_ar [252]=220;
 cp1251up_ar [253]=221; cp1251up_ar [254]=222; cp1251up_ar [255]=223; 

 for (i=0; i<256; i++)
  cp866low_ar [i]=0;

 cp866low_ar [240]=241; cp866low_ar [128]=160; cp866low_ar [129]=161; cp866low_ar [130]=162; cp866low_ar [131]=163;
 cp866low_ar [132]=164; cp866low_ar [133]=165; cp866low_ar [134]=166; cp866low_ar [135]=167; cp866low_ar [136]=168;
 cp866low_ar [137]=169; cp866low_ar [138]=170; cp866low_ar [139]=171; cp866low_ar [140]=172; cp866low_ar [141]=173;
 cp866low_ar [142]=174; cp866low_ar [143]=175; cp866low_ar [144]=224; cp866low_ar [145]=225; cp866low_ar [146]=226;
 cp866low_ar [147]=227; cp866low_ar [148]=228; cp866low_ar [149]=229; cp866low_ar [150]=230; cp866low_ar [151]=231;
 cp866low_ar [152]=232; cp866low_ar [153]=233; cp866low_ar [154]=234; cp866low_ar [155]=235; cp866low_ar [156]=236;
 cp866low_ar [157]=237; cp866low_ar [158]=238; cp866low_ar [159]=239; 

 for (i=0; i<256; i++)
  cp866up_ar [i]=0;

 cp866up_ar [241]=240; cp866up_ar [160]=128; cp866up_ar [161]=129; cp866up_ar [162]=130; cp866up_ar [163]=131;
 cp866up_ar [164]=132; cp866up_ar [165]=133; cp866up_ar [166]=134; cp866up_ar [167]=135; cp866up_ar [168]=136;
 cp866up_ar [169]=137; cp866up_ar [170]=138; cp866up_ar [171]=139; cp866up_ar [172]=140; cp866up_ar [173]=141;
 cp866up_ar [174]=142; cp866up_ar [175]=143; cp866up_ar [224]=144; cp866up_ar [225]=145; cp866up_ar [226]=146;
 cp866up_ar [227]=147; cp866up_ar [228]=148; cp866up_ar [229]=149; cp866up_ar [230]=150; cp866up_ar [231]=151;
 cp866up_ar [232]=152; cp866up_ar [233]=153; cp866up_ar [234]=154; cp866up_ar [235]=155; cp866up_ar [236]=156;
 cp866up_ar [237]=157; cp866up_ar [238]=158; cp866up_ar [239]=159; 

 for (i=0; i<256; i++)
  cp1251to866_ar [i]=0;

 cp1251to866_ar [168]=240; cp1251to866_ar [184]=241; cp1251to866_ar [192]=128; cp1251to866_ar [193]=129; cp1251to866_ar [194]=130;
 cp1251to866_ar [195]=131; cp1251to866_ar [196]=132; cp1251to866_ar [197]=133; cp1251to866_ar [198]=134; cp1251to866_ar [199]=135;
 cp1251to866_ar [200]=136; cp1251to866_ar [201]=137; cp1251to866_ar [202]=138; cp1251to866_ar [203]=139; cp1251to866_ar [204]=140;
 cp1251to866_ar [205]=141; cp1251to866_ar [206]=142; cp1251to866_ar [207]=143; cp1251to866_ar [208]=144; cp1251to866_ar [209]=145;
 cp1251to866_ar [210]=146; cp1251to866_ar [211]=147; cp1251to866_ar [212]=148; cp1251to866_ar [213]=149; cp1251to866_ar [214]=150;
 cp1251to866_ar [215]=151; cp1251to866_ar [216]=152; cp1251to866_ar [217]=153; cp1251to866_ar [218]=154; cp1251to866_ar [219]=155;
 cp1251to866_ar [220]=156; cp1251to866_ar [221]=157; cp1251to866_ar [222]=158; cp1251to866_ar [223]=159; cp1251to866_ar [224]=160;
 cp1251to866_ar [225]=161; cp1251to866_ar [226]=162; cp1251to866_ar [227]=163; cp1251to866_ar [228]=164; cp1251to866_ar [229]=165;
 cp1251to866_ar [230]=166; cp1251to866_ar [231]=167; cp1251to866_ar [232]=168; cp1251to866_ar [233]=169; cp1251to866_ar [234]=170;
 cp1251to866_ar [235]=171; cp1251to866_ar [236]=172; cp1251to866_ar [237]=173; cp1251to866_ar [238]=174; cp1251to866_ar [239]=175;
 cp1251to866_ar [240]=224; cp1251to866_ar [241]=225; cp1251to866_ar [242]=226; cp1251to866_ar [243]=227; cp1251to866_ar [244]=228;
 cp1251to866_ar [245]=229; cp1251to866_ar [246]=230; cp1251to866_ar [247]=231; cp1251to866_ar [248]=232; cp1251to866_ar [249]=233;
 cp1251to866_ar [250]=234; cp1251to866_ar [251]=235; cp1251to866_ar [252]=236; cp1251to866_ar [253]=237; cp1251to866_ar [254]=238;
 cp1251to866_ar [255]=239;


 char txt [_MAX_PATH];
 memset (txt, 0, _MAX_PATH);
 GetModuleFileName (NULL, txt, _MAX_PATH);
 for (i=_MAX_PATH-1; i>=0 && ap_l==0; i--)
 {
  if (txt [i]=='\\')
   ap_l=i+1;
 }

 for (i=0; i<ap_l; i++)
  AppPath [i]=txt [i];

 memset (cfgPath, 0, _MAX_PATH);
 memcpy (cfgPath, AppPath, ap_l);
 strcat (cfgPath, "data\\config.txt");

 char qDir [_MAX_PATH], qDir2 [_MAX_PATH], qp [mxQBuff];

 sprintf (cp, "\n");
  strcat (cp, " Регистронезависимая поисковая утилита.\n");
  strcat (cp, " Не распознаёт архивы. Читает первые полмегабайта из файла.\n");
  strcat (cp, " Пробелы в именах каталогов и запросах должны быть заменены на \"%\".\n");
  strcat (cp, " Поддерживает кодировки: CP866 (DOS), CP1251 (Windows) и UTF-8.\n");
 cp1251to866 ();

 file=fopen (cfgPath, "rb");
 if (file!=NULL)
 {
  fgets (qDir2, _MAX_PATH, file);
  fclose (file);
 }

 if (strlen (qDir2)>0 && exists (qDir2))
 {
  sprintf (cp, "\n Введите \"s\", чтобы пропустить вопрос\n и задать путь \"%s\".", qDir2);
  cp1251to866 ();

  while (true)
  {
   sprintf (cp, "\n Директория? ");
   cp1251to866 ();

   scanf ("%s", qp); 
   memset (qDir, 0, _MAX_PATH);
   strncpy (qDir, qp, _MAX_PATH-1);

   if (qDir [1]!=':')// !strcmp (qDir, "s")
   {
    sprintf (qDir, "%s", qDir2);
    break;
   }
   else
   {
    for (i=0; i<strlen (qDir); i++)
     if (qDir [i]=='%')
      qDir [i]=' ';

    if (exists (qDir))
    {
     file=fopen (cfgPath, "wb");
     if (file!=NULL)
     {
      fputs (qDir, file);
      fclose (file);
     }

     break;
    }
    else
    {
     sprintf (cp, " Такой директории нет.");
     cp1251to866 ();
    }
   }
  }
 }
 else
 {
  while (true)
  {
   sprintf (cp, "\n Директория? ");
   cp1251to866 ();

   scanf ("%s", qp);
   memset (qDir, 0, _MAX_PATH);
   strncpy (qDir, qp, _MAX_PATH-1);
   for (i=0; i<strlen (qDir); i++)
    if (qDir [i]=='%')
     qDir [i]=' ';

   if (exists (qDir))
    break;
   else
   {
    sprintf (cp, " Такой директории нет.");
    cp1251to866 ();
   }
  }

  file=fopen (cfgPath, "wb");
  if (file!=NULL)
  {
   fputs (qDir, file);
   fclose (file);
  }
 }

 if (qDir [strlen (qDir)-1]!='\\')
  strcat (qDir, "\\");

 memset (ask, 0, 10);
 while (ask [0]!='q' && ask [0]!='Q' && ask [0]!=169-256 && ask [0]!=137-256)//strcmp (ask, "q")
 {
  sprintf (cp, " Запрос? ");
  cp1251to866 ();

  scanf ("%s", qp);// query
  memset (query, 0, mxQl1);
  strncpy (query, qp, mxQl1-1);
  for (i=0; i<strlen (query); i++)
   if (query [i]=='%')
    query [i]=' ';

  if (!once)
   printf ("\n");

  timeS=clock ();

  iL=0;

  sprintf (dir [0], "%s", qDir);

  strcpy (ql, query);
  strcpy (qu, query);
  strupr (qu);
  strlwr (ql);
  cp866low ();
  cp866up ();

  strcpy (query1251, query);
  cp866to1251 ();
  strcpy (ql1251, query1251);
  strcpy (qu1251, query1251);
  strupr (qu1251);
  strlwr (ql1251);
  cp1251low ();
  cp1251up ();

  cp866toUTF8 ();

  rL=0; rLmx=0; qL=strlen (query);

  if (argc>2)
   rLmx=atoi (argv [2]);

  FILE *fOpen;
  char fBody [mxFileL+_MAX_PATH+2];

  char basePath [_MAX_PATH], tPath [_MAX_PATH];
  sprintf (basePath, "%s", dir [0]);

  if (!once)
   load ();

  for (i=0; i<l; i++)
  {
   fC [i]=0;
   rC [i]=0;
   rCs [i]=0;
  }

  if (!once)
   printf ("\n");
  sprintf (cp, " Выполнено: ");
  cp1251to866 ();
  char ps [4];
  memset (ps, 0, 4);
  int pr=0, lp=-1;
  if (!l)
   printf ("100%c", 37);
  for (i=0; i<l; i++)
  {
   pr=(i+1)*100/l;
   if (pr!=lp)
   {
    for (int ip=0; ip<strlen (ps); ip++)
     printf ("%c", 8);
    printf ("%d%c", pr, 37);
    sprintf (ps, "%d+", pr);
    lp=pr;
   }

   fOpen=fopen (files [i], "rb");
   if (fOpen!=NULL)
   {
    // less than 4GB
    fseek (fOpen, 0, SEEK_END);// seek to end of file
    fSize=ftell (fOpen);// get current file pointer
    fseek (fOpen, 0, SEEK_SET);// seek to begin of file

    char fName [_MAX_PATH];
    memset (fName, 0, _MAX_PATH);
    for (i2=strlen (files [i])-1; i2>0; i2--)
    {
     if (files [i] [i2]=='\\')
     {
      for (i3=i2+1; i3<strlen (files [i]); i3++)
       fName [i3-(i2+1)]=files [i] [i3];
      break;
     }
    }

    if (fSize>mxFileL)
     fSize=mxFileL;
    if (!once)
     vl+=fSize/1024;
    memset (fBody, 0, mxFileL+_MAX_PATH+2);
    fread (fBody, 1, fSize, fOpen);

    fBody [fSize]=' ';
    for (i2=0; i2<strlen (fName); i2++)
     fBody [fSize+i2+1]=fName [i2];

    if (!strcmp (ql, ql1251) && !strcmp (qu, qu1251))// latin only
    {
     for (i2=0; i2<int (fSize+strlen (fName)-qL+2); i2++)// int:-1?
     {
      c=0;
      for (i3=0; i3<qL; i3++)
      {
       if (ql [i3]!=fBody [i2+i3] && qu [i3]!=fBody [i2+i3])
        break;
        c++;
      }
      if (c==qL)
       fC [i]++;
     }
    }
    else
    {
     for (i2=0; i2<int (fSize+strlen (fName)-qL+2); i2++)
     {
      c=0;
      for (i3=0; i3<qL; i3++)
      {
       if (ql [i3]!=fBody [i2+i3] && qu [i3]!=fBody [i2+i3])
        break;
       c++;
      }
      if (c==qL)
       fC [i]++;
      else
      {
       c=0;
       for (i3=0; i3<qL; i3++)
       {
        if (ql1251 [i3]!=fBody [i2+i3] && qu1251 [i3]!=fBody [i2+i3])
         break;
        c++;
       }
       if (c==qL)
        fC [i]++;
       else 
       {
        c=0;
        for (i3=0; i3<qL2; i3++)
        {
         if (qlUTF8 [i3]!=fBody [i2+i3] && quUTF8 [i3]!=fBody [i2+i3])
          break;
         c++;
        }
        if (c==qL2)
         fC [i]++;
       }
      }
     }
    }
    fclose (fOpen);
   }
  }
  if (once)
   printf ("\n");

  if (!once)
  {
   char tv [16], tt [16], tr [16];
   sprintf (tv, "%d", vl/1024);
   sprintf (tt, "%d", tl/1024);
   memset (tr, 0, 16);
   c=0; s=0;
   for (i=strlen (tv)-1; i>=0; i--)
   {
    tr [strlen (tv)-1-i+s]=tv [i];
    c++;
    if (c==3 && i!=0)
    {
     c=0;
     s++;
     tr [strlen (tv)-1-i+s]=' ';
    }
   }
   memset (tv, 0, 16);
   for (i=strlen (tr)-1; i>=0; i--)
    tv [strlen (tr)-1-i]=tr [i];

   c=0; s=0;
   for (i=strlen (tt)-1; i>=0; i--)
   {
    tr [strlen (tt)-1-i+s]=tt [i];
    c++;
    if (c==3 && i!=0)
    {
     c=0;
     s++;
     tr [strlen (tt)-1-i+s]=' ';
    }
   }
   memset (tt, 0, 16);
   for (i=strlen (tr)-1; i>=0; i--)
    tt [strlen (tr)-1-i]=tr [i];

   int percent;
   if (tl>0)
    percent=vl*100/tl;
   else
    percent=100;
   sprintf (cp, "\n Просмотрено: %d%c (%s из %s Мб)\n", percent, 37, tv, tt);
   cp1251to866 ();

   once=true;
  }

  for (i=0; i<l; i++)
  {
   if (fC [i]>0)
   {
    rC [rL]=fC [i];
    sprintf (filesR [rL], "%s", files [i]);
    rL++;
   }
  }

  char timeP [16], timeP1 [16], timeP2 [16];
  int minutes=0;
  float seconds=float (float (clock ()-timeS)/1000);
  char minutesN [3] [16]={"минуту", "минуты", "минут"};

  if (seconds>=60)
  {
   minutes=seconds/60;
   seconds-=minutes*60;
   sprintf (timeP1, "%d", minutes);
   sprintf (timeP2, "%f", seconds);
   for (i=0; i<5; i++)
    timeP2 [strlen (timeP2)-1]='\0';

   sprintf (timeP, "%s %s %s с.", timeP1, minutesN [number (minutes)], timeP2);
  }
  else
  {
   sprintf (timeP2, "%f", seconds);
   for (i=0; i<5; i++)
    timeP2 [strlen (timeP2)-1]='\0';

   sprintf (timeP, "%s с.", timeP2);
  }

  sprintf (cp, " Запрос занял: %s\n", timeP);
  cp1251to866 ();
  sprintf (cp, " Найдено: %d\n", rL);
  cp1251to866 ();

  printf ("%c", 7);// beep

  cL=0;
  if (rL>0)
  {
   printf ("\n");

   for (i=0; i<rL; i++)
   {
    mx=0;
    for (i2=0; i2<rL; i2++)
    {
     if (rC [i2]>rCs [i])
     {
      rCs [i]=rC [i2];
      sprintf (filesRs [i], "%s", filesR [i2]);
      mx=i2;
     }
    }
    rC [mx]=0;
   }

   if (rLmx>0)
   {
    cL=rLmx;
    if (cL>rL)
     cL=rL;
   }
   else
    cL=rL;

   int mxL=0;
   iL=cL;
   if (cL>iC)
    cL=iC;
   for (i=0; i<cL; i++)
   {
    if (strlen (filesRs [i])-strlen (basePath)>mxL)
     mxL=strlen (filesRs [i])-strlen (basePath);
   }

   char tP [_MAX_PATH], scL [4], sI [4], iP [4];
   for (i=0; i<cL; i++)
   {
    c=0;
    memset (tPath, 0, _MAX_PATH);
    for (i2=strlen (basePath); i2<strlen (filesRs [i]); i2++)
    {
     tPath [c]=filesRs [i] [i2];
     c++;
    }

    memset (iP, 0, 4);
    sprintf (scL, "%d", cL);
    sprintf (sI, "%d", i+1);
    for (i2=0; i2<strlen (scL)-strlen (sI); i2++)
     iP [i2]=' ';
    strcat (iP, sI);

    sprintf (cp, " %s) %s: %d\n", iP, tPath, rCs [i]);
    cp1251to866 ();
   }
  }

  if (cL==1)
  {
   sprintf (cp, "\n Введите \"n\", чтобы начать новый поиск в той же директории,\n \"q\", чтобы выйти, \"1\" для открытия файла или \"-1\" - каталога: ");
   cp1251to866 ();
  }
  else if (cL>1)
  {
   sprintf (cp, "\n Введите \"n\", чтобы начать новый поиск в той же директории,\n \"q\", чтобы выйти или число от 1 до %d для открытия файла\n (укажите префикс \"-\" для открытия каталога, например \"-%d\"): ", iL, cL);
   cp1251to866 ();
  }
  else
  {
   sprintf (cp, "\n Введите \"n\", чтобы начать новый поиск в той же директории,\n или \"q\" чтобы выйти: ");
   cp1251to866 ();
  }

  memset (ask, 0, 10);
  bool fn=false;
  while (ask [0]!='q' && ask [0]!='Q' && ask [0]!=169-256 && ask [0]!=137-256
      && ask [0]!='n' && ask [0]!='N' && ask [0]!=226-256 && ask [0]!=146-256)
  {
   scanf ("%s", qp);
   strncpy (ask, qp, 10);

   bool od=false;
   if (ask [0]=='-')
   {
    for (i=0; i<strlen (ask)-1; i++)
     ask [i]=ask [i+1];
    ask [strlen (ask)-1]='\0';
    od=true;
   }

   if (iL>0 && atoi (ask)>0 && atoi (ask)<=iL)
   {
    char ta [_MAX_PATH+2], tb [_MAX_PATH+2], tc [_MAX_PATH+12];
    sprintf (ta, "%s", filesRs [atoi (ask)-1]);

    if (od)
    {
     for (i=strlen (ta)-1; i>=0; i--)
     {
      if (ta [i]=='\\')
       break;
      ta [i]='\0';
     }
    }
    sprintf (tb, "%s", ta);

    for (i=0; i<strlen (ta); i++)
    {
     if (ta [i]==' ')
     {
      sprintf (ta, "\"%s\"", tb);
      break;
     }
    }

    if (od)
     sprintf (tc, "explorer %s", ta);
    else
     sprintf (tc, "%s", ta);
    
    if (!fn)
    {
     printf ("\n");
     fn=true;
    }
    sprintf (cp, " Открываю \"%s\", ", tb);
    cp1251to866 ();

    if (od)
     system (tc);
    else
     ShellExecute (NULL, "open", tc, "", "", SW_SHOW);
   }
  }
  printf ("\n");
 }
}