home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / parent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.1 KB  |  120 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20. ** file:        parent.c
  21. ** description: test the process machinery
  22. **              see also child.c
  23. */
  24.  
  25. #include "prmem.h"
  26. #include "prprf.h"
  27. #include "prinit.h"
  28. #include "prproces.h"
  29.  
  30. typedef struct Child
  31. {
  32.     const char *name;
  33.     PRProcess *process;
  34.     PRProcessAttr *attr;
  35. } Child;
  36.  
  37. static PrintUsage(void)
  38. {
  39.     PR_fprintf(PR_GetSpecialFD(PR_StandardError),
  40.         "Usage: parent [-d] child [options]\n");
  41. }
  42.  
  43. PRIntn main (PRIntn argc, char **argv)
  44. {
  45.     PRStatus rv;
  46.     PRInt32 test_status = 1;
  47.     PRFileDesc *debug = NULL;
  48.     Child *child = PR_NEWZAP(Child);
  49.  
  50.     if (1 == argc)
  51.     {
  52.         PrintUsage();
  53.         return 2;
  54.     }
  55.  
  56.     argv += 1;  /* don't care about our program name */
  57.     while (argv[0][0] == '-')
  58.     {
  59.         if (argv[0][1] == 'd')
  60.             debug = PR_GetSpecialFD(PR_StandardError);
  61.         else
  62.         {
  63.             PrintUsage();
  64.             return 2;  /* not sufficient */
  65.         }
  66.         argv += 1;
  67.     }
  68.  
  69.     if (NULL == *argv)
  70.     {
  71.         PrintUsage();
  72.         return 2;
  73.     }
  74.  
  75.     child->name = *argv;
  76.     if (NULL != debug) PR_fprintf(debug, "Forking %s\n", child->name);
  77.  
  78.     child->attr = PR_NewProcessAttr();
  79.     PR_SetStdioRedirect(
  80.         child->attr, PR_StandardOutput,
  81.         PR_GetSpecialFD(PR_StandardOutput));
  82.     PR_SetStdioRedirect(
  83.         child->attr, PR_StandardError,
  84.         PR_GetSpecialFD(PR_StandardError));
  85.  
  86.     child->process = PR_CreateProcess(
  87.         child->name, argv, NULL, child->attr);
  88.  
  89.     test_status = (NULL == child->process) ? 1 : 0;
  90.     if (NULL != debug)
  91.         PR_fprintf(
  92.             debug, "Child was %sforked\n",
  93.             (0 == test_status) ? "" : "NOT ");
  94.  
  95.     if (0 == test_status)
  96.     {
  97.         if (NULL != debug) PR_fprintf(debug, "Waiting for child to exit\n");
  98.         rv = PR_WaitProcess(child->process, &test_status);
  99.         if (PR_SUCCESS == rv)
  100.         {
  101.             if (NULL != debug)
  102.                 PR_fprintf(
  103.                     debug, "Child exited %s\n",
  104.                     (0 == test_status) ? "successfully" : "with error");
  105.         }
  106.         else
  107.         {
  108.             test_status = 1;
  109.             if (NULL != debug)
  110.                 PR_fprintf(debug, "PR_WaitProcess failed\n");
  111.         }
  112.     }
  113.     PR_Cleanup();
  114.     return test_status;
  115.     
  116. }  /* main */
  117.  
  118. /* parent.c */
  119.  
  120.