Skip to content

Commit

Permalink
Refactor the preparation of execvp parameters for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Dec 18, 2024
1 parent 26d3b3b commit bcb5cfb
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/cpulimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,19 +506,15 @@ int main(int argc, char *argv[])
/* Executable file */
const char *cmd = argv[optind];
/* Command line arguments */
char **cmd_args = (char **)malloc((size_t)(argc - optind + 1) * sizeof(char *));
char **cmd_args = (char **)calloc((size_t)(argc - optind + 1), sizeof(char *));
if (cmd_args == NULL)
{
fprintf(stderr, "Memory allocation failed for cmd_args\n");
exit(EXIT_FAILURE);
}

/* Prepare command arguments */
for (i = 0; i < argc - optind; i++)
{
cmd_args[i] = argv[i + optind];
}
cmd_args[i] = NULL;
/* Prepare command arguments for execvp */
memcpy(cmd_args, argv + optind, (size_t)(argc - optind) * sizeof(char *));

/* If verbose, print the command being executed */
if (verbose)
Expand Down

0 comments on commit bcb5cfb

Please sign in to comment.