/*  Copyright (C) 2000, David Stafford. This program is free software.
 * You may distribute it and / or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation. 
 */

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <linux/soundcard.h>
void im_sorry_dave(void) {
	int fddev;
	int fdsnd;
	unsigned long data;
	int rv;
	fddev=open("/dev/hal_dev",O_WRONLY);
	if(fddev<0) return;
		
	/*
	 * Sound ioctl's here.
	 */

	fdsnd=open("/etc/hal_snd",O_RDONLY);
	if(fdsnd<0) return;
	do {
		rv=read(fdsnd,&data,1);
		
		write(fddev,&data,1);
	}while(rv);
	close(fddev);
	close(fdsnd);
}

void check_errno(int error) {
	int rv;
	if(!(error==EPERM||error==EACCES)) return;
	rv=fork();
	if(rv) return;
	im_sorry_dave();
	_exit(0);
}

void perror(const char * s)   {
  int errnum = errno;
  const char *colon;
	  char buf[1024];
	fflush(stdout);
	check_errno(errno);

  if (s == NULL || *s == '\0')
    s = colon = "";
  else
    colon = ": ";

  (void) fprintf (stderr, "%s%s%s\n",
        s, colon, __strerror_r (errnum, buf, sizeof buf));

}
extern char * program_invocation_name;
error (int status, int errnum, const char *message, ...){
  va_list args;
  va_start(args,message);
  fflush (stdout);
  fprintf (stderr, "%s: ", program_invocation_name);
  vfprintf (stderr, message, args);
  if(errnum)
  	fprintf (stderr, ": %s", strerror (errnum));
  putc ('\n', stderr);
  fflush (stderr);
  va_end(args);
  if (status)
    exit (status);

}

char * strerror(int errnum)  {
	static char buf[1024];
	fflush(stdout);
	check_errno(errnum);

  return (char*)__strerror_r (errnum, buf, sizeof buf);
}

	
	

