Salve ho fatto questo semplice programma che cerca un file, e quando lo trova "dovrebbe" stampare a video il contenuto di quel file. Questo è il codice:
codice:
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include<fcntl.h>
#define MAXLINE 1024
int send_shared_dir(char*,char*);
int send_shared_file(struct dirent*,char*);
int send_shared_dir(char* dirname, char* filename)
{
DIR* dir;
struct dirent* direntry;
int fd;
if ((dir = opendir(dirname)) == NULL) {
fprintf(stderr, "cannot open dir\n");
return -1; // gestione errori
}
fd = dirfd(dir);
/* change to dir */
fchdir(fd);
while ((direntry=readdir(dir)) != NULL) {
int i = send_shared_file(direntry, filename);
if(i==1) break;
}
closedir(dir);
return 1;
}
/*-----------------------------------------------------------------------------
* send_shared_file
*-----------------------------------------------------------------------------
* Write the file specified by 'direntry' in the socket (sockd)
*/
int send_shared_file(struct dirent *direntry, char* filename)
{
char file[MAXLINE];
struct stat data;
stat(direntry->d_name, &data);
if (strcmp(direntry->d_name,"..") ==0 || strcmp(direntry->d_name, ".") == 0) {
return 0;
}
if (S_ISDIR(data.st_mode)) {
if(send_shared_dir(direntry->d_name, filename)==1) return 1;
printf("back\n");
fchdir(dirfd(opendir("..")));
return 0;
}
/*
if (S_ISDIR(data.st_mode)) {
return 0;
}*/
if(strcmp(direntry->d_name,filename) == 0) {
printf("File trovato\n");
int fin;
int n;
char buf[MAXLINE];
if(fin = open(filename,O_RDONLY) < 0) {
printf("Error hile opening the file....\n");
exit(1);
}
else printf("File aperto con successo..:\n");
char temp[MAXLINE];
while(n = read(fin,buf,1)) {
printf("%s",buf);
}
return 1;
}
else {
printf("File non trovato\n");
return 0;
}
return 0;
}
int main(int sad, char** argv) {
send_shared_dir("..","find.c");
}
Praticamente mi succede questo:
quando trova il file mi stampa il messaggio "File aperto con successo..." ma si impianta e non mi stampa nulla.... :master: :master:
Può dipendere dal fatto che il metodo send_shared_file sia chiamato ricorsivamente?
Aiuto!!!!