如何将int/char传递到system()中

时间:2022-08-10 10:07:49

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

好吧,这听起来可能有点傻,但我想不出一种方法把int/char传递到这个系统调用中。

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"

我期望这将给我以下命令在终端“cal 3 2009”

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

终端会显示2009年3月的日历。

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.

什么好主意吗?我需要使这个方法系统(“cal”)返回一个动态日历。

Notes: cal take the argument cal month year

注:卡尔按月计算

7 个解决方案

#1


8  

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

您需要构建适当的命令行字符串,system()不会为您做:

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.

通常对缓冲区溢出的注意是适用的,尽管在这种情况下,当两个参数都是整数时,您应该是相当安全的。

#2


3  

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

基本上,从系统调用中输出printf:

char my_cmd[MAX_SIZE];
snprintf(my_cmd, MAX_SIZE, "cal %d %d", month, year);
system(my_cmd);

#3


1  

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.

您需要传递一个已经进行了所有必要转换的字符串。您可以使用sprintf()来生成这样的字符串,只要小心分配足够大的缓冲区即可。

#4


1  

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.

这是因为您假设系统的行为与printf类似,但事实并非如此。要获得所需的内容,您必须首先通过sprintf获得替换,然后将该缓冲区传递给系统。

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.

不过要小心,这可能会成为一个潜在的安全漏洞,因为您可能允许在命令行执行时传递未知参数。此外,还必须注意,您使用的临时缓冲区足够大,足以承载最终的字符串。

#5


1  

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);
}

#6


0  

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

在调用系统之前,您必须将命令格式化为字符串,例如使用snprintf

#7


0  

char
  string[64];

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

system( string );

#1


8  

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

您需要构建适当的命令行字符串,system()不会为您做:

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.

通常对缓冲区溢出的注意是适用的,尽管在这种情况下,当两个参数都是整数时,您应该是相当安全的。

#2


3  

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

基本上,从系统调用中输出printf:

char my_cmd[MAX_SIZE];
snprintf(my_cmd, MAX_SIZE, "cal %d %d", month, year);
system(my_cmd);

#3


1  

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.

您需要传递一个已经进行了所有必要转换的字符串。您可以使用sprintf()来生成这样的字符串,只要小心分配足够大的缓冲区即可。

#4


1  

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.

这是因为您假设系统的行为与printf类似,但事实并非如此。要获得所需的内容,您必须首先通过sprintf获得替换,然后将该缓冲区传递给系统。

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.

不过要小心,这可能会成为一个潜在的安全漏洞,因为您可能允许在命令行执行时传递未知参数。此外,还必须注意,您使用的临时缓冲区足够大,足以承载最终的字符串。

#5


1  

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);
}

#6


0  

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

在调用系统之前,您必须将命令格式化为字符串,例如使用snprintf

#7


0  

char
  string[64];

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

system( string );