From owner-freebsd-questions Sat Dec 2 12:32:40 2000 Delivered-To: freebsd-questions@freebsd.org Received: from cheshire.manunkind.org (cheshire.manunkind.org [216.254.114.213]) by hub.freebsd.org (Postfix) with ESMTP id A159237B400 for ; Sat, 2 Dec 2000 12:32:36 -0800 (PST) Received: (from ryan@localhost) by cheshire.manunkind.org (8.11.1/8.11.1) id eB2Ka6x40716; Sat, 2 Dec 2000 15:36:06 -0500 (EST) (envelope-from ryan) Date: Sat, 2 Dec 2000 15:36:06 -0500 From: Ryan Younce To: Sue Blake Cc: Kent Stewart , freebsd-questions@FreeBSD.ORG Subject: Re: unpseakable speaker device? Message-ID: <20001202153606.B40048@cheshire.manunkind.org> References: <20001202222840.K377@welearn.com.au> <3A28E1E2.8D703D3@urx.com> <20001202231152.M377@welearn.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20001202231152.M377@welearn.com.au>; from sue@welearn.com.au on Sat, Dec 02, 2000 at 11:11:54PM +1100 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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 #include 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