Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 9 odgovora
  • 8.627 pregleda

Najnovija ↓

Prolazak kroz sve direktorijume??? 10.04.2001. u 03:25 · pre 311 meseci #1
Da li neko zna ili ima neku ideju za pisanje procedure koja prolazi kroz sve direktorijume na disku,drugim recima da pretrazi ceo disk na svim particijama?
Recimo da napravi spisak svih fajlova na disku i da sva ta imena fajlova sa putanjama ucita u neki slog???

Bio bih vam puno zahvalan na idejama ili mozda nekim resenjima
thanx ;)
Re: Prolazak kroz sve direktorijume??? 10.04.2001. u 09:00 · pre 311 meseci #2
Eve ti pa se zanimaj :o)
Code:

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

int yes_dir(const char *);
char *find;

int main(int argc,char *argv[]){
  struct DIR *dir;
  struct dirent *dirent;
  char filename[1024];
  if (argc!=3){
    printf("Usage %s <dir> <file to find>\n",argv[0]);
    exit(1);
  }
  find=argv[2];
  dir=(struct DIR *)opendir(argv[1]);
  if ( dir == NULL ){
    perror("opendir");
    exit(1);
  }
   while ( (dirent = readdir((DIR *)dir)) != NULL ){
    if ( *argv[1]=='/' )
       sprintf(filename,"%s/%s",argv[1],dirent->d_name);
     else
       sprintf(filename,"/%s/%s",argv[1],dirent->d_name);
    if ( strcmp("/",argv[1])==0 )
       sprintf(filename,"/%s",dirent->d_name);
    if ( is_dir(filename) ){
     if (strcmp(".",dirent->d_name) && strcmp("..",dirent->d_name) ){
        yes_dir(filename);
     }
     }else{
        if ( strcmp(find,dirent->d_name)==0 )
             printf("%s\n",filename);
   }
  }
  closedir((DIR *)dir);
}

int is_dir(const char *path){
  struct stat buf;
  lstat(path,&buf);
  if ( buf.st_mode & S_IFDIR )
     return 1;
  return 0;
}

/* If it is dir then call this function */
int yes_dir(const char *path){
  struct DIR * dir;
  struct dirent * dirent;
  char filename[1024];
  dir=(struct DIR *)opendir(path);
   if (dir==NULL){
     return 1;
  }
  while ( (dirent=readdir((DIR *)dir)) != NULL ){
     if (*path=='/')
        sprintf(filename,"%s/%s",path,dirent->d_name);
     else
        sprintf(filename,"/%s/%s",path,dirent->d_name);
     if ( is_dir(filename) ){
        if ( strcmp(".",dirent->d_name) && strcmp("..",dirent->d_name) )
            yes_dir(filename);
     }else{
         if ( strcmp(find,dirent->d_name)==0 )
            printf("%s\n",filename);
     }
  }
 closedir((DIR *)dir);
}


Ovaj kod je bio malo cudan ali evo ti jos jedan :
Code:

/* log cleaner utility :o)                                            *
 * It is often used ./logclean /var/log                               *
 *                                     signoff predator               *
 *                                     preedator(at)sendmail(dot)ru   *
 **********************************************************************/

/* include files */
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

/* path to /dev/null */
char *linkovano="/dev/null";
/* Name of dir which is passed as 2nd argument */
char *dir_name;
/* some usefull variables */
struct DIR * dir;
struct dirent *open_dir;

/* Declaration of functions */
int is_dir(const char *);
int check_for_dev_null();
int cleanlog(const char *);
int yes_dir(const char *);

int main(int argc,char **argv){
 printf(" log cleaner by predator <[email protected]>\n");
 if (!check_for_dev_null()){
   printf(" Aborting,aborting.../dev/null isn't char device\n");
   printf(" Logs didn't chang,please remove all /var/log \n");
   exit(1);
 }
 if (argc!=2){
   printf(" Usage %s <log dir> \n",argv[0]);
   exit(1);
 }
 dir_name=argv[1];
 dir=(struct DIR *)opendir(dir_name);
 chdir(dir_name);
 while ((open_dir=readdir((DIR *)dir))!=NULL){
   if (!is_dir(open_dir->d_name)){
     chdir(dir_name);
     cleanlog(open_dir->d_name);
   }else{
     /* if dir is '.' or '..' ignore it *
      * I got SIGSEGV with this :o)     */
     if (strcmp(open_dir->d_name,".")!=0 && strcmp(open_dir->d_name,"..")!=0){
     yes_dir(open_dir->d_name);
     }
   }
 }
closedir((DIR *)dir);
return 0;
}

/* This function will check our file it is called after readdir */
int is_dir(const char *path){
  struct stat buf;
  lstat(path,&buf);
  if (buf.st_mode & S_IFDIR)
     return 1;    //1 if it is dir
  return 0;       //0 if it is file
}

/* This function will check is /dev/null char device   *
 * If it isn't log cleaner will abort                  */
int check_for_dev_null(){
  struct stat buf;
  lstat(linkovano,&buf);
  if (buf.st_mode & S_IFCHR)
    return 1;
  return 0;
}

/* This function is mother of all :o)   *
 * if file is directory is is called    *
 * This one use recursion function call */
