LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 05-15-2010, 08:23 AM   #1
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
How to get string from system command output


I have found this from a site http://stackoverflow.com/questions/2...ut-as-a-string

Code:
#define _GNU_SOURCE
#include <stdio.h>
int main() {
    char *s = NULL;
    FILE *p = popen("ls", "r");
    getdelim(&s, NULL, '\0', p);
    pclose(p);
    printf("%s", s);
    return 0;
}
But it does not work for me, segfault. Any suggestions?

Regards,
archieval
 
Old 05-15-2010, 08:28 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Compiled and ran on my machine, although output was - (null)
 
Old 05-15-2010, 08:38 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
You should check the result of popen and getdelim. p can be null or getdelim could have returned 0, most likely resulting in s remaining null as well. Those functions do not return a value for nothing, it is for checking.

A wild guess: "ls" is not found as executable.

jlinkels
 
Old 05-15-2010, 08:44 AM   #4
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
I checked the return value, and it's Invalid argument. But "ls" is a valid unix command..
 
Old 05-15-2010, 08:48 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Try popen("/bin/ls", "r"); in case $PATH does not include /bin in the sh shell created to run popen()'s first argument.
 
Old 05-15-2010, 08:53 AM   #6
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
I did /bin/ls, but still invalid argument. And the getdelim returns (null).
 
Old 05-15-2010, 09:18 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
I'm only butting in here because progress seems slow not because I'm a C-spert. So, for what it is worth ...

Given that "/bin/ls" and "r" are definitely valid for popen it is unlikely that popen is failing but always good to test. How about adding error traps as jinkels suggested? Here's your code snippet modified with lines cribbed from the getdelim man page
Code:
#define _GNU_SOURCE
#include <stdio.h>
int main() {
    char *s = NULL;
    FILE *p;
    ssize_t read;
    p = popen("ls", "r");
    if (p == NULL)
        exit(10);
    read = getdelim(&s, NULL, '\0', p);
    if (read = -1)
        exit(20);
    pclose(p);
    printf("%s", s);
    return 0;
}
The exit code will let you know if either popen or getdelim are failing (not pretty but functional).

My un-ediccated guess is that the problem is the NULL argument to getdelim(); I think it should be a pointer to an initialised size_t variable indicating the maximum number of bytes that getdelim() should transfer to buffer.
 
Old 05-15-2010, 10:05 AM   #8
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
Yes, I have done that error checkings, the result is Invalid argument for popen, and (null) was printed after getdelim. Anyway, I just used fgets, to read from the stream. Thanks!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to output a string by using wildcards? fish-master Linux - Newbie 3 02-21-2010 03:03 PM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-04-2008 11:59 PM
System Command To Output CPU Type and RAM stranger_6_7 Linux - General 5 02-07-2007 03:08 AM
How to compare the output of a command with a string? daYz Programming 2 12-31-2006 09:36 AM
how to format the output of a command and save it into a string variable gn00kie Programming 1 07-15-2006 03:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration