home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / system.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-22  |  1.8 KB  |  73 lines

  1. /* 
  2.  * system.c --
  3.  *
  4.  *    Source code for the "system" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/system.c,v 1.6 89/03/22 00:47:35 rab Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <stdlib.h>
  21. #include <signal.h>
  22. #include <sys/wait.h>
  23.  
  24. /*
  25.  *----------------------------------------------------------------------
  26.  *
  27.  * system --
  28.  *
  29.  *    Pass a string off to "sh", and return the result of executing
  30.  *    it.
  31.  *
  32.  * Results:
  33.  *    The return value is the status returned by "sh".
  34.  *
  35.  * Side effects:
  36.  *    None.
  37.  *
  38.  *----------------------------------------------------------------------
  39.  */
  40.  
  41. int
  42. system(command)
  43.     char *command;        /* Shell command to execute. */
  44. {
  45.     int pid, pid2, result;
  46.     struct sigvec quitHandler, intHandler;
  47.     static struct sigvec newHandler = {SIG_IGN, 0, 0};
  48.     union wait status;
  49.  
  50.     sigvec(SIGINT, &newHandler, &intHandler);
  51.     sigvec(SIGQUIT, &newHandler, &quitHandler);
  52.  
  53.     pid = fork();
  54.     if (pid == 0) {
  55.     execlp("sh", "sh", "-c", command, 0);
  56.     _exit(127);
  57.     }
  58.     while (1) {
  59.     pid2 = wait(&status);
  60.     if (pid2 == -1) {
  61.         result = -1;
  62.         break;
  63.     }
  64.     if (pid2 == pid) {
  65.         result = status.w_status;
  66.         break;
  67.     }
  68.     }
  69.     sigvec(SIGINT, &intHandler, (struct sigvec *) 0);
  70.     sigvec(SIGQUIT, &quitHandler, (struct sigvec *) 0);
  71.     return result;
  72. }
  73.