ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I want to be able to read the output from a command invoked from with in a C program. I'm not very well versed on this topic so any help would be appreciated.
Here's a popen() example I made a while ago.
It slow-scrolls man pages (or info pages if man page not found) by executing tha "man" command with popen(), reading it's output (to stdout) line-by-line in a delayed loop.
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAXLINELEN 100
#define MAXCOMMAND 100
#define DISCARDSTDERR " 2>/dev/null"
static char *progname;
int slowdisplay(char *docprog, char *docname)
{
char line[MAXLINELEN + 1];
char command[MAXCOMMAND + 1];
FILE *pipe;
/*
* Construct a command line, (e.g. "man ls 2>/dev/null") with safety
* checks for string length
*/
strncpy(command, docprog, MAXCOMMAND);
command[MAXCOMMAND] = '\0';
if (strlen(command) + strlen(docname) + strlen(DISCARDSTDERR) >
MAXCOMMAND) {
fprintf(stderr, "Error: Command would be too long (max = %d).\n",
MAXCOMMAND);
return 0;
}
strcat(command, " ");
strcat(command, docname);
strcat(command, DISCARDSTDERR); /* we do not want to see the
* output to stderr */
/*
* Execute the command, setting up a pipe for getting its output.
*/
pipe = popen(command, "r");
if ((pipe = popen(command, "r")) == NULL) {
perror(progname);
return 0;
}
/*
* Read the doc from the pipe line-by-line, with a short
* delay between de lines for demonstration.
*/
while (fgets(line, MAXLINELEN + 1, pipe) != NULL) {
printf(line);
usleep(100000); /* Delay-per-line = 0.1 sec ( =
* 100 ms = 100000 us ) */
}
if (pclose(pipe) == -1)
return 0;
return 1;
}
int main(int argc, char *argv[])
{
if ((progname = strrchr(argv[0], '/')) == NULL)
progname = argv[0];
else
progname++;
if (argc != 2) {
fprintf(stderr, "Usage: %s <name of man or info page>\n", progname);
return 1;
}
/*
* First try man page
*/
if (slowdisplay("man", argv[1])) {
return 0; /* If successfull, end program */
}
/*
* else try info page
*/
if (!slowdisplay("info", argv[1])) {
fprintf(stderr,
"Document not found in neither man of info system.\n");
return 1; /* Not succesfull, return exit
* code 1 to indicate failure */
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.