int yes_dir(const char *path){        //funny name yes_dir :o))))
 struct DIR *tmp;
 struct dirent *tmp_dir;
 /* Opening our dir path */
 tmp=(struct DIR *)opendir(path);
 if ( tmp==NULL )
    return 1;
 /* No chdir() to that dir */
 chdir(path);
 /* Show may begin */
 while ((tmp_dir=readdir((DIR *)tmp))!=NULL){
  /* Now chceck if file is dir */
  if (!is_dir(tmp_dir->d_name)){
    /* If it isn't call cleanlog() */
    cleanlog(tmp_dir->d_name);
  }else{
    /* if dir is '.' or '..' ignore it *
     * I got SIGSEGV with this :o)     */
    if (strcmp(tmp_dir->d_name,".")!=0 && strcmp(tmp_dir->d_name,"..")!=0){
    /* Call again this function */
    yes_dir(tmp_dir->d_name);
    }
  }
}
 /* When we clean our subdir we must go one dir up */
  closedir((DIR *)tmp);
  chdir("..");
return 0;
}

/* This functio will delete file and link it to /dev/null */
int cleanlog(const char *path){
 unlink(path);
 symlink(linkovano,path);
 return 0;
}


Re: Prolazak kroz sve direktorijume??? 22.04.2001. u 22:13 · pre 310 meseci #4
Nisu mi bash jasne sledece stvari:
1. gde da nadjem unistd.h tj. u kom CPP okruzenju se on nalazi
2. koliko vidim prvi source trazi ODREDJENI fajl u ODREDJENOM direktorijumu a potreban je prog koji prolazi kroz sve direktorijume i skuplja imena fajlova

Voleo bih da predator (ako ima vremena i volje) malko pojasni
Re: Prolazak kroz sve direktorijume??? 22.04.2001. u 22:54 · pre 310 meseci #5
Ok pod
1: unistd.h se nalazi /usr/include ali kako je to default include direktorijum ubacujes ga samo sa #include <unistd.h> a okruzenje je UNIX :o) Ne znam kako radi cygwin gnu C compiler za win

2. Pa prolazi ovo kroz sve direktorijume samo satvi za dir da bude root (/) i on ce sve direktorijume i poddirektorijum da izlista...
A sacu da pojasnim ovaj kod :o) (nadam se da si to trazio):

posto je kod sa chdir("..") i chdir(dir) mnogo lepsi njega cu da objasnim...to je ovaj drugi kod :

int yes_dir(const char *path){ //funny name yes_dir :o))))

Ova funkcija se pozva kad god se naidje na neki dir !!!!

Code:

 struct DIR *tmp;
 struct dirent *tmp_dir;
 /* Opening our dir path */
 tmp=(struct DIR *)opendir(path);
 if ( tmp==NULL )
    return 1;


Ako otvaranje tog direktorijuma ne uspe iz bilo kog razloga izlazi se iz funkcije...Mada ne vidim razlog zasto ne bi uspelo (jedino zbog dozvola)


/* Nwo chdir() to that dir */
chdir(path);

Ovde ulazimo u taj dir i pocinjemo sa njegovim listanjem...za svaki entry proveravamo da li je dir...ako je dir poziva se opet ova funkcija i ulazi se u taj dir to je ovaj sledeci kod
Code:
[b]

 /* Show may begin */
 while ((tmp_dir=readdir((DIR *)tmp))!=NULL){
  /* Now chceck if file is dir */
  if (!is_dir(tmp_dir->d_name)){
    /* If it isn't call cleanlog() */
    cleanlog(tmp_dir->d_name);
  }else{
    /* if dir is '.' or '..' ignore it *
     * I got SIGSEGV with this :o)     */
    if (strcmp(tmp_dir->d_name,".")!=0 && strcmp(tmp_dir->d_name,"..")!=0){
    /* Call again this function */
    yes_dir(tmp_dir->d_name);
    }
  }              
}
[/b]


Kada se zavrsi listanje direktorijuma moramo se vratiti tamo odakle smo krenuli...drugim recima kad se izlista ceo dir i nenadje se nista vraca se na dir odakle je sve pocelo

Code:

 /* When we clean our subdir we must go one dir up */
  closedir((DIR *)tmp);
  chdir("..");
return 0;
}         


Drugim recima kako to radi recimo imas direktorijum sa 2 dira :

pocinje listanje tog direktorijuma i naidje se na prvi dir ulazi se u njega i on se lista po istom principu...kad se zavrsi svo listanje (kad nema vise nista u prvom diru) vraca se na paren i nastavlja njega da lista dalje dok ne nadije na sledeci dir...2gi dir cita isto tako...
Uopste glavna fora ovde je rekurzija...da nije nje namucio bih se gadno :o)
Re: Re: Prolazak kroz sve direktorijume??? 24.04.2001. u 13:26 · pre 310 meseci #7

U svakom slucaju ja bih dodao da je predatorov primer dobar. readdir() je mnogo mocna stvar! Imao sam davno problema sa tim i resio sam citavu pricu bez readdir(). Problem je sto je program komercijalan i ne smem da vam posaljem kod. :(

Pozdrav svima

Leka

Potpis
Dejan Lekic
software engineer, MySQL/PgSQL DBA, sysadmin
Re: Prolazak kroz sve direktorijume??? 07.05.2001. u 12:19 · pre 310 meseci #9
Predator, hvala na objasnjenju koda, mislim da sam sada razumeo koja je
logika programa. Ne znam koga si pitao za sta sluzi program, mene ili Leku?
Meni je trebao taj source, a Najlepsi je prvo pitao umesto mene, pa ako te
interesuje cemu ce da sluzi, reci da ti kazem :)
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme