/* nicolas.janey@univ-fcomte.fr */

#include <io.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <direct.h>

void parcourt(char *d);

void effacement(char *d) {
  char *commande =(char *) calloc(4096,1);
  strcpy(commande,"rd ");
  strcat(commande,d);
  strcat(commande," /s /q");
  printf("%s\n",commande);
  system(commande);
  free(commande);
}

void descente(char *wd,char *rep) {
  int l = strlen(rep);
  if ( ( l == 1 ) && ( rep[0] == '.' ) )
    return;
  if ( ( l == 2 ) && ( rep[0] == '.' ) && ( rep[1] == '.' ) )
    return;
  char *r =(char *) calloc(4096,1);
  strcpy(r,wd);
  strcat(r,"\\");
  strcat(r,rep);
  if ( !strcmp(rep,"_vti_cnf") )
    effacement(r);
    else
    parcourt(r);
  free(r);
}

struct repertoire {
  char name[4096]; };

void parcourt(char *wd) {
  _chdir(wd);
  int cpt = 0;
  { struct _finddata_t fileinfo ;
    long hld = _findfirst("*.*",&fileinfo) ;
    if ( hld != -1 )
      if ( fileinfo.attrib & _A_SUBDIR ) {
        cpt++; }
    while ( !_findnext(hld,&fileinfo) ) {
      if ( fileinfo.attrib & _A_SUBDIR ) {
        cpt++; } }
    _findclose(hld) ; }
  repertoire *rp =(repertoire *) calloc(cpt,sizeof(repertoire));
  cpt = 0;
  { struct _finddata_t fileinfo ;
    long hld = _findfirst("*.*",&fileinfo) ;
    if ( hld != -1 )
      if ( fileinfo.attrib & _A_SUBDIR ) {
        strcpy((char *) &rp[cpt],fileinfo.name);
        cpt++; }
    while ( !_findnext(hld,&fileinfo) ) {
      if ( fileinfo.attrib & _A_SUBDIR ) {
        strcpy((char *) &rp[cpt],fileinfo.name);
        cpt++; } }
    _findclose(hld) ; }
  for ( int i = 0 ; i < cpt ; i++ ) {
    descente(wd,(char *) &rp[i]); }
  free(rp);
}

int main(int argc,char* argv[]) {
  char cwd[4096];
  _getcwd(cwd,4096);
  parcourt(cwd);
	return 0;
}

