popen

Vasilis Vasaitis vvas at hal.csd.auth.gr
Tue Apr 20 00:58:49 EEST 1999


On Sun, Apr 18, 1999 at 11:09:53PM +0300, Mavroyanopoulos Nikos wrote:
> Η libc λέει αυτό: 
>   "In the event of an error, `Popen' returns a null pointer.  This
>    might happen if the pipe or stream cannot be created, if the
>    subprocess cannot be forked, or if the program cannot be executed."
>                                           ^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> το popen ομως αρνείται να επιστρέψει null οταν το
> εκτελέσιμο που καλώ δεν υπάρχει... υπάρχει καποιος τρόπος να το
> παρακάμψω χωρίς να καλέσω τα fork, pipe,exec κλπ κλπ;

  Arxika vlepontas kai dokimazontas to programma sou mou fanike kai
emena parakseno na epistrefei ena stream kai oxi NULL s' auti tin
periptosi. Omos oi ennoies edo einai leptes. As arxisoume apo to eksis
(ego tha xrisimopoiiso to `man popen' afou teinei na einai pio akrives,
an kai ligotero didaktiko, apo to `info libc'):

       The  command  argument  is  a pointer to a null-terminated
       string containing a shell command line.  This  command  is
       passed  to  /bin/sh  using the -c flag; interpretation, if
       any, is performed by the shell.

  Tora auto mas leei oti i ektelesi tou programmatos tha theorithei epitixis,
an kai mono an i ektelesi tou shell einai epitixis. Apo ekei kai pera,
to ti tha kanei to shell me auti tin entoli einai thema diko tou kai tou
programmatos sou, pantos oxi tis popen() (alloste den iparxei asfalis
tropos na elegksei to apotelesma). Epomenos, pote i popen epistrefei
lathos? Stis eksis periptoseis:

       The popen function returns NULL if the fork(2) or  pipe(2)
       calls fail, or if it cannot allocate memory.

  Edo vevaia den anaferei tipota gia to ti ginetai an apotixei na
ektelesei to shell. To eksigei ligo parakato, sta bugs:

       Failure to execute the shell is indistinguishable from the
       shell's failure to execute command, or an  immediate  exit
       of the command.  The only hint is an exit status of 127.

> Το πρόγραμμα κάπως έτσι. To πρόβλημα είναι ότι θέλω να ξέρω άν υπήρξε
> λάθος ή όχι κατά την εκτέλεση. (αν καλέσω το perror βγάζει success)

  Mia apli lisi pou skeftika ego einai amesos meta tin klisi tis popen
na elegxeis an kapoio child process exei epistrepsei, opote simainei oti
kati pige strava. Auto to kritirio gia na xrisimopoiithei me sigouria
proipothetei oti den exeis alla paidia s' auto to simeio tou programmatos.

+ #include <sys/types.h>
+ #include <sys/wait.h>
> #include <stdio.h>
> 
> main() {
> char command[30];
> FILE *FROMF;
> 
>   strcpy(command, "/etc/gzip");
> 
>   FROMF=popen(command ,"r");
-   if (FROMF==NULL) {printf("Why?");}
+   if (FROMF==NULL || waitpid(-1, NULL, WNOHANG)==0) fprintf(stderr, "Why?");
> 
>   fprintf(stderr, "i'm invicible!\n"); 
>   fflush(stderr); /* Aυτό πού στο καλό πήγε? */
> 
> }

  To programma auto dinei ta parakato apotelesmata sto sistima mou:

mod at arakis:~ $ ./popen ; sleep 1
Why?i'm invicible!
sh: /etc/gzip: No such file or directory
mod at arakis:~ $ _

  Elpizo na voithisa...

> -- 
> Nikos Mavroyanopoulos
> mailto:nmav at hellug.gr
> http://sg1.math.uoi.gr/~ma06205

-- 
Vasilis Vasaitis
vvas at hal.csd.auth.gr

--
====================================================================
Gia boithia (h na diagrafhte) e-mail sto majordomo at hellug.gr
Ta archives tis listas einai sto http://lists.hellug.gr/archives
prin steilete kapoia erothsh psakte mipos exei hdh apanththei.
Gia opoiodipote problima stilte e-mail ston owner-linux-greek-users at hellug.gr
====================================================================



More information about the Linux-greek-users mailing list