print error message to std error

This commit is contained in:
Roy Qu 2024-06-02 09:48:44 +08:00
parent b060ecc09e
commit b05f659860
1 changed files with 11 additions and 11 deletions

View File

@ -96,7 +96,7 @@ int ExecuteCommand(vector<string>& command,bool reInp, long int &peakMemory) {
int command_size; int command_size;
if (reInp) { if (reInp) {
if (command.size()<2) { if (command.size()<2) {
printf("not enough arguments1!\n"); fprintf(stderr,"not enough arguments1!\n");
exit(-1); exit(-1);
} }
freopen(unescapeSpaces(command[0]).c_str(),"r",stdin); freopen(unescapeSpaces(command[0]).c_str(),"r",stdin);
@ -105,7 +105,7 @@ int ExecuteCommand(vector<string>& command,bool reInp, long int &peakMemory) {
command_begin = 1; command_begin = 1;
} else { } else {
if (command.size()<1) { if (command.size()<1) {
printf("not enough arguments2!\n"); fprintf(stderr,"not enough arguments2!\n");
exit(-1); exit(-1);
} }
path_to_command = unescapeSpaces(command[0]); path_to_command = unescapeSpaces(command[0]);
@ -126,10 +126,10 @@ int ExecuteCommand(vector<string>& command,bool reInp, long int &peakMemory) {
argv[0]=(char *)file.c_str(); argv[0]=(char *)file.c_str();
int result=execv(path_to_command.c_str(),argv); int result=execv(path_to_command.c_str(),argv);
if (result) { if (result) {
printf("Failed to start command %s %s!\n",path_to_command.c_str(), file.c_str()); fprintf(stderr,"Failed to start command %s %s!\n",path_to_command.c_str(), file.c_str());
printf("errno %d: %s\n",errno,strerror(errno)); fprintf(stderr,"errno %d: %s\n",errno,strerror(errno));
char* current_dir = getcwd(nullptr, 0); char* current_dir = getcwd(nullptr, 0);
printf("current dir: %s",current_dir); fprintf(stderr,"current dir: %s",current_dir);
free(current_dir); free(current_dir);
exit(-1); exit(-1);
} }
@ -140,7 +140,7 @@ int ExecuteCommand(vector<string>& command,bool reInp, long int &peakMemory) {
struct rusage usage; struct rusage usage;
w = wait4(pid, &status, WUNTRACED | WCONTINUED, &usage); w = wait4(pid, &status, WUNTRACED | WCONTINUED, &usage);
if (w==-1) { if (w==-1) {
perror("wait4 failed!"); fprintf(stderr,"wait4 failed!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
peakMemory = usage.ru_maxrss; peakMemory = usage.ru_maxrss;
@ -157,9 +157,9 @@ int main(int argc, char** argv) {
char* sharedMemoryId; char* sharedMemoryId;
// First make sure we aren't going to read nonexistent arrays // First make sure we aren't going to read nonexistent arrays
if(argc < 4) { if(argc < 4) {
printf("\n--------------------------------"); fprintf(stderr,"\n--------------------------------");
printf("\nUsage: consolepauser <0|1> <shared_memory_id> <filename> <parameters>\n"); fprintf(stderr,"\nUsage: consolepauser <0|1> <shared_memory_id> <filename> <parameters>\n");
printf("\n 1 means the STDIN is redirected by Red Panda C++; 0 means not\n"); fprintf(stderr,"\n 1 means the STDIN is redirected by Red Panda C++; 0 means not\n");
PauseExit(EXIT_SUCCESS,false); PauseExit(EXIT_SUCCESS,false);
} }
@ -183,12 +183,12 @@ int main(int argc, char** argv) {
int fd_shm = shm_open(sharedMemoryId,O_RDWR,S_IRWXU); int fd_shm = shm_open(sharedMemoryId,O_RDWR,S_IRWXU);
if (fd_shm==-1) { if (fd_shm==-1) {
//todo: handle error //todo: handle error
printf("shm open failed %d:%s\n",errno,strerror(errno)); fprintf(stderr,"shm open failed %d:%s\n",errno,strerror(errno));
} else { } else {
// `ftruncate` has already done in RedPandaIDE // `ftruncate` has already done in RedPandaIDE
pBuf = (char*)mmap(NULL,BUF_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm,0); pBuf = (char*)mmap(NULL,BUF_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm,0);
if (pBuf == MAP_FAILED) { if (pBuf == MAP_FAILED) {
printf("mmap failed %d:%s\n",errno,strerror(errno)); fprintf(stderr,"mmap failed %d:%s\n",errno,strerror(errno));
pBuf = nullptr; pBuf = nullptr;
} }
} }