Annotation of src/usr.bin/aucat/aucat.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: aucat.c,v 1.5 2002/12/09 00:45:38 millert Exp $ */
1.1 kstailey 2: /*
3: * Copyright (c) 1997 Kenneth Stailey. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by Kenneth Stailey.
16: * 4. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30:
31: #include <sys/types.h>
32: #include <machine/endian.h>
33: #include <fcntl.h>
34: #include <stdio.h>
1.4 millert 35: #include <stdlib.h>
1.1 kstailey 36: #include <unistd.h>
1.4 millert 37: #include <err.h>
1.1 kstailey 38:
39: /*
40: * aucat: concatinate and play Sun 8-bit .au files
41: */
42:
43: /* function playfile: given a file which is positioned at the beginning
44: * of what is assumed to be an .au data stream copy it out to the audio
1.5 millert 45: * device. Return 0 on success, -1 on failure.
1.1 kstailey 46: */
47: int
1.6 ! deraadt 48: playfile(int fd, char *dev)
1.1 kstailey 49: {
1.4 millert 50: static int afd = -1;
51: int rd;
52: char buf[5120];
53:
54: if (afd == -1 && (afd = open(dev, O_WRONLY)) < 0) {
55: warn("can't open %s", dev);
56: return(-1);
57: }
58: while ((rd = read(fd, buf, sizeof(buf))) > 0) {
59: write(afd, buf, rd);
60: if (rd < sizeof(buf))
61: break;
62: }
63:
64: return (0);
1.1 kstailey 65: }
66:
67: int
1.6 ! deraadt 68: main(int argc, char *argv[])
1.1 kstailey 69: {
1.4 millert 70: int fd, ch;
71: unsigned long data;
72: char magic[5];
73: char *dev;
74:
75: dev = getenv("AUDIODEVICE");
76: if (dev == 0)
77: dev = "/dev/audio";
78:
79: while ((ch = getopt(argc, argv, "f:")) != -1) {
80: switch(ch) {
81: case 'f':
82: dev = optarg;
83: break;
84: }
85: }
86: argc -= optind;
87: argv += optind;
88:
89: while (argc) {
90: if ((fd = open(*argv, O_RDONLY)) < 0)
91: err(1, "cannot open %s", *argv);
92:
93: read(fd, magic, 4);
94: magic[4] = '\0';
95: if (strcmp(magic, ".snd")) {
96: /*
97: * not an .au file, bad header.
98: * Assume raw audio data since that's
99: * what /dev/audio generates by default.
100: */
101: lseek(fd, 0, SEEK_SET);
102: } else {
103: read(fd, &data, sizeof(data));
104: data = ntohl(data);
105: lseek(fd, (off_t)data, SEEK_SET);
106: }
107: if (playfile(fd, dev) < 0)
108: exit(1);
109: argc--;
110: argv++;
111: }
112: exit(0);
1.1 kstailey 113: }