Solved

Unix system command substitution in C

Posted on 2004-09-08
4
267 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Directory does exist 19 144
How to get time_t value that is a week old 6 163
Finding a good hash function 4 120
smtp c source code 7 42
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now