views:

431

answers:

7

Ok it might sounds dumb, but I couldn't figure out a way to pass int/char into this system call

here is how I would like it to work

system ("cal %d %d", month, year);

I expect this will give me the following command on terminal "cal 3 2009"

and the terminal will show me the calendar of March 2009.

But the compiler is complaining it has too many arguments

any ideas? I need to make this method system ("cal ") return me a dynamic calendar.

Notes: cal take the argument cal month year

+1  A: 

You need to pass a string that already has all necessary transformations made. You can use sprintf() for producing such a string, just be careful with allocating a large enough buffer.

sharptooth
oh right thanks!
Jonathan
A: 

you have to format your command in a string before calling system with it, use snprintf for instance

fa.
+1  A: 

This happens because you are assuming system behaves like printf, which is not the case. To obtain what you need, you have first to obtain the substitution through sprintf into a buffer, then pass this buffer to system.

Be careful though, this can become a potential security hole, because you are potentially allowing unknown parameters to be passed at command line execution. Also, you have to be careful that the temporary buffer you use is large enough to host your final string.

Stefano Borini
A: 
char
  string[64];

sprintf( string, "cal %d %d", month, year );

system( string );
Blank Xavier
+8  A: 

You need to build the proper command line string, system() won't do it for you:

char cmd[64];

snprintf(cmd, sizeof cmd, "cal %d %d", month, year);
system(cmd);

The usual caveats about buffer overflow apply, although in this particular case when both arguments are integers you should be fairly safe.

unwind
Just a nitpick. If I remember correctly, the snprintf using sizeof could have troubles if the cmd buffer is passed (ex. last two lines are in a function by theirselves), instead of allocated in the stack. In this case, you will get a pointer, and sizeof will return 4 (on 32 bit architectures).
Stefano Borini
This sort of defensive coding is unnecessary when you control the inputs (such as knowing that the int month and year can never be more than 11 characters) - you only really need be careful for untrusted input. So char cmd[28] would be fine with sprintf.
paxdiablo
@Stefano: well ... That was not the code I wrote, so I don't consider it risky.@Pax: True, 256 might be a bit too much, but I would probably not resort to counting it out by hand and picking the exact right choice, I prefer some margin of error. But maybe 64 looks better, I'll edit. Thanks.
unwind
@unwind: agreed, indeed I was just nitpicking to warn FJ if s/he did it in the future.
Stefano Borini
+3  A: 

Basically, just do your printf thing out of the system call :

char my_cmd[MAX_SIZE];
snprintf(my_cmd, MAX_SIZE, "cal %d %d", month, year);
system(my_cmd);
shodanex
thanks im pretty noob with C :(
Jonathan
+1  A: 

try

#include <stdlib.h>
#include <stdio.h>

int main()
{
  char command_buf [20];
  const int month = 3;
  const int year = 2009;
  snprintf(command_buf, sizeof(command_buf), "cal %d %d", month, year);
  system(command_buf);
}
yves Baumes