Parce que tu as oublie d'inclure <stdlib.h>. Du coup, on se retrouve au comportement par defaut, i.e. on suppose que realloc() retourne un int.Citation:
Envoyé par Flophx
Version imprimable
Parce que tu as oublie d'inclure <stdlib.h>. Du coup, on se retrouve au comportement par defaut, i.e. on suppose que realloc() retourne un int.Citation:
Envoyé par Flophx
je reprends donc :
Code:
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 for ( i = 3 ; i < nargs ; i++ ) { if ( (strstr(argv[i], "/mnt") != NULL) || (strchr(argv[i], "/dev") != NULL) ) { compteur = 0 ; /* On explore les champs suivants */ for ( j = (i+1) ; j < nargs ; j++ ) { if ( ((strstr(argv[i], "/mnt") != NULL) && ((strchr(argv[j], "/dev") != NULL) || (strcmp(argv[j], "\n") == 0))) || ((strstr(argv[i], "/dev") != NULL) && ((strchr(argv[j], "/mnt") != NULL) || (strcmp(argv[j], "\n") == 0))) ) compteur = compteur + 1 ; } /* Si on a trouvé aucun argument, erreur */ if ( compteur == 0 ) fprintf ( stderr, "Mauvais nombre de paramètres"); else { for ( j = 0 ; j < compteur ; j++ ) /* Ici on concatène les arguments */ /* et on incrémente le compteur total d'arguments */ i = i + compteur ; } } }
Dans un premier temps, je pense qu'ajouter
aprèsCode:fflush(stdout);
devrait faire en sorte que "ici" s'affiche comme c'est attendu.Code:printf("ici");
Oui, j'avais effectivement oublié la librairie. C'est déjà ca de réglé, en revanche, la boucle se stoppe toujours. Je vais explorer les autres solutions proposéesCitation:
DaZumba
Parce que tu as oublie d'inclure <stdlib.h>. Du coup, on se retrouve au comportement par defaut, i.e. on suppose que realloc() retourne un int.
J'ai testé cette solution ne m'a pas permis d'afficher le printf("ici").Code:
1
2 fflush(stdout);
pour la solution de souviron34 avec :
J'ai des erreurs de cast à la compilation, pourtant j'ai bien inclus la librairie <string.h>.Code:
1
2 strchr, ststr
Pour info, combien de caracteres comptes tu mettre dans tmp ? A vue de nez, pas beaucoup ...Citation:
Envoyé par Flophx
Je compte faire le schéma suivant:
tmp <= nom
nom<= argument
concaténation(nom, tmp)
Le nombre de caractère dépend du nom de la clé USB. Je pourrais éventuellement transformer le
parCode:
1
2 sizeof(char*)
Code:
1
2 strlen(nom)+2
Hello,
Attention, sed -e 's/ststr/strstr/g' ;) je ne connais pas de fonction C ststr(), par contre strstr() oui. Sinon, je garderais une comparaison à "/mnt/" et "/dev/" en vérifiant qu'il s'agit en plus du début de la chaine, au cas où des malins saisissent des chemins du genre /opt/devil ou bien encore /home/myhome/bin/mnthelpers/mySpecialMounts.sh par exemple.Citation:
Envoyé par souviron34
A+
Hello,
Tss tss tss, pas de transformations douteuses. Si on revient à ton besoin initial, le but est de ne faire qu'un argument avec ce qui suit /mnt si il y a éventuellement des espaces. Pourquoi ne pas générer par exemple la chaine spécifiant le point de montage avec les espaces "échappés" par un \ (POSIX compliant).Citation:
Envoyé par Flophx
Exple:
prog je\ suis\ tout \seul
Il n'y a qu'un argv[1] qui vaut "je suis tout seul".
Voici un exemple de programme pour ton cas avec la ligne de commande ./video :
Il faut penser à d'éventuels arguments comme le -c situé entre point et périphérique de montage pour le cas de la ligne de commande ./streamer. C'est laissé en exo.Code:
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 #include <stdlib.h> #include <stdio.h> #include <string.h> void displayProgCall(int argc, char ** argv, int flagErrStream) { int i=0; FILE * outputStream = flagErrStream ? stderr : stdout; while(i<argc) { fprintf(outputStream, "%s'%s'", (i==0) ? "\tProgram invocation: " : " ", argv[i]); ++i; } fprintf(outputStream, "\n"); i=0; while(i<argc) { fprintf(outputStream, "argv[%d]: %s\n", i, argv[i]); ++i; } } int main(int argc, char ** argv) { if (argc < 6) { fprintf(stderr, "Bad number of arguments (must be at least 6)!\n"); displayProgCall(argc, argv, 1); return EXIT_FAILURE; } else { int i = 0; char * mntBeginningArg = argv[4]; /* /mnt/... */ char * mntVerify = strstr(mntBeginningArg, "/mnt/"); char * newMntParam = NULL; /* Predicate for the moment, might be at arg above 5*/ int startIdxDev = 5; char * devBeginningArg = argv[startIdxDev]; char * devVerify = strstr(devBeginningArg, "/dev/"); if (mntVerify == NULL || (mntVerify && mntVerify != mntBeginningArg)) { fprintf(stderr, "Your 4 arg must specify the mount point directory! (shall contains and begin by /mnt/)\n"); displayProgCall(argc, argv, 1); return EXIT_FAILURE; } /* OK we have found a correct mount point at parameter 4 */ /* Let's search the peripheral ID now */ while (devVerify == NULL && startIdxDev < (argc-1)) { ++startIdxDev; devBeginningArg = argv[startIdxDev]; devVerify = strstr(devBeginningArg, "/dev/"); } if (devVerify == NULL || ( devVerify && (startIdxDev == (argc-1)) && (devVerify != devBeginningArg) ) ) { fprintf(stderr, "Device directory not found!\n"); displayProgCall(argc, argv, 1); return EXIT_FAILURE; } /* OK, it means in our case that there are startIdxDev-4-1 spaces to drop for /mnt args */ size_t len = 0u; for (i=4; i<startIdxDev; ++i) { len += strlen(argv[i]); len += 2; } newMntParam = malloc(len); if (newMntParam) { for (i=4; i<startIdxDev; ++i) { strcat(newMntParam, argv[i]); if (i<(startIdxDev-1)) { strcat(newMntParam, "\\ "); } } } else { fprintf(stderr, "Allocation failure for newMntParam\n"); return EXIT_FAILURE; } puts("THE NEW COMMAND TO BE RUN SHOULD BE:"); for(i=0; i<4; ++i) { printf("%s ", argv[i]); fflush(stdout); } printf("%s ", newMntParam); fflush(stdout); for(i=startIdxDev; i<argc; ++i) { printf("%s ", argv[i]); fflush(stdout); } puts(""); free(newMntParam); } /* else nothing to do */ return EXIT_SUCCESS; }
Au passage, stdlib.h est un fichier d'entête et non une "librairie" .
A+
nb: pourquoi ne pas utiliser getopt c'est posix
ici
De façon simple, on pourrait pas essayer de voir pourquoi ma boucle stoppe, sans chercher une solution autre (non pas qu eje dénigre l'autre, mais afin que je comprenne ce qui cloche)?
Tu as peut être oublié <stdlib.h> ? Montre le code réel. (Je suggère l'usage d'un snippet)Citation:
Envoyé par Flophx
Avec sizeof char*, tu ne vas pas aller très loin. Tu sais vraiment ce que tu fais ?
D'autre part, realloc() fonctionne soit à partir de NULL, soit à partir de l'adresse d'un bloc déja alloué par<malloc(), calloc() realloc() ou (POSIX.1) strdup().
Tel quel, le comportement est indéfini.
Mes variables sont mises à NULL dès le départ, je peux donc utiliser realloc.Citation:
D'autre part, realloc() fonctionne soit à partir de NULL, soit à partir de l'adresse d'un bloc déja alloué par<malloc(), calloc() realloc() ou (POSIX.1) strdup().
Néanmoins, pourquoi la boucle s'arrete-t-elle?? S'il y a un comportement indéfini ou autre, cela génèrera pas plutot une erreur dans la console, et pas "je m'arrete et je rends la main"?
Le code posté est complet!! Il manque une fonction, mais elle n'influence en rien le comportement du programme.
Alors je peux le compiler.Citation:
Envoyé par Flophx
Oué ! Super complet le code !Code:
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 Project : Forums Compiler : GNU GCC Compiler (called directly) Directory : C:\dev\forums2\ -------------------------------------------------------------------------------- Switching to target: default Compiling: main.c main.c:3: error: `NULL' undeclared here (not in a function) main.c:6: error: syntax error before "for" main.c:16: error: syntax error before string constant main.c:16: warning: type defaults to `int' in declaration of `printf' main.c:16: warning: function declaration isn't a prototype main.c:16: warning: conflicting types for built-in function 'printf' main.c:16: warning: data definition has no type or storage class main.c:17: error: syntax error before '--' token main.c:18: error: syntax error before string constant main.c:18: warning: type defaults to `int' in declaration of `printf' main.c:18: warning: function declaration isn't a prototype main.c:18: warning: data definition has no type or storage class main.c:34: warning: type defaults to `int' in declaration of `nom' main.c:34: error: conflicting types for 'nom' main.c:3: error: previous definition of 'nom' was here main.c:34: warning: implicit declaration of function `realloc' main.c:34: warning: implicit declaration of function `strlen' main.c:34: error: `argv' undeclared here (not in a function) main.c:34: error: `i' undeclared here (not in a function) main.c:34: warning: initialization makes pointer from integer without a cast main.c:34: error: initializer element is not constant main.c:34: warning: data definition has no type or storage class main.c:36: error: syntax error before "if" main.c:39: error: syntax error before string constant main.c:39: warning: type defaults to `int' in declaration of `sprintf' main.c:39: warning: function declaration isn't a prototype main.c:39: warning: conflicting types for built-in function 'sprintf' main.c:39: warning: data definition has no type or storage class main.c:40: warning: type defaults to `int' in declaration of `free' main.c:40: warning: parameter names (without types) in function declaration main.c:40: warning: data definition has no type or storage class main.c:41: error: syntax error before '&' token main.c:41: warning: type defaults to `int' in declaration of `test_parametres' main.c:41: warning: function declaration isn't a prototype main.c:41: warning: data definition has no type or storage class main.c:42: error: syntax error before string constant main.c:42: warning: type defaults to `int' in declaration of `printf' main.c:42: warning: function declaration isn't a prototype main.c:42: warning: data definition has no type or storage class main.c:43: error: syntax error before '--' token main.c:44: error: syntax error before string constant main.c:44: warning: type defaults to `int' in declaration of `printf' main.c:44: warning: function declaration isn't a prototype main.c:44: warning: data definition has no type or storage class main.c:56: warning: type defaults to `int' in declaration of `nom' main.c:56: error: redefinition of 'nom' main.c:34: error: previous definition of 'nom' was here main.c:56: error: redefinition of 'nom' main.c:34: error: previous definition of 'nom' was here main.c:56: warning: initialization makes pointer from integer without a cast main.c:56: error: initializer element is not constant main.c:56: warning: data definition has no type or storage class main.c:58: error: syntax error before "if" main.c:61: error: syntax error before string constant main.c:61: warning: type defaults to `int' in declaration of `sprintf' main.c:61: warning: function declaration isn't a prototype main.c:61: warning: data definition has no type or storage class main.c:62: error: syntax error before string constant main.c:62: warning: type defaults to `int' in declaration of `printf' main.c:62: warning: function declaration isn't a prototype main.c:62: warning: data definition has no type or storage class main.c:74: error: syntax error before '&' token main.c:74: warning: type defaults to `int' in declaration of `test_parametres' main.c:74: warning: function declaration isn't a prototype main.c:74: warning: data definition has no type or storage class main.c:75: error: syntax error before string constant main.c:75: warning: type defaults to `int' in declaration of `printf' main.c:75: warning: function declaration isn't a prototype main.c:75: warning: data definition has no type or storage class main.c:76: error: syntax error before '--' token main.c:77: error: syntax error before string constant main.c:77: warning: type defaults to `int' in declaration of `printf' main.c:77: warning: function declaration isn't a prototype main.c:77: warning: data definition has no type or storage class Process terminated with status 1 (0 minutes, 6 seconds) 28 errors, 46 warnings
On a pas que ça à faire d'essayer de deviner ce qui manque. Poste du code compilable, ou alors ne perd pas ton temps à demander de l'aide. On est pas tes esclaves...
:oops: :oops: désolé mes doigts on fourché..... :?Citation:
Envoyé par Foobar1329
J'ai corrigé....
Bien, je reposte donc mon code...
video.c:
video.h:Code:
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 #include "video.h" /** **Cette fontion permet de vérifier qu'il y a bien le nombre de paramètres nécessaires **à l'initialisation de la vidéo **/ void test_parametres(char **a, char *b){ if(b==NULL){ printf("Erreur de chargement sur %s\n", *a);} else{ *a=b; } } /** **Permet de composer la commande qui permet de déclencher la video. **/ char *commande_acquisition(char *param[NB_PARAM]){ char *commandeLog; sprintf(commandeLog, "streamer -t %s -s %sx%s -r 24 -o %s.yuv -c %s",param[0], param[1], param[2],param[3],param[4]); printf("Commande exécutée: '%s'\n", commandeLog); return commandeLog; } /* Fonction main qui permet de créer un fichier vidéo */ int main(int argc,char *argv[]){ int j=4; char* nom=NULL; char *tmp=NULL; for(i=argc-1; i>0; i--){ printf("i: %d\n", i); if(j==4){ printf("Parametre lu: %s\n", argv[i]); if(strncmp(argv[i],"/dev/",5)==0){ test_parametres(¶metres[j],argv[i]); printf("parametre defini n°%d: %s\n", j, parametres[j]); j--; printf("j: %d\n",j); } else{ } } else if(j==3){ printf("ici"); if(strncmp(argv[i],"/mnt/",5)==0){ *tmp = realloc(nom, sizeof(char*)); if(tmp!=NULL){ strcpy(tmp,nom); *nom=realloc(nom, (strlen(argv[i])+strlen(tmp)+2)); if(nom!=NULL){ strcpy(nom,argv[i]); sprintf(nom,"%s%s",tmp); free(tmp); test_parametres(¶metres[j],nom); printf("parametre defini n°%d: %s\n", j, parametres[j]); j--; printf("j: %d\n",j); } } } else{ *tmp = realloc(nom, sizeof(char*)); if(tmp!=NULL){ strcpy(tmp,nom); *nom=realloc(nom, (strlen(argv[i])+strlen(tmp)+2)); if(nom!=NULL){ strcpy(nom,argv[i]); sprintf(nom,"%s%s",tmp); printf("Construction nom: %s\n", nom); } } } } else{ free(nom); test_parametres(¶metres[j],argv[i]); printf("parametre defini n°%d: %s\n", j, parametres[j]); j--; printf("j: %d\n",j); } } return 0; }
la ligne de commande à tester est:Code:
1
2
3
4
5
6 #include <stdio.h> #include <string.h> #define NB_PARAM 5 int i; char *parametres[NB_PARAM]={"duree", "width", "height", "nomDossier", "device"};
Code:
1
2 ./video 00:01:00 320 240 /mnt/My U3 drive /dev/video0
Et autre question:
POur compiler je fais:
Je n'ai pas autant de ligne dans mon compilateur... Je dois utiliser differemment le compilateur (i.e avec d'autres options?)Code:
1
2 gcc main.c -o main
Bon ben on a une déjà belle erreur :
Ton pointeur commandeLog n'est pas initialisé, tu écris dans la nature, pas étonnant que ça plante !Code:
1
2
3
4
5
6
7
8
9
10 char *commande_acquisition(char *param[NB_PARAM]){ char *commandeLog; sprintf(commandeLog, "streamer -t %s -s %sx%s -r 24 -o %s.yuv -c %s",param[0], param[1], param[2],param[3],param[4]); printf("Commande exécutée: '%s'\n", commandeLog); return commandeLog; }
Il faut que tu regardes si tu disposes d'un sprintf sécurisé qui ne dépasse pas la taille du buffer de 256 caractères.Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 char *commande_acquisition(char *param[NB_PARAM]){ // au hasard un peu, il faut voir grand char *commandeLog = malloc(256); // on teste le retour if (commandeLog != NULL) { sprintf(commandeLog, "streamer -t %s -s %sx%s -r 24 -o %s.yuv -c %s",param[0], param[1], param[2],param[3],param[4]); printf("Commande exécutée: '%s'\n", commandeLog); } else fprintf(stderr, "Pb alloc mémoire\n"); return commandeLog; }
Tiens pour bien configurer ton compilateur ;)Citation:
Envoyé par Flophx
http://emmanuel-delahaye.developpez....tm#cfg_compilo
OUi, celle là j ela connaissais, je l'avais pas encore corrigée; Mais dans le main cette fonction n'apparait pas!!