IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

probleme avec stat


Sujet :

C

  1. #1
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2011
    Messages : 9
    Par défaut probleme avec stat
    Bonjour, je suis actuellement en train de recoder la fonction ls (un projet de mon ecole)
    je bloque a cause de la fonction stat.
    apparament, il y a une erreur dans mon code car avec
    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
     
    void    aff_right(struct stat *recupstat)
    {
      if ((S_IFMT & recupstat->st_mode) == S_IFREG)
        printf("-");
      else if ((S_IFMT & recupstat->st_mode) == S_IFDIR)
        printf("d");
      else if ((S_IFMT & recupstat->st_mode) == S_IFCHR)
        printf("c");
      else if ((S_IFMT & recupstat->st_mode) == S_IFBLK)
        printf("b");
      else if ((S_IFMT & recupstat->st_mode) == S_IFIFO)
        printf("p");
      else if ((S_IFMT & recupstat->st_mode) == S_IFLNK)
        printf("l");
      else if ((S_IFMT & recupstat->st_mode) == S_IFSOCK)
        printf("s");
      else
      printf("?");
      ((recupstat->st_mode & S_IRUSR) == S_IRUSR) ? printf("r") : printf("-");
      ((recupstat->st_mode & S_IWUSR) == S_IWUSR) ? printf("w") : printf("-");
      ((recupstat->st_mode & S_IXUSR) == S_IXUSR) ? printf("x") : printf("-");
      ((recupstat->st_mode & S_IRGRP) == S_IRGRP) ? printf("r") : printf("-");
      ((recupstat->st_mode & S_IWGRP) == S_IWGRP) ? printf("w") : printf("-");
      ((recupstat->st_mode & S_IXGRP) == S_IXGRP) ? printf("x") : printf("-");
      ((recupstat->st_mode & S_IROTH) == S_IROTH) ? printf("r") : printf("-");
      ((recupstat->st_mode & S_IWOTH) == S_IWOTH) ? printf("w") : printf("-");
      ((recupstat->st_mode & S_IXOTH) == S_IXOTH) ? printf("x\n") : printf("-\n");
    }
    mon programme m'affiche tout fièrement
    ?-wx-----x
    ?-wx-----x
    ?-wx-----x
    ?-wx-----x
    ?-wx-----x

    au lieu de :
    -rwxr-xr-x
    -rw-r--r--
    -rw-r--r--
    -rw-r--r--
    -rw-r--r--
    alors voila, j'ai chercher sur internet pas mal de temps, mais impossible de changer quoi que ce soit...

  2. #2
    Membre émérite
    Avatar de D[r]eadLock
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    504
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 504
    Par défaut
    Bizarre, car chez moi, ça a l'air de marcher, i.e. pas de '?' dans l'output. Montre le reste du code, i.e. comment tu appelles ta fonction. (et aussi la valeur de st_mode, genre un %x au lieu de ?).
    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
    #include <sys/stat.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
     
    void aff_right(struct stat *recupstat)
    {
      if ((S_IFMT & recupstat->st_mode) == S_IFREG)
        printf("-");
      else if ((S_IFMT & recupstat->st_mode) == S_IFDIR)
        printf("d");
      else if ((S_IFMT & recupstat->st_mode) == S_IFCHR)
        printf("c");
      else if ((S_IFMT & recupstat->st_mode) == S_IFBLK)
        printf("b");
      else if ((S_IFMT & recupstat->st_mode) == S_IFIFO)
        printf("p");
      else if ((S_IFMT & recupstat->st_mode) == S_IFLNK)
        printf("l");
      else if ((S_IFMT & recupstat->st_mode) == S_IFSOCK)
        printf("s");
      else
      printf("?");
      ((recupstat->st_mode & S_IRUSR) == S_IRUSR) ? printf("r") : printf("-");
      ((recupstat->st_mode & S_IWUSR) == S_IWUSR) ? printf("w") : printf("-");
      ((recupstat->st_mode & S_IXUSR) == S_IXUSR) ? printf("x") : printf("-");
      ((recupstat->st_mode & S_IRGRP) == S_IRGRP) ? printf("r") : printf("-");
      ((recupstat->st_mode & S_IWGRP) == S_IWGRP) ? printf("w") : printf("-");
      ((recupstat->st_mode & S_IXGRP) == S_IXGRP) ? printf("x") : printf("-");
      ((recupstat->st_mode & S_IROTH) == S_IROTH) ? printf("r") : printf("-");
      ((recupstat->st_mode & S_IWOTH) == S_IWOTH) ? printf("w") : printf("-");
      ((recupstat->st_mode & S_IXOTH) == S_IXOTH) ? printf("x") : printf("-");
    }
     
    int main(int argc, char *argv[])
    {
    	int i;
    	for(i=0; i < argc; ++i) {
    		struct stat s;
    		if (stat(argv[i], &s) == 0) {
    			aff_right(&s);
    			printf(" %s\n", argv[i]);
    		} else {
    			fprintf(stderr, "Failed to stat %s: %s\n", argv[i],
    				strerror(errno));
    		}
    	}
    	return 0;
    }

  3. #3
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2011
    Messages : 9
    Par défaut
    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
     
    void            recup(char *name)
    {
      struct stat           *recup_stat;
      struct passwd         *struct_name;
      struct group          *group;
     
      stat(name, recup_stat);
      if (name[0] != '.')
        {
          aff_right(recup_stat);
          //printf(" %i ", recup_stat->st_nlink);                                                                                                                                                                      
          struct_name = getpwuid(recup_stat->st_uid);
          //printf("%s ", struct_name->pw_name);                                                                                                                                                                       
          group = getgrgid(recup_stat->st_gid);
          //printf ("%s ", group->gr_name);                                                                                                                                
          /*printf(" %s\n", name);*/
        }
    }
    ca c'est l'appel de aff right.
    pour ce qui est de la valeur de st_mode, elle vaut : 400b40c1
    elle ne change pour aucun des fichier...

  4. #4
    Membre prolifique
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 811
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 811
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par grocko_t Voir le message
    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
     
      if ((S_IFMT & recupstat->st_mode) == S_IFREG)
        printf("-");
      else if ((S_IFMT & recupstat->st_mode) == S_IFDIR)
        printf("d");
      else if ((S_IFMT & recupstat->st_mode) == S_IFCHR)
        printf("c");
      else if ((S_IFMT & recupstat->st_mode) == S_IFBLK)
        printf("b");
      else if ((S_IFMT & recupstat->st_mode) == S_IFIFO)
        printf("p");
      else if ((S_IFMT & recupstat->st_mode) == S_IFLNK)
        printf("l");
      else if ((S_IFMT & recupstat->st_mode) == S_IFSOCK)
        printf("s");
      else
      printf("?");
    Salut
    Pourquoi tu n'utilises pas les macro S_ISDIR(), S_ISREG(), S_ISFIFO(), ... dédiées à ce travail ???
    Citation Envoyé par grocko_t Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      ((recupstat->st_mode & S_IRUSR) == S_IRUSR) ? printf("r") : printf("-")
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    printf("%c",  ((recupstat->st_mode & S_IRUSR) == S_IRUSR) ?"r" :"-")


    Citation Envoyé par grocko_t Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    void            recup(char *name)
    {
      struct stat           *recup_stat;
     
      stat(name, recup_stat);
    Ouais !!! Et vas-y que je stocke des données dans un pointeur non alloué...
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

Discussions similaires

  1. my_ls probleme avec la fonction stat
    Par toams69 dans le forum C
    Réponses: 1
    Dernier message: 11/11/2008, 11h38
  2. Probleme de stats avec criteres de dates
    Par reuel dans le forum IHM
    Réponses: 7
    Dernier message: 24/07/2007, 12h39
  3. probleme avec la touche F10
    Par b.grellee dans le forum Langage
    Réponses: 2
    Dernier message: 15/09/2002, 22h04
  4. Probleme avec fseek
    Par Bjorn dans le forum C
    Réponses: 5
    Dernier message: 04/08/2002, 07h17
  5. [Kylix] probleme avec un imagelist
    Par NicoLinux dans le forum EDI
    Réponses: 4
    Dernier message: 08/06/2002, 23h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo