Annotation of src/usr.bin/aucat/aucat.c, Revision 1.10
1.10 ! ian 1: /* $OpenBSD: aucat.c,v 1.9 2003/10/20 21:10:19 jmc 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.8 david 36: #include <string.h>
1.1 kstailey 37: #include <unistd.h>
1.4 millert 38: #include <err.h>
1.1 kstailey 39:
40: /*
1.9 jmc 41: * aucat: concatenate and play Sun 8-bit .au files
1.1 kstailey 42: */
1.7 deraadt 43:
44: int playfile(int, char *);
1.1 kstailey 45:
46: /* function playfile: given a file which is positioned at the beginning
47: * of what is assumed to be an .au data stream copy it out to the audio
1.5 millert 48: * device. Return 0 on success, -1 on failure.
1.1 kstailey 49: */
50: int
1.6 deraadt 51: playfile(int fd, char *dev)
1.1 kstailey 52: {
1.4 millert 53: static int afd = -1;
54: int rd;
55: char buf[5120];
56:
57: if (afd == -1 && (afd = open(dev, O_WRONLY)) < 0) {
58: warn("can't open %s", dev);
59: return(-1);
60: }
61: while ((rd = read(fd, buf, sizeof(buf))) > 0) {
62: write(afd, buf, rd);
63: if (rd < sizeof(buf))
64: break;
65: }
66:
67: return (0);
1.1 kstailey 68: }
69:
70: int
1.6 deraadt 71: main(int argc, char *argv[])
1.1 kstailey 72: {
1.4 millert 73: int fd, ch;
74: unsigned long data;
75: char magic[5];
76: char *dev;
77:
78: dev = getenv("AUDIODEVICE");
79: if (dev == 0)
80: dev = "/dev/audio";
81:
82: while ((ch = getopt(argc, argv, "f:")) != -1) {
83: switch(ch) {
84: case 'f':
85: dev = optarg;
86: break;
87: }
88: }
89: argc -= optind;
90: argv += optind;
91:
92: while (argc) {
93: if ((fd = open(*argv, O_RDONLY)) < 0)
94: err(1, "cannot open %s", *argv);
95:
96: read(fd, magic, 4);
97: magic[4] = '\0';
98: if (strcmp(magic, ".snd")) {
99: /*
100: * not an .au file, bad header.
101: * Assume raw audio data since that's
102: * what /dev/audio generates by default.
103: */
104: lseek(fd, 0, SEEK_SET);
105: } else {
106: read(fd, &data, sizeof(data));
107: data = ntohl(data);
108: lseek(fd, (off_t)data, SEEK_SET);
109: }
110: if (playfile(fd, dev) < 0)
111: exit(1);
1.10 ! ian 112: (void) close(fd);
1.4 millert 113: argc--;
114: argv++;
115: }
116: exit(0);
1.1 kstailey 117: }