system.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <unistd.h>
00003 #include <sys/types.h>
00004 #ifndef __MINGW32__
00005 #include <sys/wait.h>
00006 #endif
00007 #include <grass/gis.h>
00008 #include <grass/glocale.h>
00009 /****************************************************************
00010  * G_system (command)
00011  *     char *command;
00012  *
00013  * This is essentially the UNIX system() call, except for the signal
00014  * handling. During the call, user generated signals (intr, quit)
00015  * for the parent are ignored, but allowed for the child. Parent
00016  * signals are reset upon completion.
00017  *
00018  * This routine is useful for menu type programs that need to run
00019  * external commands and allow these commands to be interrupted by
00020  * the user without killing the menu itself.
00021  *
00022  * Note: if you want the signal settings to be the same for the
00023  * parent and the command being run, set them yourself and use
00024  * the UNIX system() call instead.
00025  ****************************************************************/
00026 
00027 #include <signal.h>
00028 #include <stdio.h>
00029 
00030 
00046 int G_system ( char *command)
00047 {
00048     int status, pid, w;
00049     void (*sigint)()
00050 #ifdef SIGQUIT
00051         , (*sigquit)()
00052 #endif
00053             ;
00054 
00055     sigint  = signal (SIGINT,  SIG_IGN);
00056 #ifdef SIGQUIT
00057     sigquit = signal (SIGQUIT, SIG_IGN);
00058 #endif
00059 
00060     fflush (stdout);
00061     fflush (stderr);
00062 
00063 #ifdef __MINGW32__
00064     signal (SIGINT,  SIG_DFL);
00065     _spawnl ( P_WAIT,
00066               "command",
00067               "command",
00068               "/c",
00069               command,
00070               NULL );
00071 #else    
00072     if ( (pid = fork()) == 0)
00073     {
00074         signal (SIGINT,  SIG_DFL);
00075         signal (SIGQUIT, SIG_DFL);
00076     
00077         execl ("/bin/sh", "sh", "-c", command, NULL);
00078         _exit(127);
00079     }
00080 
00081     if (pid < 0)
00082     {
00083         G_warning (_("Can not create a new process!"));
00084         status = -1;
00085     }
00086     else
00087     {
00088         while ( (w = wait (&status)) != pid && w != -1);
00089 
00090         if (w == -1)
00091             status = -1;
00092     }
00093 
00094 #endif
00095 
00096     signal (SIGINT,  sigint);
00097 #ifdef SIGQUIT
00098     signal (SIGQUIT, sigquit);
00099 #endif
00100 
00101     return (status);
00102 }

Generated on Fri Nov 21 11:02:18 2008 for GRASS by  doxygen 1.5.1