command: Move environ-adding code to common function virCommandAddEnv.

This is just code motion.  The semantics of the code should be
identical after this change.
This commit is contained in:
Richard W.M. Jones 2012-09-24 17:30:18 +01:00
parent f8fbeb50d5
commit f644361b1e

View File

@ -984,6 +984,22 @@ virCommandNonblockingFDs(virCommandPtr cmd)
cmd->flags |= VIR_EXEC_NONBLOCK; cmd->flags |= VIR_EXEC_NONBLOCK;
} }
/* Add an environment variable to the cmd->env list. 'env' is a
* string like "name=value".
*/
static inline void
virCommandAddEnv(virCommandPtr cmd, char *env)
{
/* Arg plus trailing NULL. */
if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
VIR_FREE(env);
cmd->has_error = ENOMEM;
return;
}
cmd->env[cmd->nenv++] = env;
}
/** /**
* virCommandAddEnvFormat: * virCommandAddEnvFormat:
* @cmd: the command to modify * @cmd: the command to modify
@ -1009,14 +1025,7 @@ virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
} }
va_end(list); va_end(list);
/* Arg plus trailing NULL. */ virCommandAddEnv(cmd, env);
if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
VIR_FREE(env);
cmd->has_error = ENOMEM;
return;
}
cmd->env[cmd->nenv++] = env;
} }
/** /**
@ -1056,14 +1065,7 @@ virCommandAddEnvString(virCommandPtr cmd, const char *str)
return; return;
} }
/* env plus trailing NULL */ virCommandAddEnv(cmd, env);
if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
VIR_FREE(env);
cmd->has_error = ENOMEM;
return;
}
cmd->env[cmd->nenv++] = env;
} }
@ -1084,9 +1086,7 @@ virCommandAddEnvBuffer(virCommandPtr cmd, virBufferPtr buf)
return; return;
} }
/* env plus trailing NULL. */ if (virBufferError(buf)) {
if (virBufferError(buf) ||
VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
cmd->has_error = ENOMEM; cmd->has_error = ENOMEM;
virBufferFreeAndReset(buf); virBufferFreeAndReset(buf);
return; return;
@ -1096,7 +1096,7 @@ virCommandAddEnvBuffer(virCommandPtr cmd, virBufferPtr buf)
return; return;
} }
cmd->env[cmd->nenv++] = virBufferContentAndReset(buf); virCommandAddEnv(cmd, virBufferContentAndReset(buf));
} }