popen ()

popen ()

Post by Surya Kir » Fri, 11 Oct 2002 18:52:03



Hi,
Please look at the following code snippet...
===========================================================================
int main(void)
{
   char *s;
   FILE *fp ;
   char cmd [256];
   char buf[100] = "No processes running\n" ;

   sprintf (cmd, "\\ps -ef | \\grep netscape 2>/dev/null") ;
   fp = popen (cmd, "r") ;

   if (!fp) exit (1);

   do
   {
      s = fgets (buf, sizeof (buf), fp);
      printf ("%s", buf) ;
   }while (s);

   return 0 ;

Quote:}

========================================================================
apparently i'm trying to find out the processes with name netscape...
but if i dont find any process i'm printing an error "NO PROCESS
RUNNING". this i could achieve by directing the output to /dev/null.
now my question is what is the similar way of doing it on windows

Thanks in advance,
Surya

 
 
 

popen ()

Post by Dan Merc » Fri, 11 Oct 2002 22:47:42




> Hi,
> Please look at the following code snippet...
> ===========================================================================
> int main(void)
> {
>    char *s;
>    FILE *fp ;
>    char cmd [256];
>    char buf[100] = "No processes running\n" ;

>    sprintf (cmd, "\\ps -ef | \\grep netscape 2>/dev/null") ;

Just curious - why are you escaping ps and grep?

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

Quote:>    fp = popen (cmd, "r") ;

>    if (!fp) exit (1);

>    do
>    {
>       s = fgets (buf, sizeof (buf), fp);
>       printf ("%s", buf) ;
>    }while (s);

>    return 0 ;
> }
> ========================================================================
> apparently i'm trying to find out the processes with name netscape...
> but if i dont find any process i'm printing an error "NO PROCESS
> RUNNING". this i could achieve by directing the output to /dev/null.
> now my question is what is the similar way of doing it on windows

> Thanks in advance,
> Surya


 
 
 

popen ()

Post by Jens.Toerr.. » Fri, 11 Oct 2002 23:00:48



> Please look at the following code snippet...
> ===========================================================================
> int main(void)
> {
>    char *s;
>    FILE *fp ;
>    char cmd [256];
>    char buf[100] = "No processes running\n" ;
>    sprintf (cmd, "\\ps -ef | \\grep netscape 2>/dev/null") ;
>    fp = popen (cmd, "r") ;
>    if (!fp) exit (1);
>    do
>    {
>       s = fgets (buf, sizeof (buf), fp);
>       printf ("%s", buf) ;
>    }while (s);
>    return 0 ;
> }
> ========================================================================
> apparently i'm trying to find out the processes with name netscape...
> but if i dont find any process i'm printing an error "NO PROCESS
> RUNNING". this i could achieve by directing the output to /dev/null.
> now my question is what is the similar way of doing it on windows

Why do you need the '\\' in the command string? BTW, you don't need
the cmd[] array, you can pass the command string also directly to
popen().

There's a subtle problem with your code: if you're unlucky your
program will also print out the processes you just started for doing
ps and grep, giving you false positives. I usually get:

jens      3592  3591  0 15:41 pts/4    00:00:00 sh -c \ps -ef | \grep netscape
2>/dev/null
jens      3594  3592  0 15:41 pts/4    00:00:00 grep netscape
jens      3594  3592  0 15:41 pts/4    00:00:00 grep netscape

You could remove these unwanted lines if you would change your command
to

popen( "ps -ef | grep netscape | grep -v grep 2>/dev/null", "r" );

The redirection of stderr to /dev/null does, by the way, seem to
be unnecessary.

Finally, the loop isn't quite ok, even when there are no more lines
you still print out buf, repeating the very last line. You might
change this by using

#include <stdio.h>
#include <unistd.h>

int main( void )
{
   char *s;
   FILE *fp ;
   int got_line = 0;
   char buf[ 100 ] = "No processes running\n";

   fp = popen ( "ps -ef | grep netscape | grep -v grep", "r");

   if ( ! fp )
      return 1;

   while ( fgets( buf, sizeof buf, fp ) )
   {
      printf ( "%s", buf );
      got_line = 1;
   }

   if ( ! got_line )
      printf( "%s", buf );

   return 0;

Quote:}

