home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1625 / wha.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.1 KB  |  186 lines

  1. /*
  2. ** wha - re-display messages from say(1)
  3. **
  4. ** Copyright (C) 1990 by Jef Poskanzer.
  5. **
  6. ** Permission to use, copy, modify, and distribute this software and its
  7. ** documentation for any purpose and without fee is hereby granted, provided
  8. ** that the above copyright notice appear in all copies and that both that
  9. ** copyright notice and this permission notice appear in supporting
  10. ** documentation.  This software is provided "as is" without express or
  11. ** implied warranty.
  12. */
  13.  
  14. #ifndef lint
  15. static char rcsid[] = "@(#) $Header: wha.c,v 1.10 90/07/26 10:11:00 jef Exp $";
  16. #endif
  17.  
  18. #include <stdio.h>
  19. #include <sys/signal.h>
  20. #include <errno.h>
  21. #include "libsaywha.h"
  22.  
  23. /* External routines. */
  24. extern int fprintf(), printf(), strcmp(), strlen();
  25. extern char *ctime(), *getenv(), *sprintf();
  26. extern int errno;    /* in case errno.h fails to declare it */
  27.  
  28. /* Forward routines. */
  29. int cleanup();
  30.  
  31. main( argc, argv )
  32. int argc;
  33. char *argv[];
  34.     {
  35.     int argn, n, t, messages, message, writing, written;
  36.     long lt;
  37.     char *user, *home, filename[200], buf[2000];
  38.     char tuser[200];
  39.     FILE *fp;
  40.     char *usage = "usage:  %s [ <n> | <user> | <n> <user> | <user> <n> ]\n";
  41.  
  42.     /* Initialize. */
  43.     sw_argv0 = argv[0];
  44.     (void) signal( SIGINT, cleanup );
  45.     (void) signal( SIGHUP, cleanup );
  46.     if ( sw_check_sayrc( ) < 0 )
  47.     (void) cleanup( 1 );
  48.     if ( sw_check_my_sayfile( ) < 0 )
  49.     (void) cleanup( 1 );
  50.  
  51.     /* Check args. */
  52.     n = -1;
  53.     user = NULL;
  54.     for ( argn = 1; argn < argc; ++argn )
  55.     {
  56.     if ( sscanf( argv[argn], "%d", &t ) == 1 )
  57.         {
  58.         if ( n != -1 )
  59.         {
  60.         (void) fprintf( stderr, usage, sw_argv0 );
  61.         (void) cleanup( 1 );
  62.         }
  63.         n = t;
  64.         }
  65.     else
  66.         {
  67.         if ( user != NULL )
  68.         {
  69.         (void) fprintf( stderr, usage, sw_argv0 );
  70.         (void) cleanup( 1 );
  71.         }
  72.         user = argv[argn];
  73.         }
  74.     }
  75.     if ( n == -1 )
  76.     n = 1;
  77.  
  78.     /* Open up the sayfile. */
  79.     if ( ( home = getenv( "HOME" ) ) == NULL )
  80.     {
  81.     (void) fprintf( stderr, "%s: can't find home directory\n", sw_argv0 );
  82.     (void) cleanup( 1 );
  83.     }
  84.     (void) sprintf( filename, "%s/.sayfile", home );
  85.     if ( ( fp = fopen( filename, "r" ) ) == NULL )
  86.     {
  87.     (void) fputs( sw_argv0, stderr );
  88.     perror( ": can't open .sayfile" );
  89.     (void) cleanup( 1 );
  90.     }
  91.     else
  92.     {
  93.     /* Read through once just to count up the messages that match. */
  94.     messages = 0;
  95.     while ( errno = 0, fgets( buf, sizeof(buf), fp ) != NULL )
  96.         {
  97.         if ( buf[0] == MSGSEP )
  98.         {
  99.         if ( sscanf( buf + 1, "%d,%s\n", &t, tuser ) != 2 )
  100.             {
  101.             (void) fprintf( stderr, "%s: garbled .sayfile!\n",
  102.             sw_argv0 );
  103.             (void) cleanup( 1 );
  104.             }
  105.         if ( user == NULL )
  106.             ++messages;
  107.         else if ( strcmp( user, tuser ) == 0 )
  108.             ++messages;
  109.         }
  110.         }
  111.     if ( errno != 0 )
  112.         {
  113.         (void) fputs( sw_argv0, stderr );
  114.         perror( ": error reading .sayfile" );
  115.         (void) cleanup( 1 );
  116.         }
  117.     if ( messages > 0 )
  118.         {
  119.         /* Now read through again to print. */
  120.         (void) rewind( fp );
  121.         message = 0;
  122.         writing = 0;
  123.         written = 0;
  124.         while ( errno = 0, fgets( buf, sizeof(buf), fp ) != NULL )
  125.         {
  126.         if ( buf[0] == MSGSEP )
  127.             {
  128.             if ( sscanf( buf + 1, "%d,%s\n", &t, tuser ) != 2 )
  129.             {
  130.             (void) fprintf( stderr, "%s: garbled .sayfile!\n",
  131.                 sw_argv0 );
  132.             (void) cleanup( 1 );
  133.             }
  134.             if ( user == NULL || strcmp( user, tuser ) == 0 )
  135.             {
  136.             ++message;
  137.             if ( message > messages - n )
  138.                 {
  139.                 char *cp;
  140.  
  141.                 writing = 1;
  142.                 ++written;
  143.                 if ( written > 1 )
  144.                 (void) putchar( '\n' );
  145.                 lt = t;
  146.                 cp = ctime( < );
  147.                 cp[strlen(cp) - 1] = '\0';
  148.                 (void) printf( "[%s]\n", cp );
  149.                 }
  150.             else
  151.                 writing = 0;
  152.             }
  153.             else
  154.             writing = 0;
  155.             }
  156.         else if ( writing )
  157.             (void) fputs( buf, stdout );
  158.         }
  159.         if ( errno != 0 )
  160.         {
  161.         (void) fputs( sw_argv0, stderr );
  162.         perror( ": error reading .sayfile" );
  163.         (void) cleanup( 1 );
  164.         }
  165.         }
  166.  
  167.     if ( fclose( fp ) == EOF )
  168.         {
  169.         (void) fprintf( stderr, "%s: error closing ", sw_argv0 );
  170.         perror( filename );
  171.         (void) cleanup( 1 );
  172.         }
  173.     }
  174.  
  175.     /* All done. */
  176.     (void) cleanup( 0 );
  177.     }
  178.  
  179. int
  180. cleanup( status )
  181. int status;
  182.     {
  183.     sw_cleanup( );
  184.     exit( status );
  185.     }
  186.