0

I this part of code, instructs my program (which make screenshots) to spawn a command and quit (close) itself. This can be used to switch to a program using a key in my program, such as to spawn "gimp" or another image editor that a user would like to use it.

case SWITCH_TO:
    if( arg ) {
        char commandline[ 256 ];
        snprintf( commandline, sizeof (commandline), "%s &", arg );
        system( commandline );
        cmd->quit = 1;
    }
    break;

For example using:

program-command SWITCH_TO "gimp"

will have my program call system( "gimp &" ), quit (close) itself and run gimp.

program-command SWITCH_TO "fotoxx"

will have my program call system( "fotoxx &" ), quit (close) itself and run fotoxx.

I want my program to check if "commandline" is valid (application found in $PATH) and if not, command "program-command SWITCH_TO" not run and not close my program ("cmd->quit = 1" do this, close program).

Thanks

8
  • If you want to launch a program, I would go with the exec family, pubs.opengroup.org/onlinepubs/009604499/functions/exec.html However, since the program should already be alive, I would go with neither exec or system. Commented Mar 20, 2012 at 8:50
  • I dont understand what you mean by "I want to check if application which want to switch with program exist, and if not, "program-command SWITCH_TO" to do nothing and program continue to run"? Commented Mar 20, 2012 at 8:56
  • This might help you: stackoverflow.com/questions/1014822/… Commented Mar 20, 2012 at 8:57
  • Not sure what do you want, can be more specific? If you want to check the status of your "switch to command", fork+exec+wait can do the job. Commented Mar 20, 2012 at 9:00
  • 1
    I don't understand your design. You show what appears to be a command line with options that executes your program. That in turn looks at its arguments, and either runs the other command it was given or...well, what is the alternative action? It would seem that the only real alternative is to exit (quit), but that's not what you want, so what do you want to happen when the given command is not available? Is your program to sit there, prompting the user for the next thing to do? Isn't it easier to let the shell do that instead of your program? Indeed, you might then just end up typing gimp &. Commented Mar 20, 2012 at 9:32

1 Answer 1

1

As an initial solution, you can try adding a check of the return value of the system() call.

It will be -1 on error, or else the return status of the program you run. Not sure how the latter behaves when you use & to get a child process. Also not sure if that detaches the child enough from your parent; if not, the child will terminate when you quit your application.

As others have suggested, look into fork() and exec() calls to do this properly. You can use a plain exec() to replace your process with that of the program you wish to start; if that fails you are still running, and thus you never need to set the quit flag in that case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.