home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / exit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.3 KB  |  119 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. #include "prio.h"
  20. #include "prprf.h"
  21. #include "prinit.h"
  22. #include "prthread.h"
  23. #include "prproces.h"
  24. #include "prinrval.h"
  25.  
  26. #include "plgetopt.h"
  27.  
  28. #include <stdlib.h>
  29.  
  30. static PRInt32 dally = 0;
  31. static PRFileDesc *err = NULL;
  32. static PRBool verbose = PR_FALSE, force = PR_FALSE;
  33.  
  34. static void Help(void)
  35. {
  36.     PR_fprintf(err, "Usage: [-t s] [-h]\n");
  37.     PR_fprintf(err, "\t-d   Verbose output              (default: FALSE)\n");
  38.     PR_fprintf(err, "\t-x   Forced termination          (default: FALSE)\n");
  39.     PR_fprintf(err, "\t-t   Time for thread to block    (default: 10 seconds)\n");
  40.     PR_fprintf(err, "\t-h   This message and nothing else\n");
  41. }  /* Help */
  42.  
  43. static void Dull(void *arg)
  44. {
  45.     PR_Sleep(PR_SecondsToInterval(dally));
  46.     if (verbose && force)
  47.         PR_fprintf(err, "If you see this, the test failed\n");
  48. }  /* Dull */
  49.  
  50. static PRIntn PR_CALLBACK RealMain(PRIntn argc, char **argv)
  51. {
  52.     PLOptStatus os;
  53.     PLOptState *opt = PL_CreateOptState(argc, argv, "ht:dx");
  54.  
  55.     err = PR_GetSpecialFD(PR_StandardError);
  56.  
  57.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  58.     {
  59.         if (PL_OPT_BAD == os) continue;
  60.         switch (opt->option)
  61.         {
  62.         case 'd':  /* verbosity */
  63.             verbose = PR_TRUE;
  64.             break;
  65.         case 'x':  /* force exit */
  66.             force = PR_TRUE;
  67.             break;
  68.         case 't':  /* seconds to dally in child */
  69.             dally = atoi(opt->value);
  70.             break;
  71.         case 'h':  /* user wants some guidance */
  72.         default:
  73.             Help();  /* so give him an earful */
  74.             return 2;  /* but not a lot else */
  75.         }
  76.     }
  77.     PL_DestroyOptState(opt);
  78.  
  79.     if (0 == dally) dally = 10;
  80.  
  81.     /*
  82.      * Create LOCAL and GLOBAL threads
  83.      */
  84.     (void)PR_CreateThread(
  85.         PR_USER_THREAD, Dull, NULL, PR_PRIORITY_NORMAL,
  86.               PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
  87.  
  88.     (void)PR_CreateThread(
  89.         PR_USER_THREAD, Dull, NULL, PR_PRIORITY_NORMAL,
  90.               PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0);
  91.  
  92.     if (verbose)
  93.         PR_fprintf(
  94.             err, "Main is exiting now. Program should exit %s.\n",
  95.             (force) ? "immediately" : "after child dally time");
  96.  
  97.     if (force)
  98.     {
  99.         PR_ProcessExit(0);
  100.         if (verbose)
  101.         {
  102.             PR_fprintf(err, "You should not have gotten here.\n");
  103.             return 1;
  104.         }
  105.     }
  106.     return 0;
  107.         
  108. }
  109.  
  110.  
  111. PRIntn main(PRIntn argc, char *argv[])
  112. {
  113.     PRIntn rv;
  114.     
  115.     PR_STDIO_INIT();
  116.     rv = PR_Initialize(RealMain, argc, argv, 0);
  117.     return rv;
  118. }  /* main */
  119.