diff --git a/src/library.cpp b/src/library.cpp index 2a7bbf07b3..94f8dfa7a3 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -445,26 +445,14 @@ is passed to :cpp:func:`lammps_commands_string` for processing. void lammps_commands_list(void *handle, int ncmd, const char **cmds) { LAMMPS *lmp = (LAMMPS *) handle; - - int n = ncmd+1; - for (int i = 0; i < ncmd; i++) n += strlen(cmds[i]); - - char *str = (char *) lmp->memory->smalloc(n,"lib/commands/list:str"); - str[0] = '\0'; - n = 0; + std::string allcmds; for (int i = 0; i < ncmd; i++) { - strcpy(&str[n],cmds[i]); - n += strlen(cmds[i]); - if (str[n-1] != '\n') { - str[n] = '\n'; - str[n+1] = '\0'; - n++; - } + allcmds.append(cmds[i]); + if (allcmds.back() != '\n') allcmds.append(1,'\n'); } - lammps_commands_string(handle,str); - lmp->memory->sfree(str); + lammps_commands_string(handle,allcmds.c_str()); } /* ---------------------------------------------------------------------- */