Concerning your question about how to do this under windows I would
guess that you better ask in a windows related group, I assume that
you will have to use some quite different methods under windows and
you can't expect people in comp.unix.programmer to also be experts
for windows programming ;-)
                                     Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring
 
 
 

popen ()

Post by Dan Merc » Sat, 12 Oct 2002 04:08:19





>> Please look at the following code snippet...
>> ===========================================================================
>> int main(void)
>> {
>>    char *s;
>>    FILE *fp ;
>>    char cmd [256];
>>    char buf[100] = "No processes running\n" ;

>>    sprintf (cmd, "\\ps -ef | \\grep netscape 2>/dev/null") ;
>>    fp = popen (cmd, "r") ;

>>    if (!fp) exit (1);

>>    do
>>    {
>>       s = fgets (buf, sizeof (buf), fp);
>>       printf ("%s", buf) ;
>>    }while (s);

>>    return 0 ;
>> }
>> ========================================================================
>> apparently i'm trying to find out the processes with name netscape...
>> but if i dont find any process i'm printing an error "NO PROCESS
>> RUNNING". this i could achieve by directing the output to /dev/null.
>> now my question is what is the similar way of doing it on windows

> Why do you need the '\\' in the command string? BTW, you don't need
> the cmd[] array, you can pass the command string also directly to
> popen().

> There's a subtle problem with your code: if you're unlucky your
> program will also print out the processes you just started for doing
> ps and grep, giving you false positives. I usually get:

> jens      3592  3591  0 15:41 pts/4    00:00:00 sh -c \ps -ef | \grep netscape
> 2>/dev/null
> jens      3594  3592  0 15:41 pts/4    00:00:00 grep netscape
> jens      3594  3592  0 15:41 pts/4    00:00:00 grep netscape

> You could remove these unwanted lines if you would change your command
> to

> popen( "ps -ef | grep netscape | grep -v grep 2>/dev/null", "r" );

> The redirection of stderr to /dev/null does, by the way, seem to
> be unnecessary.

> Finally, the loop isn't quite ok, even when there are no more lines
> you still print out buf, repeating the very last line. You might
> change this by using

> #include <stdio.h>
> #include <unistd.h>

> int main( void )
> {
>    char *s;
>    FILE *fp ;
>    int got_line = 0;
>    char buf[ 100 ] = "No processes running\n";

>    fp = popen ( "ps -ef | grep netscape | grep -v grep", "r");

Or just

fp = popen ( "ps -ef | grep '[n]'etscape", "r");

You should also pclose popened pipes to avoid zombies.

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

- Show quoted text -

>    if ( ! fp )
>       return 1;

>    while ( fgets( buf, sizeof buf, fp ) )
>    {
>       printf ( "%s", buf );
>       got_line = 1;
>    }

>    if ( ! got_line )
>       printf( "%s", buf );

>    return 0;
> }

> Concerning your question about how to do this under windows I would
> guess that you better ask in a windows related group, I assume that
> you will have to use some quite different methods under windows and
> you can't expect people in comp.unix.programmer to also be experts
> for windows programming ;-)
>                                      Regards, Jens
> --
>       _  _____  _____

>   _  | |  | |    | |
> | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
>   \___/ens|_|homs|_|oerring

Opinions expressed herein are my own and may not represent those of my employer.
 
 
 

1. why must child in popen() close remaining popen()ed fds?

Could someone explain why the child in popen() must close the remaining
popened() file descriptors before it execs?  Or perhaps you could point
me to a good online reference?

Thanks,
Matt

--
                --------------------------------------

                |  http://www.faradic.net/~mmcclure  |
                --------------------------------------

2. AIX Informix Install Question???

3. can you rewrite popen()? (or run popen from a different libc)

4. Apache 1.1.1 - LITTLE_ENDIAN redefined

5. how to popen telnet ???

6. Linux on TOSHIBA 1950 notebook

7. popen(3) and SIGPIPE

8. Incoming PPP and modem problems

9. call to popen never returns..

10. C++ iostream popen equivalent?

11. Making a popen() replacement with vfork()

12. getting error output from popen()

13. popen issue