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); }
Partager