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

iattila
  • 135
  • 1
  • 2
  • 6
  • If you want to launch a program, I would go with the exec family, http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html However, since the program should already be alive, I would go with neither exec or system. – Niklas Hansson Mar 20 '12 at 08: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"? – Pavan Manjunath Mar 20 '12 at 08:56
  • This might help you: http://stackoverflow.com/questions/1014822/how-to-know-which-window-has-focus-and-how-to-change-it – Niklas Hansson Mar 20 '12 at 08: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. – wangshuaijie Mar 20 '12 at 09:00
  • If `firefox` is not found in your `$PATH`, `system()` will have the behaviour that you expect, i.e., do nothing. There is no need for an additional check. – mouviciel Mar 20 '12 at 09:04
  • >>>> If firefox is not found in your $PATH, system() will have the behaviour that you expect, i.e., do nothing. There is no need for an additional check. <<<<<< It will close, that do not want! "cmd->quit = 1" do this. – iattila Mar 20 '12 at 09:08
  • 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 &`. – Jonathan Leffler Mar 20 '12 at 09:32
  • Jonathan Leffler, command run from fifo file create by program (controlling the program on another machine with my notebook) – iattila Mar 20 '12 at 12:11

1 Answers1

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.

unwind
  • 391,730
  • 64
  • 469
  • 606