C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2003, 04:18 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
Calling a shell command in C code

The code below worked. It showed all of the parameters of the shell command ifconfig. Now, I would like to stored its output in a variable as in
string_result = system(string1);
The above code does not work.

1) What command that I need to run to stored the result of a shell command in a variable in C program ?
======================================

#include <stdio.h>
#include <stdlib.h>

int system(const char *string1);

int main()
{
char string1[] = "ifconfig";

system(string1);
return(0);
}

Reply With Quote
  #2  
Old June 5th, 2003, 04:24 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 20 m 5 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
pipe the output of the command to a file and then read in the file.

Reply With Quote
  #3  
Old June 5th, 2003, 08:36 PM
lmolina lmolina is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Barranquilla - Colombia
Posts: 1 lmolina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Did you find the solution?

Hi, i have the same problem you posted. Would you please let me know when you find the solution?

I found this command: _system_r(), and it is supposed to work but when linking with gcc linker it returns "udefined reference to '_system_r'". Do you know why?

Please, stay in touch.

Thanx
Luis Molina

Reply With Quote
  #4  
Old June 6th, 2003, 01:15 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,824 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 1 h 9 m 26 sec
Reputation Power: 446
I tried answering earlier, but we had terrible access problems at work. As a result, the sample code I had written is lost. So now I'm at home and late for bed, so I'll be terse.

The piping should be done with the popen function call. You pass it a string with the command and the open option (just like fopen). Also like fopen, it returns a FILE* which you then use to fread or fgets just as you would from a text file on disk. The only difference would be that you use pclose to close the pipe when you are done.

For example:
FILE* fp = popen("ifconfig","r"); // open a shell and run ifconfig, then read its output.

The command string can also contain options and parameters.

That should get you started.

Reply With Quote
  #5  
Old June 6th, 2003, 06:44 PM
Net_Vicious Net_Vicious is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Barranquilla - Colombia
Posts: 1 Net_Vicious User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Solution

/* Hola, esta es una solucion que yo encontre a ese problema que tambien tube. */

#include<stdio.h>
#include<string.h>

main()
{

// FILE *popen(const char *orden, const char *tipo);
FILE *f;
int i;
char cadena;
char cad[300];

f = popen("ping -w 1 127.0.0.1 ","r"); //>> /root/TesisNetsaint/Pesca/result.txt

for ( i=0 ; i <=280 ; i++) // 280 = Size of the result
{
//printf( "%c", fgetc(f) );
cad[i] = fgetc(f) ;
} // Bueno!!!!

cad[i]= '\o' ;
printf("%s",cad);

if( strstr(cad,"0 packets received") ){
printf("Host DOWN");
}else{
printf("Host UP");
}
}

Reply With Quote
  #6  
Old June 6th, 2003, 07:26 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,824 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 1 h 9 m 26 sec
Reputation Power: 446
Re: Solution

Quote:
Originally posted by Net_Vicious
/* Hola, esta es una solucion que yo encontre a ese problema que tambien tube. */

...


cad[i]= '\o' ;

...


¿No debe ser cero? Como '\0'.

Shouldn't that be a zero, as in '\0'?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Calling a shell command in C code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway