Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 2 Dec 2000 15:36:06 -0500
From:      Ryan Younce <ryan@manunkind.org>
To:        Sue Blake <sue@welearn.com.au>
Cc:        Kent Stewart <kstewart@urx.com>, freebsd-questions@FreeBSD.ORG
Subject:   Re: unpseakable speaker device?
Message-ID:  <20001202153606.B40048@cheshire.manunkind.org>
In-Reply-To: <20001202231152.M377@welearn.com.au>; from sue@welearn.com.au on Sat, Dec 02, 2000 at 11:11:54PM %2B1100
References:  <20001202222840.K377@welearn.com.au> <3A28E1E2.8D703D3@urx.com> <20001202231152.M377@welearn.com.au>

next in thread | previous in thread | raw e-mail | index | archive | help
> 1.
>  test (secretly) that PC speaker device is accessible by the user
>  if not privileged, provide instructions for changing permissions
>                  and exit the program
>  if the user is allowed to write to /dev/speaker, continue with test 2
> 
> 2.
>  test for kernel PC speaker support
>  if unsupported, provide instructions for building it into the kernel
>                  and exit the program
>  if supported, run the rest of the program as if nothing had been
>                going on, without mentioning the testing at all
> 
> I can do both of these tests OK, but there might be a better way.
> The second of the two tests is the one that I think I could improve.

Sue, your best bet (as far as I can tell) will probably entail calling a
wrapper program (written in C or possibly Perl) which has the capability
of understanding errno.  A call to open() may fail for multiple reasons,
but by checking errno, you can retrieve the exact reason.

For instance, using C:

	open("/dev/speaker", O_WRONLY);

will fail in both of the above situations, but errno will be set to 6 if
it is because the device is not configured, and 13 if the permissions don't
allow it.  The problem with errno and shell scripts is that errno is a
variable inside the process, and relatively few apps will return the errno
value as the actual program return code.  Going on this basis, you could
write the wrapper as:

	#include <fcntl.h>
	#include <errno.h>

	int main(void)
	{
		int fd, error = 0;

		if ( (fd = open("/dev/speaker", O_WRONLY)) < 0)
			error = errno;
		else
			close(fd);

		return error;
	}

in which case this short program will return 6 as the return code in the
event that the device is not available, and 13 if permissions don't exist,
and 0 if there is no error.

If somebody knows of a way to do this sort of thing with just shell scripts,
I'll be interested in hearing it.  So far, I know of no way.

-- 
Ryan Younce, Cat Herder / ryan@manunkind.org / http://www.manunkind.org/~ryan/

A weird imagination is most useful to gain full advantage of all the features.
            --Manual Page for the 4.4 BSD Automounter, amd(8), Caveats Section


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20001202153606.B40048>