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 :

recreer la fonction LS-LR


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 226
    Par défaut recreer la fonction LS-LR solution
    boujours tous le monde


    je usi vraiment debutant je n'y comprend pas grand chose mais j'essaie

    voila je dois faire un programme qui ressemble a la fonction Ls-R.

    J'ai deja pu affiche les droits

    je ne sais pas comment faire pour affiche le reste (date, taille, ....)

    avec l'appel au fonction

    opendir, chdir, readdir, and stat
    merci de votre aide d,avance

    voila 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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     
    #include <sys/types.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <string.h>
    #include <pwd.h>
    int recls(char *);
    int lsdetails(struct stat*); //note alternative prototype
    int printperms(mode_t mode);
    int printowner(int uid);
    int main(int argc, char **argv){
    	recls(argv[1]);
    }	
    int recls(char *path)	{
    	DIR *dir;
    	struct dirent *dirslot;
    	struct stat statbuff;
    	chdir(path);
    	dir=opendir(".");
    	while (1) {
    		dirslot=readdir(dir);
    		if (dirslot==NULL) break;//note change
    		printf(" %s ", dirslot->d_name);
    		stat(dirslot->d_name, &statbuff);
    		lsdetails(&statbuff);
    	} 
    	rewinddir(dir);
    	while (1) {
    		dirslot=readdir(dir);
    		if (dirslot==NULL) return;//note
    		if (strcmp(dirslot->d_name, ".")==0) continue;
    		if (strcmp(dirslot->d_name, "..")==0) continue;
    		stat(dirslot->d_name, &statbuff);
     		if (S_ISDIR(statbuff.st_mode))  {
                    	printf(" %s\n", dirslot->d_name);
                    	chdir(dirslot->d_name);
                    	recls(".");
                    	chdir("..");
                    	}
    		}
    	}
    int lsdetails(struct stat *astatbuff) {
    		printowner(astatbuff->st_uid);
    		printperms(astatbuff->st_mode);
    		return 0;
    		}
     
    int printowner(int uid) {
    	printf("%s", getpwuid (uid)->pw_name);
    	}
     
    int printperms(mode_t mode)	{
    	char perms[11]; int i;
    	for (i=0;i<11;i++)  perms[i]='-';
    	perms[10]=0;
    	if (mode & S_IRUSR) perms[1]='r';
    	if (mode & S_IWUSR) perms[2]='w';
    	if (mode & S_IXUSR) perms[3]='x';
    	if (mode & S_I
    		printf("  %s\n",perms);
    	}

  2. #2
    Membre émérite Avatar de valefor
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    711
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 711
    Par défaut
    La page de man de stat te donne la structure que te remplis la fonction. Tu devrais la dépouiller un peut plus, cette structure.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 226
    Par défaut
    bon je n'ai pas eu rellement de reponse,


    je vous donne la reponse, j'ai a peut pret reussi apres vous pouvez l'ameliorer et poster vos programme

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
     
     
    #include <sys/types.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <string.h>
    #include <pwd.h>
    #include <grp.h>
    #include <time.h>
     
    struct stat statbuff;
    int recls(char *);
    int lsdetails(struct stat*); //note alternative prototype
    int printperms(mode_t mode);
    int printowner(int uid);
     
    int main(int argc, char **argv){
    	recls(argv[1]);
    	printf("\n");
    }	
     
    int recls(char *path){
    	DIR *dir;                                             
    //Pointer of type DIR
    	struct dirent *dirslot;
     
    	chdir(path);
    	dir=opendir(".");                                     
    //Open the current directory
    	while (1) {
    		dirslot=readdir(dir);
    		if (dirslot==NULL) break;//note change
     
    		stat(dirslot->d_name, &statbuff);
    		lsdetails(&statbuff);
    		printf(" \t%s\n ", dirslot->d_name);               
    //Display the name of file
    	} 
    	rewinddir(dir);
    	while (1) {
    		dirslot=readdir(dir);                              
    //Read the directory
    		if (dirslot==NULL) return;//note
    		if (strcmp(dirslot->d_name, ".")==0) continue;     
    // If pointer d_name current fonction continue
    		if (strcmp(dirslot->d_name, "..")==0) continue;   
    // If pointer d_name parent  fonction continue
    		stat(dirslot->d_name, &statbuff);
     		if (S_ISDIR(statbuff.st_mode))  {
                    	printf("\n %s:\n", dirslot->d_name);   
    // Display the name of the directory
                    	chdir(dirslot->d_name);
                    	recls(".");                            
    //Call function recls
                    	chdir("..");                           
    //Change directory into the parent
                    	}
    		}
    	}
    int lsdetails(struct stat *astatbuff) {
     
    		printperms(astatbuff->st_mode);                 
    //call function printperms
    		printf(" %d",astatbuff->st_nlink);              
    //Hard link
    		printowneruid(astatbuff->st_uid);               
    //Call printowneruid
    		printownergid(astatbuff->st_gid);              
    //Call function printownergid
    		printf(" %d\t",astatbuff->st_size);             
    //Display size
    		lastmodif();                                   
    //Call function lastmodif 
    		return 0;
    		}
     
    int printowneruid(int uid) {
    	printf("\t%s\t", getpwuid (uid)->pw_name);          
    // Display user name ID
    	}
     
    int printownergid(int gid) {
    	printf("%s\t", getgrgid (gid)->gr_name);            
    //Display group name ID
    	}
     
    int printperms(mode_t mode)	{                           
    //Display all permition 
    	char perms[11]; int i;
    	for (i=0;i<11;i++)  perms[i]='-';  //regular file
    	perms[10]=0;
     
    	if (S_ISDIR(mode))perms[0]='d';  //Directory
    	if (S_ISBLK(mode))perms[0]='b';  //Block device
    	if (S_ISCHR(mode))perms[0]='c';  //Character device
    	if (S_ISLNK(mode))perms[0]='l';  //Symbolic link
    	if (S_ISSOCK(mode))perms[0]='s'; //Socket
     
    	if (mode & S_IRUSR) perms[1]='r';  //Permission owner read 
    	if (mode & S_IWUSR) perms[2]='w';  //Permission owner write 
    	if (mode & S_IXUSR) perms[3]='x';  //Permission owner execute 
     
    	if (mode & S_IRGRP) perms[4]='r';  //Permission group read 
    	if (mode & S_IWGRP) perms[5]='w';  //Permission group write 
    	if (mode & S_IXGRP) perms[6]='x';  //Permission group execute
     
     
    	if (mode & S_IROTH) perms[7]='r';  //Permission others read 
    	if (mode & S_IWOTH) perms[8]='w';  //Permission others write 
    	if (mode & S_IXOTH) perms[9]='x';  //Permission others execute
     
     
    		printf("%s\t",perms);          
    //Display all perms end after display /t it for tab
    	}
     
    int lastmodif()                                                    
    //New prog for date and hours
    {
    	struct tm *date;
    	date=localtime(&statbuff.st_mtime);                           
    //It's latest date of modification
     
    	//Date
     
    	printf("%d-",date->tm_year+1900);                              
    //Display year linux begin in 1900 for display good Year add +1900
    		//display months correctly
    		if(date->tm_mon<10){printf("0%d-",date->tm_mon+1);}        
    //Display month and linux display 0 to 11 and for display good month add +1 
    		else printf("%d:",date->tm_mon+1);
     
    		if (date->tm_mday<10) {printf("0%d",date->tm_mday);}       // Display Day
    		else printf("%d",date->tm_mday);
     
    	  // ex: March-08-2008
    	  // The fonction ls-lr display March-08-2008  but for display 08 add 0 if number of month is <10
     
     
            //Display hour correctly
            if (date->tm_hour<10){printf("  0%d:",date->tm_hour);}     
     // Display Hour
            else printf("   %d:",date->tm_hour);
     
            if (date->tm_min<10){printf("0%d:",date->tm_min);}          
    // Display Minute
            else printf("%d:",date->tm_min);
     
            if (date->tm_sec<10){printf("0%d",date->tm_sec);}          
    // Display Seconde
            else printf("%d",date->tm_sec);
     
            // ex: 10:06:55
            // The fonction ls-lr display 10:06:55  but for display 06 add 0 if hours or minute or seconde <10
     
    }

    je peux mettre ce aue j'ai fait car il fallais le rendre hier.

    mon programme fonctionne

    mais vous pouvez poster des ameliorations.

    merci pour tous.

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 226
    Par défaut
    je vois que depuis que j'ai mi ma reponse personne n'a mi de reponse ou a amelioré le programme.

    je vous attend


    Aller bonne journée a tous

  5. #5
    Expert confirmé

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 610
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 610
    Billets dans le blog
    2
    Par défaut
    simplement parce qu'ici ce n'est pas un forum "jeux" ou "lyceens.com" ou quelque chose du genre...

    Mais parce qu'on pose des questions et on y repond : entraide..

    Comme dit la banniere :

    "Du debutant au Professionnel"

    Tu as réusssi, tant mieux pour toi.

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2008
    Messages : 226
    Par défaut
    je ne prend pas ça pour un jeux.

    C'est juste que si d'autre personne peuvent l'ameliorer ça peut aider a comprendre d'autre ligne de code et permettre d'analyser la programmation.

    Un forom comme celui si et la aussi pour apprendre et non faire du plagiat ou que de l'aide sinon je n'en voit pas l'utilité.


    Dsl de repondre comme ça mais bon

  7. #7
    Expert confirmé

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 610
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 610
    Billets dans le blog
    2
    Par défaut
    ok une amélioration toute simple :

    Au lieu de :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
            //Display hour correctly
            if (date->tm_hour<10){printf("  0%d:",date->tm_hour);}     
     // Display Hour
            else printf("   %d:",date->tm_hour);
     
            if (date->tm_min<10){printf("0%d:",date->tm_min);}          
    // Display Minute
            else printf("%d:",date->tm_min);
     
            if (date->tm_sec<10){printf("0%d",date->tm_sec);}          
    // Display Seconde
            else printf("%d",date->tm_sec);
    mettre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
            printf("%.02d:%.02d:%.02d",
                    date->tm_hour, date->tm_min, date->tm_sec);

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 8
    Dernier message: 12/02/2013, 01h08
  2. Fonction API
    Par margilb dans le forum C++Builder
    Réponses: 2
    Dernier message: 08/07/2002, 11h11
  3. Implémentation des fonctions mathématiques
    Par mat.M dans le forum Mathématiques
    Réponses: 9
    Dernier message: 17/06/2002, 16h19
  4. fonction printf
    Par ydeleage dans le forum C
    Réponses: 7
    Dernier message: 30/05/2002, 11h24
  5. FOnction api specifiant la position de la souris
    Par florent dans le forum C++Builder
    Réponses: 4
    Dernier message: 15/05/2002, 20h07

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