Jump to content
mastervlad

Problema directoare c unix

Recommended Posts

Posted (edited)

Am compilat un program care imi afiseaza toate fisierele dintr-un director si ma gandeam sa-l modific astfel incat sa gasesc aceste fisiere. Vreo idee? Mersi.

PS. S-a rezolvat, m-a ajutat JIHAD, multumesc.

Edited by mastervlad
Posted (edited)

Cred ca ceva de genul asta vrei.

Iti listeaza fisierele din directory2 care nu exista in directory1.


#include <dirent.h>

int main(int argc, char *argv[]){
char *temp, *temp1, *directory1, *directory2;
DIR *dir1, *dir2;
struct dirent *epdf;
struct dirent *epdf2;
if (argc == 3) {
directory1 = argv[1];
directory2 = argv[2];
dir1 = opendir(directory1);
dir2 = opendir(directory2);
if ((dir1 != NULL) && (dir2 != NULL)){
while (epdf = readdir(dir1)){
temp = strdup(epdf->d_name);
while (epdf2 = readdir(dir2)){
temp1 = strdup(epdf2->d_name);
if(strcmp(temp,temp1)!=0)
printf("%s/%s\n",directory2,temp1);
}
}
closedir(dir1);
closedir(dir2);
}
} else exit(printf("Argument missing\n"));
printf("Done.\n");
}

Sunt mai multe moduri in care poti face asta, eu am ales calea cea mai la indemana.

am updatat sa-ti accepte si argumente.

iti sugerez sa reverifici codul ca l-am scris in graba...

Edited by JIHAD
Posted
Cred ca ceva de genul asta vrei.

Iti listeaza fisierele din directory2 care nu exista in directory1.


#include <dirent.h>

int main(int argc, char *argv[]){
char *temp, *temp1, *directory1, *directory2;
DIR *dir1, *dir2;
struct dirent *epdf;
struct dirent *epdf2;
if (argc == 3) {
directory1 = argv[1];
directory2 = argv[2];
dir1 = opendir(directory1);
dir2 = opendir(directory2);
if ((dir1 != NULL) && (dir2 != NULL)){
while (epdf = readdir(dir1)){
temp = strdup(epdf->d_name);
while (epdf2 = readdir(dir2)){
temp1 = strdup(epdf2->d_name);
if(strcmp(temp,temp1)!=0)
printf("%s/%s\n",directory2,temp1);
}
}
closedir(dir1);
closedir(dir2);
}
} else exit(printf("Argument missing\n"));
printf("Done.\n");
}

Sunt mai multe moduri in care poti face asta, eu am ales calea cea mai la indemana.

am updatat sa-ti accepte si argumente.

iti sugerez sa reverifici codul ca l-am scris in graba...

L-am verificat si am compilat, merge, dar cred ca trebuie sa precizez si calea fisierelor care apar in primul si nu apar in al2lea, cred ca trebuie o citire recursiva..

Posted (edited)

te joci tu cu el mai departe. ideea e tot asta

uite o solutie sa-ti arate cum ai zis tu.

int parsedir(char *directory1, char *directory2){
char *temp, *temp1;
DIR *dir1, *dir2;
struct dirent *epdf;
struct dirent *epdf2;
dir1 = opendir(directory1);
dir2 = opendir(directory2);
if ((dir1 != NULL) && (dir2 != NULL)){
while (epdf = readdir(dir1)){
temp = strdup(epdf->d_name);
while (epdf2 = readdir(dir2)){
temp1 = strdup(epdf2->d_name);
if(strcmp(temp,temp1)!=0)
printf("%s/%s\n",directory2,temp1);
}
}
closedir(dir1);
closedir(dir2);
}

free(temp);
free(temp1);
printf("Done.\n");
return 0;
}



int main(int argc, char *argv[]){

if (argc == 3) {
parsedir(argv[1],argv[2]);
parsedir(argv[2],argv[1]);
} else exit(printf("Argument missing\n"));
return 0;
}

Edited by JIHAD
Posted (edited)

uite si recursiv.

iti cauta doar intr-o directie. cauta fisiere din argv[1] (recursiv, toate subfolderele) in path2(recursiv, toate subfolderele) . le afiseaza si le si numara pe cele negasite.

Sa-ti mearga in ambele directii vezi exemplul dinainte.

cu placere.

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

#define path2 "/etc"

bool *dir2compare( char *path, char *findme ){
DIR *dir;
struct dirent *entry;
char spath[MAXPATHLEN] = "", spath2[MAXPATHLEN] = "";
if( !(dir = opendir( path))){ perror("opendir"); exit(1);}
for( entry = readdir( dir); entry; entry = readdir( dir)){
sprintf( spath, "%s/%s", path, entry->d_name);
if( entry->d_type == DT_REG){
if(strcmp(entry->d_name,findme)==0)
return true;
}
if( entry->d_type == DT_DIR &&
(strcmp( ".", entry->d_name)) &&
(strcmp( "..", entry->d_name))){
dir2compare(spath,findme);
}
}
closedir( dir);
}



char *dirfind( char *path, int *fgasit){
DIR *dir;
char *temp;
struct dirent *entry;
char spath[MAXPATHLEN] = "", spath2[MAXPATHLEN] = "";
if( !(dir = opendir( path))){ perror("opendir"); exit(1);}
for( entry = readdir( dir); entry; entry = readdir( dir)){
sprintf( spath, "%s/%s", path, entry->d_name);
if( entry->d_type == DT_REG){
if(!dir2compare(path2, entry->d_name)) { printf("[*] %s\n", spath); (*fgasit)++; }
}
if( entry->d_type == DT_DIR &&
(strcmp( ".", entry->d_name)) &&
(strcmp( "..", entry->d_name))){
dirfind(spath, fgasit);
}
}
closedir( dir);
return(0);
}
int main(int argc, char *argv[]){
int i = 0;
if (argc == 2){
dirfind(argv[1],&i);
printf(" %d fisiere nu corespund.\n", i);
}
}

Edited by JIHAD

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...