/* -*- C -*- */

/* ss.c */

/*
 * Author: Nikita Danilov <NikitaDanilov@yahoo.COM>
 * Keywords: signal, sigqueue
 *
 * This file is part of ss.
 *
 * Ss is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>

typedef enum { ok = 0, 
			   invalid_argc, 
			   sigqueue_fails } ret_t;

int main( int argc, char **argv )
{
  ret_t result;

  result = ok;
  if( argc == 4 )
	{
	  if( isdigit( argv[ 2 ][ 0 ] ) )
		{
		  /* ss pid signum value */
		  pid_t pid;
		  int   sig;
		  int   val;
		  union sigval send;

		  pid = atoi( argv[ 1 ] );
		  sig = atoi( argv[ 2 ] );
		  val = strtol( argv[ 3 ], NULL, 0 );
		  printf( "ss: sigqueue( %i, %i, { sival_int: %i} )", pid, sig, val );
		  send.sival_int = val;
		  if( sigqueue( pid, sig, send ) == -1 )
			{
			  printf( " fails: %s(%i)", strerror( errno ), errno );
			  result = sigqueue_fails;
			}
		  printf( "\n" );
		}
	}
  else if( ( argc == 2 ) && !strcmp( argv[ 1 ], "-?" ) )
	{
	  extern const char * const sys_siglist[];
	  int i;
		  
	  for( i = 0 ; i < _NSIG ; ++i )
		{
		  if( sys_siglist[ i ] != NULL )
			{
			  printf( "oid: sys_siglist[ %i ] = %s\n", i, sys_siglist[ i ] );
			}
		}
	}
  else
	{
	  fprintf( stderr, "Usage:"
			   "\n\t\t%s pid signum val"
			   "\n\t\t%s -?\n"
			   "\n\tSend signal with info (sigqueue)"
			   "\n\t-? shows signal names"
			   "\n\tCompiled from " __FILE__ " at " __DATE__ "\n\n", 
			   argv[ 0 ], argv[ 0 ] );
	  result = invalid_argc;
	}

  return result;
}

