gpt4 book ai didi

c - 如何在 C 中手动将环境值设置为整数零

转载 作者:行者123 更新时间:2023-11-30 18:41:34 26 4
gpt4 key购买 nike

I have to manually set value of ENVIRONMENT initially to integer 0. Through the below function I will try to retrieve it and set it again to the deisred value.

int abcd()
{
int temp1=0,temp2;
temp1++;
char *env=NULL;
env=getenv("ENVIRONMENT");
sscanf(env,"%d",&temp2);
temp2=temp2+temp1;
printf("%d",temp2);
sprintf(temp2,"%d",env);
setenv("ENVIRONMENT",env,0);
}

all the integer variabes are 32 bit unsigned.

最佳答案

让我们看看gcc是怎么想的

nil.c:12:1: warning: passing argument 1 of ‘sprintf’ makes pointer from integer without a cast
nil.c:12:1: note: expected ‘char *’ but argument is of type ‘int’


sprintf(temp2,"%d",env);
¯¯¯¯¯ ¯¯¯

看起来好像是说你的参数类型错误。 sprintf 的手册页怎么说?

  int sprintf(char *str, const char *format, ...);

嗯。我敢打赌你一定想这么做……

  sprintf (env, "%d", temp2);

但是等等……env 指向的缓冲区有多大?它是否适合 temp2 可能包含的所有可能值以及强制的 \0 终止符?

getenv 手册说:

   RETURN VALUE

The getenv() function returns a pointer to the value in the environment, or NULL
if there is no match.

呃哦。 “在环境里!”我们可能不应该写入该缓冲区!

   BUGS

Because sprintf() and vsprintf() assume an arbitrarily long string, callers must
be careful not to overflow the actual space; this is often impossible to assure.
Note that the length of the strings produced is locale-dependent and difficult to
predict. Use snprintf() and vsnprintf() instead (or asprintf(3) and
vasprintf(3)).

哦,那个 asprintf 听起来是个好主意......

   int asprintf(char **strp, const char *fmt, ...);

DESCRIPTION

The functions asprintf() and vasprintf() are analogs of sprintf(3) and
vsprintf(3), except that they allocate a string large enough to hold the output
including the terminating null byte, and return a pointer to it via the first
argument. This pointer should be passed to free(3) to release the allocated
storage when it is no longer needed.

RETURN VALUE

When successful, these functions return the number of bytes printed, just like
sprintf(3). If memory allocation wasn't possible, or some other error occurs,
these functions will return -1, and the contents of strp is undefined.

编辑:作为对评论的回应,我没有注意到另一个错误:

   DESCRIPTION
The setenv() function adds the variable name to the environment with the value
value, if name does not already exist. If name does exist in the environment,
then its value is changed to value if overwrite is nonzero; if overwrite is zero,
then the value of name is not changed. This function makes copies of the strings
pointed to by name and value (by contrast with putenv(3)).


setenv ("ENVIRONMENT", "0", 0);
¯

您可能还希望启用覆盖该值。

我本来鼓励您自己重构它,但是将编译器和手册的内容放在一起,我们得到:

int abcd()
{
/* Set up automatic variables with default values */
const int temp1 = 1;
int env_value = 0;
char *old_env = NULL;
char *new_env = NULL;

/* Let's see if this is set in the environment */
old_env = getenv ("ENVIRONMENT");
/* Was it set? And, if so, can we get a decimal integer from it? */
if (NULL != old_env
&& 1 == sscanf (old_env, "%d", &env_value)) {
/* OK, both library functions succeeded, so let's increment… */
env_value += temp1;
}
/* Log the output */
printf ("new env value = %d\n", env_value);
/* Let's allocate a new buffer and write the string version of
* this number to it */
asprintf (&new_env, "%d", env_value);
/* Note … forgot to check success of asprintf!
* Correcting this bug left as an excercise to the reader. */

/* Copy the new value into the environment, overwriting if necessary */
setenv ("ENVIRONMENT", new_env, !0);
/* Clean up that buffer! */
free (new_env);
}

关于c - 如何在 C 中手动将环境值设置为整数零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com