home *** CD-ROM | disk | FTP | other *** search
- /*
- ** wha - re-display messages from say(1)
- **
- ** Copyright (C) 1990 by Jef Poskanzer.
- **
- ** Permission to use, copy, modify, and distribute this software and its
- ** documentation for any purpose and without fee is hereby granted, provided
- ** that the above copyright notice appear in all copies and that both that
- ** copyright notice and this permission notice appear in supporting
- ** documentation. This software is provided "as is" without express or
- ** implied warranty.
- */
-
- #ifndef lint
- static char rcsid[] = "@(#) $Header: wha.c,v 1.10 90/07/26 10:11:00 jef Exp $";
- #endif
-
- #include <stdio.h>
- #include <sys/signal.h>
- #include <errno.h>
- #include "libsaywha.h"
-
- /* External routines. */
- extern int fprintf(), printf(), strcmp(), strlen();
- extern char *ctime(), *getenv(), *sprintf();
- extern int errno; /* in case errno.h fails to declare it */
-
- /* Forward routines. */
- int cleanup();
-
- main( argc, argv )
- int argc;
- char *argv[];
- {
- int argn, n, t, messages, message, writing, written;
- long lt;
- char *user, *home, filename[200], buf[2000];
- char tuser[200];
- FILE *fp;
- char *usage = "usage: %s [ <n> | <user> | <n> <user> | <user> <n> ]\n";
-
- /* Initialize. */
- sw_argv0 = argv[0];
- (void) signal( SIGINT, cleanup );
- (void) signal( SIGHUP, cleanup );
- if ( sw_check_sayrc( ) < 0 )
- (void) cleanup( 1 );
- if ( sw_check_my_sayfile( ) < 0 )
- (void) cleanup( 1 );
-
- /* Check args. */
- n = -1;
- user = NULL;
- for ( argn = 1; argn < argc; ++argn )
- {
- if ( sscanf( argv[argn], "%d", &t ) == 1 )
- {
- if ( n != -1 )
- {
- (void) fprintf( stderr, usage, sw_argv0 );
- (void) cleanup( 1 );
- }
- n = t;
- }
- else
- {
- if ( user != NULL )
- {
- (void) fprintf( stderr, usage, sw_argv0 );
- (void) cleanup( 1 );
- }
- user = argv[argn];
- }
- }
- if ( n == -1 )
- n = 1;
-
- /* Open up the sayfile. */
- if ( ( home = getenv( "HOME" ) ) == NULL )
- {
- (void) fprintf( stderr, "%s: can't find home directory\n", sw_argv0 );
- (void) cleanup( 1 );
- }
- (void) sprintf( filename, "%s/.sayfile", home );
- if ( ( fp = fopen( filename, "r" ) ) == NULL )
- {
- (void) fputs( sw_argv0, stderr );
- perror( ": can't open .sayfile" );
- (void) cleanup( 1 );
- }
- else
- {
- /* Read through once just to count up the messages that match. */
- messages = 0;
- while ( errno = 0, fgets( buf, sizeof(buf), fp ) != NULL )
- {
- if ( buf[0] == MSGSEP )
- {
- if ( sscanf( buf + 1, "%d,%s\n", &t, tuser ) != 2 )
- {
- (void) fprintf( stderr, "%s: garbled .sayfile!\n",
- sw_argv0 );
- (void) cleanup( 1 );
- }
- if ( user == NULL )
- ++messages;
- else if ( strcmp( user, tuser ) == 0 )
- ++messages;
- }
- }
- if ( errno != 0 )
- {
- (void) fputs( sw_argv0, stderr );
- perror( ": error reading .sayfile" );
- (void) cleanup( 1 );
- }
- if ( messages > 0 )
- {
- /* Now read through again to print. */
- (void) rewind( fp );
- message = 0;
- writing = 0;
- written = 0;
- while ( errno = 0, fgets( buf, sizeof(buf), fp ) != NULL )
- {
- if ( buf[0] == MSGSEP )
- {
- if ( sscanf( buf + 1, "%d,%s\n", &t, tuser ) != 2 )
- {
- (void) fprintf( stderr, "%s: garbled .sayfile!\n",
- sw_argv0 );
- (void) cleanup( 1 );
- }
- if ( user == NULL || strcmp( user, tuser ) == 0 )
- {
- ++message;
- if ( message > messages - n )
- {
- char *cp;
-
- writing = 1;
- ++written;
- if ( written > 1 )
- (void) putchar( '\n' );
- lt = t;
- cp = ctime( < );
- cp[strlen(cp) - 1] = '\0';
- (void) printf( "[%s]\n", cp );
- }
- else
- writing = 0;
- }
- else
- writing = 0;
- }
- else if ( writing )
- (void) fputs( buf, stdout );
- }
- if ( errno != 0 )
- {
- (void) fputs( sw_argv0, stderr );
- perror( ": error reading .sayfile" );
- (void) cleanup( 1 );
- }
- }
-
- if ( fclose( fp ) == EOF )
- {
- (void) fprintf( stderr, "%s: error closing ", sw_argv0 );
- perror( filename );
- (void) cleanup( 1 );
- }
- }
-
- /* All done. */
- (void) cleanup( 0 );
- }
-
- int
- cleanup( status )
- int status;
- {
- sw_cleanup( );
- exit( status );
- }
-