Solved

Unix system command substitution in C

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 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

10 Experts available now in Live!

Get 1:1 Help Now