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