home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume7 / gosip2 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-08  |  5.4 KB  |  211 lines

  1. #include "global.h"
  2. #include "cat.h"
  3. #include "edit.h"
  4. #include "control.h"
  5. #include "util.h"
  6. #include "lu.h"
  7.  
  8. #define VERSION 104
  9. #define usage() (void) fprintf ( stderr, "Usage: %s [-p|-l|-e|-t|-s|-r|-v|-x|-z]\n", file ); exit ( 4 );
  10. #define check_flag() if ( argv[1][2] ) { usage() }
  11.  
  12. extern char *sprintf();
  13. char text_file[MAX_FILE_LENGTH], file[MAX_FILE_LENGTH];
  14. char history_file[MAX_FILE_LENGTH], last_file[MAX_FILE_LENGTH];
  15. #ifdef LOCKF_BROKEN
  16. char lock_file[MAX_FILE_LENGTH];
  17. #endif LOCKF_BROKEN
  18.  
  19. /*  void init_file_names ( invocation )
  20.  *
  21.  *  This function works out the names of the files used by gosip, from the
  22.  *  name by which it is invoked, and places them in various global variables.
  23.  *
  24.  *  invocation  :  points to the name by which gosip was invoked (argv[0]).
  25.  */
  26. void init_file_names ( invocation )
  27. char *invocation;
  28. {
  29.   char *ptr = invocation, *temp = invocation;
  30.  
  31.   while ( *ptr != '\0' )
  32.     if (  *ptr++ == '/' )
  33.       temp = ptr;
  34.   (void) strcpy ( file, temp);
  35.   (void) sprintf ( history_file, "%s/HISTOrY.%s", DATA_FILE_DIRECTORY, file );
  36.   (void) sprintf ( text_file, "%s%s/text.%s", DATA_FILE_DIRECTORY, SECRET_DIR, file );
  37.   (void) sprintf ( last_file, "%s/LAsT.%s", DATA_FILE_DIRECTORY, file );
  38. #ifdef LOCKF_BROKEN
  39.   (void) sprintf ( lock_file, "%s/lLo$Ck.%s", DATA_FILE_DIRECTORY, file );
  40. #endif LOCKF_BROKEN
  41. }
  42.  
  43. /*  void reserve()
  44.  *
  45.  *  This function prints a message as soon as the gosip file is no longer
  46.  *  being edited.  It firsts checks if gosip is being edit.  If so, it
  47.  *  fork()s, and the child checks for the lock every 5 seconds.
  48.  */
  49. static void reserve()
  50. {
  51.   int pid;
  52.  
  53.   if ( ! lock ( CHECK ) )
  54.   {
  55.     (void) printf ( "The %s file is not being edited.\n", file );
  56.     exit ( 1 );
  57.   }
  58.   error ( pid = fork(), "fork failed" );
  59.   if ( pid == 0 )
  60.   {
  61.     while ( lock ( CHECK ) )
  62.       sleep ( 5 );
  63.     (void) printf ( "\007\r\nThe %s file is no longer being edited.\r\n", file );
  64.   }
  65.   else
  66.     (void) printf ( "You'll be informed when %s becomes available.\n", file );
  67.   exit ( 0 );
  68. }
  69.  
  70. main ( argc, argv )
  71. int argc;
  72. char *argv[];
  73. {
  74.   init_file_names ( argv[0] );
  75.   check_if_up();
  76.  
  77.   if ( ! strcmp ( file, "lu" ) )
  78.     lu ( argc, argv );
  79.   if ( ! strcmp ( file, "allgoss" ) )
  80.   {
  81.     char *files[FILE_NUM], **fptr = files;
  82.     byte num = allgoss ( files );
  83.  
  84.     while ( num-- )
  85.       puts ( *fptr++ );
  86.     exit ( 0 );
  87.   }
  88.  
  89.   if ( argc == 1 )
  90.   {
  91.     char *opt = getenv ( "GOSIP" );
  92.  
  93.     if ( opt )
  94.     {
  95.       argc = 2;
  96.       argv[1] = opt;
  97.     }
  98.   }
  99.   switch ( argc )
  100.   {
  101.   default:
  102.     usage();
  103.   case 1:
  104.     do_edit ( normal, "dummy" );
  105.   case 2:
  106.     if ( argv[1][0] == '-' )
  107.       switch ( argv[1][1] )
  108.       {
  109.       case '#':
  110.         if ( strcmp ( usercode(), SUPER_USER ) )
  111.           catfile ( normal );   /* pretend the option doesn't exist */
  112.         if ( argv[1][2] == '\0' )
  113.         {
  114.           puts ( "Use the option correctly, fishhead.\n" );
  115.           exit ( 3 );
  116.         }
  117.         if ( ! access ( DOWN_FILE, F_OK ) )
  118.         {
  119.           puts ( "The system is already down, fool.\n" );
  120.           exit ( 2 );
  121.         }
  122.         (void) printf ( "Bringing down the %s files.\n", file );
  123.         {                       /* set up a 'lock' file */
  124.           FILE *fp = fopen ( DOWN_FILE, "w" );
  125.  
  126.           if ( ! fp )
  127.           {
  128.             perror ( "Couldn't create lock file" );
  129.             exit ( 2 );
  130.           }
  131.           (void) fprintf ( fp, "%s\n", &argv[1][2] );
  132.           error ( fclose ( fp ), "Couldn't close lock file" );
  133.           if ( chmod ( DOWN_FILE, 0666 ) == -1 )
  134.             perror ( "Couldn't alter permissions" );
  135.         }
  136.         (void) printf ( "The %s files are down.\n", file );
  137.         exit ( 0 );
  138.       case '@':
  139.         check_flag();
  140.         if ( strcmp ( usercode(), SUPER_USER ) )
  141.           catfile ( normal );   /* pretend the option doesn't exist */
  142.         if ( access ( DOWN_FILE, F_OK ) )
  143.         {
  144.           puts ( "The system isn't down, fool.\n" );
  145.           exit ( 2 );
  146.         }
  147.         inform_users();
  148.         if ( unlink ( DOWN_FILE ) )
  149.           perror ( "Couldn't remove the down file" );
  150.         else
  151.           (void) printf ( "Brought %s up again.\n", file );
  152.         exit ( 0 );
  153.       case 'c':
  154.         do_edit ( cflag, &argv[1][2] );
  155.       case 'e':
  156.         check_flag();
  157.         do_edit ( abort, "dummy" );
  158.       case 'l':
  159.         check_flag();
  160.         catfile ( normal );
  161.       case 'p':
  162.         (void) printf ( "Sending %s to the printer.\n", file );
  163.         (void) fflush ( stdout );
  164.         if ( argv[1][2] )
  165.         {
  166.           argv[1][1] = 'P';
  167.           execlp ( "lpr", "lpr", argv[1], text_file, (char *) 0 );
  168.         }
  169.         else
  170.           execlp ( "lpr", "lpr", text_file, (char *) 0 );
  171.         perror ( "Couldn't run lpr" );
  172.         exit ( 2 );
  173.       case 'r':
  174.         check_flag();
  175.         reserve();
  176.       case 's':
  177.         check_flag();
  178.         exit ( lock ( CHECK ) != 0);
  179.       case 't':
  180.         check_flag();
  181.         if ( lock ( CHECK ) )
  182.         {
  183.           lastedit ( EDIT );
  184.           exit ( 1 );
  185.         }
  186.         else
  187.         {
  188.           (void) printf ( "The %s file is not being edited.\n", file );
  189.           exit ( 0 );
  190.         }
  191.       case 'v':
  192.         check_flag();
  193.         (void) printf ( "APS %s version %d.\n", file, VERSION );
  194.         exit ( 0 );
  195.       case 'x':
  196.         check_flag();
  197.         do_edit ( normal, "dummy" );
  198.       case 'z':
  199.     check_flag();
  200.     catfile ( raw );
  201.       default:
  202.         usage();
  203.       }
  204.     else
  205.       if ( argv[1][1] == '\0' )
  206.         catfile ( normal );
  207.       else
  208.         usage();
  209.   }
  210. }
  211.