git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@14164 f3b2605a-c512-4ea7-a41b-209d697bcdaa
This commit is contained in:
@ -15,6 +15,7 @@
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "errno.h"
|
||||
#include "ctype.h"
|
||||
#include "unistd.h"
|
||||
#include "sys/stat.h"
|
||||
@ -1086,48 +1087,96 @@ void Input::quit()
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
char *shell_failed_message(const char* cmd, int errnum)
|
||||
{
|
||||
const char *errmsg = strerror(errnum);
|
||||
int len = strlen(cmd)+strlen(errmsg)+64;
|
||||
char *msg = new char[len];
|
||||
sprintf(msg,"shell command '%s' failed with error '%s'", cmd, errmsg);
|
||||
return msg;
|
||||
}
|
||||
|
||||
void Input::shell()
|
||||
{
|
||||
int rv,err;
|
||||
|
||||
if (narg < 1) error->all(FLERR,"Illegal shell command");
|
||||
|
||||
if (strcmp(arg[0],"cd") == 0) {
|
||||
if (narg != 2) error->all(FLERR,"Illegal shell cd command");
|
||||
chdir(arg[1]);
|
||||
rv = (chdir(arg[1]) < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
if (me == 0 && err != 0) {
|
||||
char *message = shell_failed_message("cd",err);
|
||||
error->warning(FLERR,message);
|
||||
delete [] message;
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"mkdir") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell mkdir command");
|
||||
if (me == 0)
|
||||
for (int i = 1; i < narg; i++) {
|
||||
#if defined(_WIN32)
|
||||
_mkdir(arg[i]);
|
||||
rv = _mkdir(arg[i]);
|
||||
#else
|
||||
mkdir(arg[i], S_IRWXU | S_IRGRP | S_IXGRP);
|
||||
rv = mkdir(arg[i], S_IRWXU | S_IRGRP | S_IXGRP);
|
||||
#endif
|
||||
if (rv < 0) {
|
||||
char *message = shell_failed_message("mkdir",errno);
|
||||
error->warning(FLERR,message);
|
||||
delete [] message;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"mv") == 0) {
|
||||
if (narg != 3) error->all(FLERR,"Illegal shell mv command");
|
||||
if (me == 0) rename(arg[1],arg[2]);
|
||||
rv = (rename(arg[1],arg[2]) < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
if (me == 0 && err != 0) {
|
||||
char *message = shell_failed_message("mv",err);
|
||||
error->warning(FLERR,message);
|
||||
delete [] message;
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"rm") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell rm command");
|
||||
if (me == 0)
|
||||
for (int i = 1; i < narg; i++) unlink(arg[i]);
|
||||
for (int i = 1; i < narg; i++) {
|
||||
if (unlink(arg[i]) < 0) {
|
||||
char *message = shell_failed_message("rm",errno);
|
||||
error->warning(FLERR,message);
|
||||
delete [] message;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"rmdir") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell rmdir command");
|
||||
if (me == 0)
|
||||
for (int i = 1; i < narg; i++) rmdir(arg[i]);
|
||||
for (int i = 1; i < narg; i++) {
|
||||
if (rmdir(arg[i]) < 0) {
|
||||
char *message = shell_failed_message("rmdir",errno);
|
||||
error->warning(FLERR,message);
|
||||
delete [] message;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"putenv") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell putenv command");
|
||||
for (int i = 1; i < narg; i++) {
|
||||
char *ptr = strdup(arg[i]);
|
||||
rv = 0;
|
||||
#ifdef _WIN32
|
||||
if (ptr != NULL) _putenv(ptr);
|
||||
if (ptr != NULL) rv = _putenv(ptr);
|
||||
#else
|
||||
if (ptr != NULL) putenv(ptr);
|
||||
if (ptr != NULL) rv = putenv(ptr);
|
||||
#endif
|
||||
rv = (rv < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
if (me == 0 && err != 0) {
|
||||
char *message = shell_failed_message("putenv",err);
|
||||
error->warning(FLERR,message);
|
||||
delete [] message;
|
||||
}
|
||||
}
|
||||
|
||||
// use work string to concat args back into one string separated by spaces
|
||||
@ -1144,7 +1193,9 @@ void Input::shell()
|
||||
strcat(work,arg[i]);
|
||||
}
|
||||
|
||||
if (me == 0) system(work);
|
||||
if (me == 0)
|
||||
if (system(work) != 0)
|
||||
error->warning(FLERR,"shell command returned with non-zero status");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user