home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / psm / cnt_main.c next >
Encoding:
C/C++ Source or Header  |  1992-01-23  |  2.5 KB  |  92 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. #include <stdio.h>
  12. #include <osfcn.h>
  13. #include <string.h>
  14.  
  15. #include "psm.h"
  16. #include "psm_err.h"
  17.  
  18. #ifndef FALSE
  19. #define FALSE 0
  20. #endif
  21.  
  22. main (int argc, char *argv[])
  23. {
  24.    int do_occupied = 0;
  25.    int do_clean = 0;
  26.    int do_squeeze = 0;
  27.    sos_Sync_mode mode = WAITING;
  28.  
  29.    int i = 1;
  30.    for (; i < argc && strlen (argv[i]) == 2 && argv[i][0] == '-'; i++)
  31.    {  switch (argv[i][1])
  32.       {  case 'o': do_occupied = 1;    break;
  33.      case 'd': do_clean = 1;    break;
  34.      case 's': do_squeeze = 1;    break;
  35.      case 't': mode = TESTING;    break;
  36.      default : err_raise (err_USE, err_CNT_USAGE, NULL, FALSE);
  37.            exit (1);
  38.       }
  39.    }
  40.  
  41.    if (do_clean + do_occupied + do_squeeze != 1)
  42.    {  err_raise (err_USE, err_CNT_USAGE, NULL, FALSE);
  43.       exit (1);
  44.    }
  45.  
  46.    if (i != argc-1)
  47.    {  err_raise (err_USE, err_CNT_USAGE, NULL, FALSE);
  48.       exit (1);
  49.    }
  50.  
  51.    int c, l;
  52.    sscanf (argv[i], "%d%n", &c, &l);
  53.    if (c == 0 || l != strlen (argv[i]))
  54.    {  err_raise (err_USE, err_CNT_USAGE, NULL, FALSE);
  55.       exit (1);
  56.    }
  57.  
  58.    sos_Container ct = sos_Container::make (c);
  59.  
  60.    if (do_occupied)
  61.    {  sos_Open_result opened = ct.open (READING, mode);
  62.       if (opened == UNACCESSIBLE)
  63.      err_raise (err_USE, err_CNT_OPEN_FAILED, NULL, FALSE);
  64.       else if (opened == LOCKED)
  65.      err_raise (err_USE, err_CNT_CONTAINER_BUSY, NULL, FALSE);
  66.       else
  67.      printf ("%d\n", ct.occupied ());
  68.    }
  69.    else if (do_clean)
  70.    {  sos_Open_result opened = ct.open (WRITING, mode);
  71.       if (opened == UNACCESSIBLE)
  72.      err_raise (err_USE, err_CNT_OPEN_FAILED, NULL, FALSE);
  73.       else if (opened == LOCKED)
  74.      err_raise (err_USE, err_CNT_CONTAINER_BUSY, NULL, FALSE);
  75.       else if (ct.occupied() == 0)
  76.       {  ct.destroy ();
  77.      ct.close ();
  78.       }
  79.    }
  80.    else if (do_squeeze)
  81.    {  sos_Open_result opened = ct.open (WRITING, mode);
  82.       if (opened == UNACCESSIBLE)
  83.      err_raise (err_USE, err_CNT_OPEN_FAILED, NULL, FALSE);
  84.       else if (opened == LOCKED)
  85.      err_raise (err_USE, err_CNT_CONTAINER_BUSY, NULL, FALSE);
  86.       else
  87.      ct.squeeze ();
  88.    }
  89.  
  90.    exit (0);
  91. }
  92.