LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-28-2005, 07:21 AM   #1
Miaire
LQ Newbie
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Fedora Core 3
Posts: 11

Rep: Reputation: 0
in C, Assigning output of system() to a variable


I'm new to C and Linux and I am confused on how things work. I tried to search but I don't really know if I'm searching for the right thing because lots of other things come up.

Currently, I figured out that I can use shell commands inside C by using the method system(). They display out on the screen fine, but my problem is that I need to assign them to a variable, and not have them display on the screen (yet).

I'm planning to put the output of "ls -l" to a variable. I'm thinking of an array of strings (I think there are no strings, but an array char, so I guess this is an array of array of chars?) which will contain every line of the "ls -l" command except for the first line. (I think I need it in an array because I'm supposed to scroll through it in a menu-like style where I'm following example 18 on this link:
http://www.tldp.org/HOWTO/NCURSES-Pr...tml#MENUBASICS
but I think it's another question for another time, hopefully I can figure it out and not have to though, hahah)

What I have is this, but it doesn't work.

Code:
#include <stdlib.h>
int main() {
   char output[] = system( "ls -l" );
   printf( "%s", output);
   return 0;
}
which gives this error.
Code:
system.c: In function 'main':
system.c:5: error: invalid initializer
I believe it is wrong, but I'm completely clueless. Perhaps you can help, thanks.

Last edited by Miaire; 01-28-2005 at 07:29 AM.
 
Old 01-28-2005, 07:27 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Two things. If a function returns a pointer-to-char (dynamically allocated or static) and you want to assign the return value to a variable you do:
Code:
char *ptr = some_func_returning_pointer_to_char();
Second, system() doesn't return char* it returns int. Do a:
Code:
$ man 3 system
for more details on the return value.

The system() call is part of standard C (and C++ of course) but how it operates and what you can do with the processes it spawns is platform-dependent.
 
Old 01-28-2005, 08:29 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
man popen

I think this does what you are looking for.
 
Old 01-28-2005, 01:39 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
popen example:
Code:
#include <stdio.h>               
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void process(const char *filespec)
{
	char cmd[296]={0x0}; /* 40 + 256 */  
	char tmp[256]={0x0};
	long total=0;
	int retval=0;
	FILE *in=NULL;
	
    snprintf(cmd,sizeof(cmd)-40,"ll %s | /usr/bin/cut -c 35-45 2>/dev/null",filespec);
    in=popen(cmd, "r");
    if(in==NULL)
    {
         perror("Shell execution error");
         exit(EXIT_FAILURE);
    }           
    while(fgets(tmp,4095,in)!=NULL) 
    {
         total+=atol(tmp);
    }        
    if(!feof(in))
    {
        perror("Input stream error");
        exit(EXIT_FAILURE);
    }    
    retval=pclose(in);
    if(retval==EOF || retval==127)    
    {
        perror("Shell invocation error");
        exit(EXIT_FAILURE); 
    }    
    fprintf(stdout,"%-64s total: %d\n",filespec,total);
}

int main(int argc, char *argv[])
{
    if(argc<2)
    {
    	char filespec[256]={0x0};
    	while(fgets(filespec,sizeof(filespec),stdin)!=NULL)
    	{
    	    char *p=memchr(filespec,'\n', sizeof(filespec));
    		if(p!=NULL) *p=0x0;
    	    process(filespec);
    	}      	  
    }	
    else
    {      	
        int i=1;
        while(i<argc)
        {
            process(argv[i]);
            i++;	
        }
    }
    return 0;
}
 
Old 01-30-2005, 12:40 PM   #5
Miaire
LQ Newbie
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Fedora Core 3
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for the answers, I have gotten what I needed. Figured out char arrays (strings) and casting char to int in the process.

This is what I came up with:
Code:
#include <ncurses.h>
#include <stdio.h>

int main()
{
   int size=0;
   FILE *in=NULL;
   char temp[256];
   
   in=popen("ls -a -l | wc -l", "r");
   fgets(temp, 255, in);
   size = atoi(temp) - 1;

   char list[size][256];

   in=popen("ls -a -l", "r"); //for reading
   fgets(list[0], 255, in); // discard total: xxx, first line
  
   int i;
   for( i = 0; i < size; i++) {
      fgets(list[i], 255, in);   
   }

   for(i=0; i < size; i++) {
      printf("%s", list[i]);
   }
   return 0;
}
It does what I expect it to do (though it probably have hidden problems somewhere like memory things I don't really understand yet. Any comments on it?) , now to format and continue on with ncurses. Thanks for help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving the output of a file as a variable mrobertson Programming 5 07-08-2005 12:43 AM
A Simple n00b question - assigning your system a name TuxFreak Fedora 9 12-02-2004 04:40 PM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM


All times are GMT -5. The time now is 11:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration