linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* command output to variables
@ 2009-01-07 12:35 Jai Sharma
  2009-01-07 12:54 ` Xin Zou
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jai Sharma @ 2009-01-07 12:35 UTC (permalink / raw)
  To: linux-c-programming

Hi All

I m PHP, Perl programmer , but finally I need C to program in LInux.
In perl I simply use `` operator to get command output.

$who_output = `who` ;


but in C , how can i get the output. Using system function i m easily
run the command. But how to get its output .

system("who");

Plz help

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: command output to variables
  2009-01-07 12:35 command output to variables Jai Sharma
@ 2009-01-07 12:54 ` Xin Zou
  2009-01-07 13:33 ` Michele Mondelli
  2009-01-07 14:31 ` Bert Wesarg
  2 siblings, 0 replies; 6+ messages in thread
From: Xin Zou @ 2009-01-07 12:54 UTC (permalink / raw)
  To: Jai Sharma; +Cc: linux-c-programming

It's not easy to get output in C.
You can use pipe to get the result from standard output.

On Wed, Jan 7, 2009 at 8:35 PM, Jai Sharma <jai.unix@gmail.com> wrote:
> Hi All
>
> I m PHP, Perl programmer , but finally I need C to program in LInux.
> In perl I simply use `` operator to get command output.
>
> $who_output = `who` ;
>
>
> but in C , how can i get the output. Using system function i m easily
> run the command. But how to get its output .
>
> system("who");
>
> Plz help
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: command output to variables
  2009-01-07 12:35 command output to variables Jai Sharma
  2009-01-07 12:54 ` Xin Zou
@ 2009-01-07 13:33 ` Michele Mondelli
  2009-01-07 14:59   ` Jai Sharma
  2009-01-10 14:57   ` Jason
  2009-01-07 14:31 ` Bert Wesarg
  2 siblings, 2 replies; 6+ messages in thread
From: Michele Mondelli @ 2009-01-07 13:33 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 188 bytes --]

A simple example of using pipe in Linux.

Regards


--
Homepage: http://www.mithenks.com
Slackware GNU/Linux User since 2003
Registered Linux User #383379
Registered Linux Machine #391361

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pipe.c --]
[-- Type: text/x-csrc; name=pipe.c, Size: 439 bytes --]

/*
 * Run the 'ls' command and print the output to stdout.
 * 
 * Compile with 
 * 		gcc -Wall pipe.c -o pipe
 * 
 * 
 * Mithenks <mithenks@siena.linux.it>
 * 
 */
#include <stdio.h>
#include <stdlib.h>

int main() {
	
	FILE *pipe;
	char buffer[1024];
	
	if ( (pipe = popen("ls","r") ) < 0 ) {
		perror("popen(): ");
		exit(-1);
	}
	
	
	fread(buffer,sizeof(buffer),1,pipe);
	printf("Output:\n%s\n",buffer);
	
	pclose(pipe);
	
	return 0;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: command output to variables
  2009-01-07 12:35 command output to variables Jai Sharma
  2009-01-07 12:54 ` Xin Zou
  2009-01-07 13:33 ` Michele Mondelli
@ 2009-01-07 14:31 ` Bert Wesarg
  2 siblings, 0 replies; 6+ messages in thread
From: Bert Wesarg @ 2009-01-07 14:31 UTC (permalink / raw)
  To: Jai Sharma; +Cc: linux-c-programming

On Wed, Jan 7, 2009 at 13:35, Jai Sharma <jai.unix@gmail.com> wrote:
> Hi All
>
> I m PHP, Perl programmer , but finally I need C to program in LInux.
> In perl I simply use `` operator to get command output.
>
> $who_output = `who` ;
>
>
> but in C , how can i get the output. Using system function i m easily
> run the command. But how to get its output .
>
> system("who");
use popen("who", "r") and read from the stream into a buffer

Bert
>
> Plz help
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: command output to variables
  2009-01-07 13:33 ` Michele Mondelli
@ 2009-01-07 14:59   ` Jai Sharma
  2009-01-10 14:57   ` Jason
  1 sibling, 0 replies; 6+ messages in thread
From: Jai Sharma @ 2009-01-07 14:59 UTC (permalink / raw)
  To: Michele Mondelli; +Cc: linux-c-programming

Hi ALL

What I think is,  C is hard to learn, but u guys r great.
I hope i will enjoy learning  C with u people,

Thanks and Regards

Jai

On Wed, Jan 7, 2009 at 7:03 PM, Michele Mondelli <mithenks.ml@gmail.com> wrote:
> A simple example of using pipe in Linux.
>
> Regards
>
>
> --
> Homepage: http://www.mithenks.com
> Slackware GNU/Linux User since 2003
> Registered Linux User #383379
> Registered Linux Machine #391361
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: command output to variables
  2009-01-07 13:33 ` Michele Mondelli
  2009-01-07 14:59   ` Jai Sharma
@ 2009-01-10 14:57   ` Jason
  1 sibling, 0 replies; 6+ messages in thread
From: Jason @ 2009-01-10 14:57 UTC (permalink / raw)
  To: Michele Mondelli, jai.unix; +Cc: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 1573 bytes --]

Michele Mondelli wrote:
> A simple example of using pipe in Linux.
>
> /*
>  * Run the 'ls' command and print the output to stdout.
>  *
>  * Compile with
>  * 		gcc -Wall pipe.c -o pipe
>  *
>  *
>  * Mithenks <mithenks@siena.linux.it>
>  *
>  */
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> 	
> 	FILE *pipe;
> 	char buffer[1024];
> 	
>	if ( (pipe = popen("ls","r") ) < 0 ) {

This should be:
	if ( (pipe = popen("ls","r") ) == NULL ) {

> 		perror("popen(): ");
> 		exit(-1);
>	}
> 	
> 	

Don't forget to clear the buffer, printf requires NULL terminated strings.
	memset(buffer, 0, sizeof(buffer));

> 	fread(buffer,sizeof(buffer),1,pipe);

In a real implementation, you'll be looping on fread(), and you'll want
to check the return value, like so:

	int rlen=0;
	if ( (rlen = fread(buffer,sizeof(buffer),1,pipe) ) != 1 ) {
		/*we have a short read or error*/
		if( feof(pipe) != 0 ) {
			printf("Finished\n");
			exit(0);
		}
		else if ( ferror(pipe) != 0 ) {
			printf("Error reading from pipe.\n");
			exit(-1);
		}
	}

> 	printf("Output:\n%s\n",buffer);
> 	
> 	pclose(pipe);
> 	
> 	return 0;
> }

So, that got a little messy, I've attached a re-write.  It's not the
most efficient thing in the world, but it should get you started.

When in doubt, 'man function_name'.  For example, 'man popen' told me I
should be checking for a NULL return value, and 'man fread' told me I
should be checking the number of items returned, _not_ the number of
bytes.  'man brain' returned NULL for me, so any errors are solely my
responsibility.  ;-)

hth,

Jason.

[-- Attachment #2: pipe.c --]
[-- Type: text/plain, Size: 1055 bytes --]

/*
 * Run the 'ls' command and print the output to stdout.
 * 
 * Compile with 
 * 		gcc -Wall pipe.c -o pipe
 * 
 * 
 * Mithenks <mithenks@siena.linux.it>
 * Changes added by Jason <lcprog@lakedaemon.net>
 * 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	
	FILE *pipe=NULL;
	char buffer[1024];
	int  rlen=0;
	int  blen=0;
	
	if ( (pipe = popen("ls","r") ) == NULL ) {
		perror("popen(): ");
		exit(-1);
	}

	/*we subtract 1 so there is always a NULL terminator*/
	blen = sizeof(buffer) - 1;

	while (1) { 
		memset(buffer,0,sizeof(buffer));

		if ( (rlen = fread(buffer,blen,1,pipe) ) != 1 ) {
			/*we have a short read or an error*/
			if ( feof(pipe) != 0 ) {
				fprintf(stderr,"Finished");
				printf("%s",buffer);
				break;
			}
			else if ( ferror(pipe) != 0 ) {
				fprintf(stderr,"Error reading from pipe.\n");
				break;
			}
			else {
				/*short read*/
				printf("%s",buffer);
			}
		}
		else {
			/*we got a full buffer*/
			printf("%s",buffer);
		}
	}

	if(pipe != NULL) {	
		pclose(pipe);
	}
	
	exit(0);
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-01-10 14:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-07 12:35 command output to variables Jai Sharma
2009-01-07 12:54 ` Xin Zou
2009-01-07 13:33 ` Michele Mondelli
2009-01-07 14:59   ` Jai Sharma
2009-01-10 14:57   ` Jason
2009-01-07 14:31 ` Bert Wesarg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).