Lancer appl depuis Terminal dans programme c

Résolu
Bonjour à tous,

Lancer un programme comme FF dans le Terminal c'est facile, il suffit d'écrire : $ firefox.
Mais dans mon cas, c'est différent, je compile et execute un fichier.c dans le Terminal, qui demande à l'utilisateur de rentrer un message. J'aimerais après avoir rentré firefox, que le navigateur se lance. (Pour l'exemple de FF).

Comment puis-je faire ?

Configuration : Ubuntu 10.10.

Cordialement.

3 réponses

  1. Modérateur
    Tu peux utiliser la fonction popen.

    man popen


    Si le processus est invoqué avec succès, pense à faire un pclose.

    Bonne chance
    1. J'ai regardé popen avec le man, mais je vois pas trop comment l'utiliser.

      En fait je voudrais faire ça :
      // Lancer une application
      if(strcmp(question, "Lance Firefox.\n") == 0)
      {
      printf("firefox\n");
      }


      Comment je peux intégrer popen dedans ?

      Merci d'avance.
      1. Modérateur
        Ben qu'est ce qui te bloque ?

        #include <stdio.h>
        
        int main(){
            FILE *fp = popen("ls -al /","w");
            if(!fp){
                fprintf(stderr,"erreur");
                return 1;
            }
            printf("processus lancé avec succès !\n");
            pclose(fp);
            return 0;
        }


        Ce qui donne :

        (mando@aldur) (~) $ gcc plop.c 
        (mando@aldur) (~) $ ./a.out 
        processus lancé avec succès !
        total 128
        drwxr-xr-x  21 root root  4096  7 déc.  19:02 .
        drwxr-xr-x  21 root root  4096  7 déc.  19:02 ..
        drwxr-xr-x   2 root root  4096  4 déc.  16:30 bin
        drwxr-xr-x   3 root root  4096  4 déc.  16:35 boot
        lrwxrwxrwx   1 root root    11  3 juin   2007 cdrom -> media/cdrom
        drwxr-xr-x  15 root root  3640  7 déc.  18:29 dev
        drwxr-xr-x 141 root root 12288  7 déc.  18:33 etc
        drwxr-xr-x   9 root root  4096  2 nov.  17:14 home
        drwxr-xr-x   2 root root  4096  3 juin   2007 initrd
        lrwxrwxrwx   1 root root    28 14 juin  22:53 initrd.img -> boot/initrd.img-2.6.32-5-686
        drwxr-xr-x  13 root root 12288  4 déc.  16:30 lib
        drwx------   2 root root 16384  3 juin   2007 lost+found
        drwxr-xr-x   5 root root  4096  7 déc.  18:29 media
        drwxr-xr-x  12 root root  4096 12 mai    2010 mnt
        dr-xr-xr-x 127 root root     0  7 déc.   2010 proc
        drwxr-xr-x  27 root root  4096  7 déc.  19:02 root
        drwxr-xr-x   2 root root  4096  4 déc.  16:31 sbin
        drwxr-xr-x   2 root root  4096  7 mai    2007 selinux
        drwxr-xr-x   3 root root  4096 17 nov.   2008 srv
        drwxr-xr-x  12 root root     0  7 déc.   2010 sys
        drwxrwxrwt  11 root root 32768  7 déc.  19:02 tmp
        drwxr-xr-x  11 root root  4096 19 sept.  2009 usr
        drwxr-xr-x  15 root root  4096 25 nov.   2009 var
        lrwxrwxrwx   1 root root    25 14 juin  22:53 vmlinuz -> boot/vmlinuz-2.6.32-5-686


        Bonne chance
        1. Merci, ça marche impeccable. J'avais oublié l'étoile devant la définition du processus. Maintenant, je dois chercher comment lancer le processus en premier plan. ;-)
        2. Modérateur
          Tu as d'autres alternatives à popen comme execve. Je n'ai pas très bien compris ce que tu appelais "au premier plan" ? Tu parles en terme de processus (commande fg) ou au sens graphique ?
        3. Ah ouais, je vais aller fouiner sur "execve". Au sens graphique, voilà.
        4. Modérateur
          Ouvre un nouveau fil de discussion on va chercher.