Salut a tous,
J'ai un petit pour ne pas dire gros probleme avec un programme que je dois ecrire. Le but est de lister dans un fichier les paths des fichiers contenus dans un repertoires et ses sous repertoires ayant plus qu'un lien dur.

Pour se faire, j'ai ecrit un code qui effectue un parcours recursif du repertoire mais je dois utiliser les appels systeme open, write et close et je suis un peu embrouille au niveau des conflits que cela genere par rapport a l'acces a mon fichier

Je me tourne donc vers vous, plein d'espoir

voici mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
 
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
 
int checkAllFilesInDir(char * directory){
 
        DIR * dir;
        FILE * descriptor;
        struct dirent * file;
 
        if ((dir = opendir(directory))==NULL){
                return EXIT_FAILURE;
        }
 
        while ((file = readdir(dir))!=NULL){
                if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
                        struct stat file_info;
                        char file_path[0x100];
                        strncpy(file_path, directory, sizeof(file_path));
                        strncat(file_path, "/", sizeof(file_path));
                        strncat(file_path, file->d_name, sizeof(file_path));
 
                        if (lstat(file_path, &file_info)!=-1){
                                if (S_ISDIR(file_info.st_mode)){
                                        checkAllFilesInDir(file_path);
                                }
                                if (S_ISREG(file_info.st_mode)){
                                        if (file_info.st_nlink > 1){
                                                int fh;
                                                if ((fh = open("data.txt", O_CREAT|O_APPEND&O_RDWR,0777))==-1){
                                                        perror("Unable to open data");
                                                        return EXIT_FAILURE;
                                                }
                                                /* ici je dois faire mon appel write */
                                                if (close(fh)==-1){
                                                        perror("Close error");
                                                        return EXIT_FAILURE;
                                                }
                                        }
                                }
                        }
                }
        }
        closedir(dir);
        return EXIT_SUCCESS;
}