Bonsoir voilà, je dois trouver un fichier appartenant au "PATH" , exemple ls qui se trouve dans le /bin
J'ai fait ceci
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 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> void separe ( char *chaine, char *tab[]) { char buffer[1024]; char *chn = chaine; char *buf = buffer; int n = 0; while(*chn != '\0') { tab[n++] = buf; while(*chn != '\0' && *chn != ':') { *buf++ = *chn++; } *buf++ = '\0'; if(*chn == ':') { chn++; } } tab[n]=NULL; } int main (int argc, char *argv[]) { char *tab[50]; int i=0; char *chaine = malloc(sizeof(char)); char* cmd = malloc(20*sizeof(char)); separe(getenv("PATH"), tab); printf("entrer un fichier à trouver"); scanf("%s", cmd); i=0; DIR *dp; struct dirent *d; int trouve; dp = opendir (tab[i]); char *buffer=malloc(1000*sizeof(char)); while((d=tab[i++]) !=NULL) { trouve = strcmp(d->d_name, cmd); if(d->d_name[0] != '.') { puts(d->d_name); if(trouve==0) { strcpy(buffer, d->d_name); strcat(buffer, "/"); strcat(buffer, cmd); printf("Cette commande se trouve dans: %s", buffer); free(buffer); closedir(dp); exit (0); } } } exit (1); }
la fonction separe est juste pour séparer les différentes parties du "PATH", mais la j'avoue que je pèche un peu
Partager