home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 (Alt) / MACD 5.bin / workbench / libs / shutdown.lzh / shutdown_5.1 / src / delay.c next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  1.7 KB  |  90 lines

  1. /*
  2.    shutdown delay example
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10. #include <proto/exec.h>
  11.  
  12. char *ShutdownPortname = "Shutdown";
  13.  
  14. ULONG
  15. PutAndGet (struct Message *msg, char *portname)
  16. {
  17.   struct MsgPort *prt, *reply_port;
  18.  
  19.   if (!(reply_port = CreateMsgPort ()))
  20.     return 1;
  21.   msg -> mn_ReplyPort = reply_port;
  22.  
  23.   Forbid ();
  24.   if (prt = FindPort (portname))
  25.   {
  26.     PutMsg (prt, msg);
  27.     Permit ();
  28.     do
  29.       WaitPort (reply_port);
  30.     while (!GetMsg (reply_port));
  31.   }
  32.   else
  33.     Permit ();
  34.  
  35.   DeleteMsgPort (reply_port);
  36.   return (ULONG) (prt ? 0 : 2);
  37. }
  38.  
  39. void
  40. DelayShutdown (BYTE delay)
  41. {
  42.   struct Message *msg;
  43.  
  44.   if (!(msg = AllocMem (sizeof (struct Message), MEMF_PUBLIC | MEMF_CLEAR)))
  45.   {
  46.     printf ("Out Of Memory.\n");
  47.     exit (0);
  48.   }
  49.   msg -> mn_Node.ln_Pri = delay;
  50.  
  51.   switch (PutAndGet (msg, ShutdownPortname))
  52.   {
  53.     case 0:
  54.       if (msg -> mn_Node.ln_Name == (APTR) -1)
  55.         printf ("Delay refused.\n");
  56.       else if (msg -> mn_Node.ln_Pri < delay)
  57.         printf ("Delay successful (but only %d seconds).\n", msg -> mn_Node.ln_Pri);
  58.       else
  59.         printf ("Delay successful (%d seconds).\n", msg -> mn_Node.ln_Pri);
  60.       break;
  61.     case 1:
  62.       printf ("Can't allocate port.\n");
  63.       break;
  64.     case 2:
  65.       printf ("Shutdown is not running.\n");
  66.       break;
  67.   }
  68.   FreeMem (msg, sizeof (struct Message));
  69. }
  70.  
  71. int
  72. main (int argc, char *argv[])
  73. {
  74.   int delay;
  75.  
  76.   if (argc != 2)
  77.   {
  78.     printf ("Usage: %s <seconds>.\n", argv[0]);
  79.     exit (20);
  80.   }
  81.   delay = atoi (argv[1]);
  82.   if (delay < 1 || delay > 127)
  83.   {
  84.     printf ("The argument must be in the range 1..127.\n");
  85.     exit (20);
  86.   }
  87.   DelayShutdown ((BYTE) delay);
  88.   exit (0);
  89. }
  90.