Where to find resource on "how to analyze " system call output
-
I like to bypass some of the Bluetooth "resources" in my C / C++ code by using (direct) system calls. I can czech for return value, but like to analyze more of the output, in including errors. Where do I start ? Contrary to my opinion ( about RTFM ) - can somebody give me link to "RTFM" how to do it ?
system(" hcitool dev");
return to stdout (?)
Devices:
hci1 00:50:B6:80:4D:5D
hci0 00:15:83:15:A2:CB -
I like to bypass some of the Bluetooth "resources" in my C / C++ code by using (direct) system calls. I can czech for return value, but like to analyze more of the output, in including errors. Where do I start ? Contrary to my opinion ( about RTFM ) - can somebody give me link to "RTFM" how to do it ?
system(" hcitool dev");
return to stdout (?)
Devices:
hci1 00:50:B6:80:4D:5D
hci0 00:15:83:15:A2:CBRather than system(), you want to use popen(). The only problem is that popen() doesn't supply a separate FILE for stderr, so you have to use output redirection to capture both e.g.
FILE *cmd = popen("hcitool dev 2>&1", "r");
char *buffer = NULL;
size_t len = 0;
ssize_t readlen;
while ( (readlen = getline(&buffer, len, cmd)) > 0)
{
/* process input buffer */
}
free(buffer);
pclose(cmd);You could perhaps also use output redirection to capture output separately e.g.
FILE *cmd = popen("hcitool dev 2>/tmp/hcierrs", "r");
/* process input as above */
FILE *cmd_errs = fopen("/tmp/hcierrs", "r");
/* loop through input from cmd_errs ... */
unlink("/tmp/hcierrs");If you're going to do that, you might want to look at creating unique temporary file names, so that you don't clobber output if you happen to have more than one instance of the program running at the same time.
mkstemp()
can help you here. For the really advanced, you might look into trying "roll your own" version of popen that usesfork()
and one of theexec()
functions to separate out stdout and stderr to two separate FILES.Keep Calm and Carry On
-
Rather than system(), you want to use popen(). The only problem is that popen() doesn't supply a separate FILE for stderr, so you have to use output redirection to capture both e.g.
FILE *cmd = popen("hcitool dev 2>&1", "r");
char *buffer = NULL;
size_t len = 0;
ssize_t readlen;
while ( (readlen = getline(&buffer, len, cmd)) > 0)
{
/* process input buffer */
}
free(buffer);
pclose(cmd);You could perhaps also use output redirection to capture output separately e.g.
FILE *cmd = popen("hcitool dev 2>/tmp/hcierrs", "r");
/* process input as above */
FILE *cmd_errs = fopen("/tmp/hcierrs", "r");
/* loop through input from cmd_errs ... */
unlink("/tmp/hcierrs");If you're going to do that, you might want to look at creating unique temporary file names, so that you don't clobber output if you happen to have more than one instance of the program running at the same time.
mkstemp()
can help you here. For the really advanced, you might look into trying "roll your own" version of popen that usesfork()
and one of theexec()
functions to separate out stdout and stderr to two separate FILES.Keep Calm and Carry On
What is a software development company? Software development is a process where software is developed for real-time use from start to finish. Software development is used by many companies around the globe to put together software solutions that make their business run as smoothly as possible. Software development companies can help companies develop everything from custom software applications for mobile and web applications to custom software solutions for enterprise, e-commerce, and web-based business. Some of the worlds biggest companies even depend on the experience of dedicated software development companies to develop software and apps for their mobile and web applications and for their enterprise, e-commerce, and web-based business. I recommend you contact syndicode.com