Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass int/char into system() a Linux system call

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

like image 347
Jonathan Avatar asked Dec 09 '22 21:12

Jonathan


1 Answers

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.

like image 178
unwind Avatar answered Dec 23 '22 18:12

unwind