Solved

Unix system command substitution in C

Posted on 2004-09-08
4
289 Views
Last Modified: 2010-04-15
Hi Experts,

I wanted to write events (errors, etc) relating to a program to a logfile using the Unix command while in a C program e.g. :

         case 1:        /* Key error message for shared memory */
       system("date >> $MYLOG  | echo Error in obtaining key to shared memory!(DCSF) >> $MYLOG");
       system("logger -f $MYLOG ");
       exit(1);

 Note that the $MYLOG is just to represent a logfile which in the actual case is the value of a C variable cLogFileName which is updated from a config file. (In this case, cLogFileName equals a value "../data/logfile01.dat" ). My question is how can I substitute the value of a C variable into the Unix shell command so that the Unix command appends to the physical file (in this case ../data/logfile01.dat) ??

Thanks in advance.

0
Comment
Question by:sbzainal
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 125 total points
ID: 12004647
you can build the command string for the system() call using snprintf(), which formats a string using the same formatters as printf...

char cmdbuf[512];
// can also use sprintf(), but snprintf() is safer...
snprintf( cmdbuf, sizeof( cmdbuf ), "logger -f %s", cLogFileName );
system( cmdbuf );

However, you might want to write a function that will open() the correct file, and write the information directly to the end of the file, rather than trying to wrap a shell command to echo information into a file...

look at fopen( filename, "a" ), fprintf() and fclose() (or even open(), write(), close() if you want to use the system calls rather than the wrapped File IO API)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12004652
Hi sbzainal,

use syslog()
http://www.die.net/doc/linux/man/man2/syslog.2.html

If you still insist on your method, use sprintf

char command[128];

sprintf (command, "date >> %s blah blah", my_c_variable);

snprintf is a safer version of sprintf that you can use for added security

Sunnycoder
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12004653
forgot, if you want to control environment variables for child programs, you can use the setenv() call:

setenv( "MYLOG", cLogFileName, 1 );

Then, a script, or program, is launched through system(), that uses this environment variable, should get the value set here (only for children, cannot pass environment to parents!)
0
 
LVL 22

Expert Comment

by:grg99
ID: 12004872
I would AVOID using system() for this kind of thing.  It generates at least two tasks, one the user shell, the other a copy of "echo".  That's okay if these tasks are going to do a lot of work for you, but these do very little.  These calls are going to be about 99% overhead.  

How about using syslog() or good old fopen( "...", "a+" ) ??


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Inorder binary search tree 5 179
Understanding the meaning of Portability in Embedded System Programming 8 193
An API detour question 7 102
gdb doesn't stop on breakpoint 2 114
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question