1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
@(#) (c) 2010 C-Squared Computing - C2C
@(#) Created: Fri Sep 10 16:05:51 EDT 2010
@(#) Description: This is an example program that tests the
@(#) popen(3C) call.
@(#) History:
@(#) KEC 10 Apr 2010 - Creation.
*/
#include <iostream>
#include <string>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static const string
OptcDefault = "ps -ef",
OptdDefault = "0",
OptiDefault = "standard in",
Options = "c:d:hi:o:",
OptoDefault = "standard out",
Program = "TestPOpen";
static int Debug = 0;
void Help()
{
cout << "Usage: " << Program << " " <<
"[ -c command ] [ -d debug level ] [ -h ] " << endl <<
" [ -i input file ] [ -o output file ]" <<
endl;
cout << endl << "where (defaults if any are in parentheses):" << endl;
cout << " -c specifies a command to run " <<
"(" << OptcDefault << ")," << endl;
cout << " -d specifies the debug level " <<
"(" << OptdDefault << ")," << endl;
cout << " -h displays this help and exits," << endl;
cout << " -i specifies an input file " <<
"(" << OptiDefault << ")," << endl;
cout << " -o specifies an output file " <<
"(" << OptoDefault << ")," << endl;
}
/* Help() */
void RunTest( const string &Command, const string &InputFileName,
const string &OutputFileName );
int main( int argc, char **argv )
{
int
bContinue = true,
bProcessingCommandLine = true,
c = 0;
string
Command = "",
InputFileName = "",
OutputFileName = "";
while( bProcessingCommandLine )
{
c = getopt( argc, argv, Options.data() );
switch( c )
{
case EOF:
bProcessingCommandLine = false;
break;
case 'c':
Command = optarg;
break;
case 'd':
Debug = atoi( optarg );
break;
case 'h':
Help();
exit( 0 );
break;
case 'i':
InputFileName = optarg;
break;
case 'o':
OutputFileName = optarg;
break;
default:
break;
} /* switch( c ) */
} /* while( bProcessingCommandLine ) */
if( bContinue )
{
if( Command.empty() )
Command = OptcDefault;
RunTest( Command, InputFileName, OutputFileName );
} /* if( bContinue ) */
}
/* main() */
void RunTest( const string &Command, const string &InputFileName,
const string &OutputFileName )
{
FILE *fd = NULL;
char Buffer[ BUFSIZ ];
size_t pos = string::npos;
string InputLine = "";
fd = popen( Command.data(), "r" );
if( NULL != fd )
{
while( fgets( Buffer, sizeof( Buffer ), fd ) )
{
InputLine = Buffer;
cout << InputLine << endl;
} /* while( fgets() ) */
pclose( fd );
fd = NULL;
} /* if( NULL != fd ) */
else
{
cerr << "Error opening " << Command << endl;
} /* if( NULL == fd ) */
}
/* RunTest() */
|