home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / docmd.c < prev    next >
Text File  |  1994-09-25  |  5KB  |  193 lines

  1. /*  docmd.c    Various routines to fork a process.
  2.     Copyright (C) 1990, 1993  Rick Adams and Bob Billson
  3.  
  4.     This file is part of the OS-9 UUCP package, UUCPbb.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     The author of UUCPbb, Bob Billson, can be contacted at:
  21.     bob@kc2wz.bubble.org  or  uunet!kc2wz!bob  or  by snail mail:
  22.     21 Bates Way, Westfield, NJ 07090
  23. */
  24.  
  25. /* Added dochcmd() and docmd_error() to this file.  Error are logged to
  26.    to the /DD/LOG/uulog file if possible. */
  27.  
  28. #include "uucp.h"
  29.  
  30. #ifndef _UCC
  31. char *strtok();
  32. #endif
  33.  
  34. QQ int childid;
  35. static QQ char *cantspawn = "cannot spawn process--> ";
  36.  
  37. #ifdef _OSK
  38. extern int chain();
  39. #endif
  40.  
  41.  
  42.  
  43. /* docmd  --fork a command
  44.  
  45.             just like system(), but no 80 character limit.  Exits if forked
  46.             process returns an error.   Does NOT return error to calling
  47.             function. */
  48.  
  49. int docmd (command)
  50. char *command;
  51. {
  52. #ifdef _OSK
  53.      char *argv[32];   /* up to 32 arguments */
  54.      char cmd [256];
  55.      int status;
  56.  
  57.      strcpy (cmd, command);     
  58.      parse_cmd (argv, cmd);
  59. # ifdef _UCC
  60.      childid = os9exec (os9fork, argv[0], argv, _environ, 0, 0, 3);
  61. # else /* C 3.2 */
  62.      childid = os9exec (os9fork, argv[0], argv, environ, 0, 0, 3);
  63. # endif
  64. #else
  65.      char cmd[512]; 
  66.      register char *p;
  67.      int status;
  68.  
  69.      strcpy (cmd, command);
  70.      p = parse_cmd (cmd);
  71.      childid = os9fork (cmd, strlen (p), p, 1, 1, 1);
  72. #endif
  73.  
  74.      if (childid == ERROR)
  75.        {
  76.           docmd_error ("docmd()", cantspawn, command);
  77.           exit (errno);
  78.        }
  79.  
  80.      /* ignore signals not for us */
  81.      while (wait (&status) != childid)
  82.           /* do nothing */;
  83.  
  84.      childid = -1;
  85.  
  86.      if (status != 0)
  87.           exit (status);
  88. }
  89.  
  90.  
  91.  
  92. /* docmd_na  --fork a command
  93.  
  94.                Just like system(), but no 80 character limit.  If the forked
  95.                process returns an error, the error is returned to the calling
  96.                function. */
  97.  
  98. int docmd_na (command)
  99. char *command;
  100. {
  101. #ifdef _OSK
  102.      char *argv[32];
  103.      char cmd [256];
  104.      int status;
  105.  
  106.      strcpy (cmd, command);
  107.      parse_cmd (argv, cmd);
  108. # ifdef _UCC
  109.      childid = os9exec (os9fork, argv[0], argv, _environ, 0, 0, 3);
  110. # else /* C 3.2 */
  111.      childid = os9exec (os9fork, argv[0], argv, environ, 0, 0, 3);
  112. # endif
  113. #else
  114.      char cmd[512];
  115.      register char *p;
  116.      int status;
  117.  
  118.      strcpy (cmd, command);
  119.      p = parse_cmd (cmd);
  120.      childid = os9fork (cmd, strlen (p), p, 1, 1, 1);
  121. #endif
  122.  
  123.      if (childid == ERROR)
  124.        {
  125.           docmd_error ("docmd_na()", cantspawn, command);
  126.           return (ERROR);
  127.        }
  128.  
  129.      /* ignore signals not for us */
  130.      while (wait (&status) != childid)
  131.           /* do nothing */;
  132.  
  133.      childid = -1;
  134.      return (status);
  135. }
  136.  
  137.  
  138.  
  139. /* dochcmd   --do chained command. */
  140.  
  141. int dochcmd (command)
  142. char *command;
  143. {
  144. #ifdef _OSK
  145.      char cmd[256];
  146.      register char *argv[32];
  147.  
  148.      strcpy (cmd, command);
  149.      parse_cmd (argv, cmd);
  150. #else
  151.      char cmd[256];
  152.      register char *p;
  153.  
  154.      strcpy (cmd, command);
  155.      p = parse_cmd (cmd);
  156. #endif
  157.  
  158. #ifdef _OSK
  159. # ifdef _UCC
  160.      if (os9exec (chain, argv[0], argv, _environ, 0, 0, 3) == ERROR)
  161. # else /* C 3.2 */
  162.      if (os9exec (chain, argv[0], argv, environ, 0, 0, 3) == ERROR)
  163. # endif
  164. #else
  165.      if (chain (cmd, strlen (p), p, 1, 1, 1) == ERROR)
  166. #endif
  167.        {
  168.           docmd_error ("dochcmd()", "cannot chain process--> ", command);
  169.           exit (0);
  170.        }
  171. }
  172.  
  173.  
  174.  
  175. static int docmd_error (name, reason, command)
  176. char *name, *reason, *command;
  177. {
  178.      char fname[100];
  179.      register FILE *log;
  180.  
  181.      sprintf (fname, "%s/uulog", logdir);
  182.  
  183.      /* Try logging the error, don't fret if we can't */
  184.      if ((log = fopen (fname, "a")) != NULL)
  185.        {
  186.           fprintf (log, "%s %s: %s%s (error %d)\n",
  187.                         gtime(), name, reason, command, errno);
  188.           fclose (log);
  189.        }
  190.  
  191.      fprintf (stderr, "%s: %s%s (error %d)\n", name, reason, command, errno);
  192. }
  193.