File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -279,18 +279,23 @@ bool verifyUserMapping(std::string fpath, std::string execName)
279279}
280280
281281
282- void runcmd (std::string cmd)
282+ int runcmd (std::string cmd)
283283{
284284 std::string readline;
285285 FILE * fp = popen (cmd.c_str (), " r" );
286- if (fp != NULL ) {
287- char buffer[4096 ];
288- while (fgets (buffer, sizeof (buffer) - 1 , fp) != NULL ) {
289- std::cout << buffer;
290- }
286+ if (!fp) {
287+ throw std::runtime_error (" popen() failed!" );
291288 }
292-
293- pclose (fp);
289+ char buffer[4096 ];
290+ while (fgets (buffer, sizeof (buffer) - 1 , fp) != NULL ) {
291+ std::cout << buffer;
292+ }
293+ // pclose returns the termination status of the command
294+ int status = pclose (fp);
295+ // Extract the exit code from the status
296+ // WEXITSTATUS is a macro that gets the actual return value (0-255)
297+ int exit_code = WEXITSTATUS (status);
298+ return exit_code;
294299}
295300
296301int changeUser (char * execUser)
@@ -376,7 +381,7 @@ int main(int argc, char **argv)
376381 bsubcmd.append (" " );
377382 bsubcmd.append (argv[i]);
378383 }
379- runcmd (bsubcmd);
384+ int exit_code = runcmd (bsubcmd);
380385
381- return 0 ;
386+ return exit_code ;
382387}
You can’t perform that action at this time.
0 commit comments