Solved

Unix system command substitution in C

Posted on 2004-09-08
4
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

734 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