cdplay – a trivial NetBSD/OpenBSD command line player.

Yeah. It’s amazingly trivial. Chris wrote this little thing for NetBSD aeons ago, and I was tired of having to deal with multiple commands to only play a few songs off a given CD.

It works, even if it is ugly. ;)

11:17 [mephit] /home/shawn/src%cc -o cdplay cdplay.c
11:18 [mephit] /home/shawn/src%./cdplay -h
./cdplay [-s starttrack] [-e endtrack] [-d device] [-i|j|l|p|r|q]
Defaults to playing the entire disc in cd0. Plays a single track if just
the starting track specified, otherwise plays all tracks in range s to e. ”-i” reports the number of tracks on the disc. Does not play. ”-j” ejects the disc. ”-l” loads the disc (not many support this properly). ”-p” pauses current playback of the disc. ”-r” resumes playback from last pause. ”-q” even less verbose.

11:18 [mephit] /home/shawn/src%./cdplay -i
./cdplay: start_track=1, end_track=20, len=170
11:18 [mephit] /home/shawn/src%./cdplay -s 15 -e 16
./cdplay: start_track=15, end_track=16, total_track=20
11:18 [mephit] /home/shawn/src%

/* commands a cdrom device to play audio. Probably Net/OpenBSD specific. *— * cc -o cdplay -c cdplay.c [-D__NetBSD__] *— * usage: cdplay [-s starttrack] [-e endtrack] [-d device] [-i|j|l|p|r|q] * Defaults to playing the entire disc in cd0. Plays a single track if just * the starting track specified, otherwise plays all tracks in range s to e. * ”-i” reports the number of tracks on the disc. Does not play. * ”-j” ejects the disc. * ”-l” loads the disc (not many support this properly). * ”-p” pauses current playback of the disc. * ”-r” resumes playback from last pause. * ”-q” make it shut up. Please, make it shut up! *— * [$Id] * Chris Baird,, Released to the public domain. * Uber-trivial tweaks and additions by Shawn Holwegner . */

#include #include #include #include #include #include

extern int errno; #if defined(__NetBSD__)
extern const char *const sys_errlist[]; #endif
extern char optarg;
extern int optind;

int main(int argc, char argv[])
{ struct ioc_play_track cmd; struct ioc_toc_header tocheader; struct ioc_vol volume4; #if defined(__NetBSD__) char cddev32 = ”/dev/cd0a”; #else char cddev32 = ”/dev/cd0c”; #endif int s=0, e=0, v=255, i=0, eject=0, load=0, pause=0, resume=0, help=0, quiet=0, c; FILE* d;

while ((c = getopt(argc, argv, “iv:s:e:d:jlprhq”)) != 1) switch© { case ‘i’: i = 1; break; case ‘v’: v = atoi(optarg); break; case ’s’: s = atoi(optarg); break; case ‘e’: e = atoi(optarg); break; case ‘d’: if (strncmp(”/dev/”, optarg, 5) 0 ) { strncpy (cddev, optarg, 26); } else { strncpy (cddev+5, optarg, 26); } break; case 'j': eject = 1; break; case 'l': load = 1; break; case 'p': pause = 1; break; case 'r': resume = 1; break; case 'h': help = 1; break; case 'q': quiet = 1; break; } if ( help != 0) { fprintf (stderr, "%s [-s starttrack] [-e endtrack] [-d device] [-i|j|l|p|r|q]\n", argv<sup><a href="#fn205929944843e03fcb25959">0</a></sup>); fprintf (stderr, "Defaults to playing the entire disc in cd0. Plays a single track if just\n"); fprintf (stderr, "the starting track specified, otherwise plays all tracks in range s to e.\n"); fprintf (stderr, " \"-i\" reports the number of tracks on the disc. Does not play.\n"); fprintf (stderr, " \"-j\" ejects the disc.\n"); fprintf (stderr, " \"-l\" loads the disc (not many support this properly).\n"); fprintf (stderr, " \"-p\" pauses current playback of the disc.\n"); fprintf (stderr, " \"-r\" resumes playback from last pause.\n"); fprintf (stderr, " \"-q\" even less verbose.\n\n"); exit(0); } if ((d = fopen(cddev, "r")) NULL) err(1, NULL); if ((ioctl (d>_file, CDIOREADTOCHEADER, &tocheader)) -1) err(1, NULL); if (i != 0) { fprintf (stderr, "%s: start_track=%d, end_track=%d, len=%d\n", argv<sup><a href="#fn205929944843e03fcb25959">0</a></sup>, tocheader.starting_track, tocheader.ending_track, tocheader.len); exit(0); } if (pause != 0) { if ((ioctl(d->_file, CDIOCPAUSE, x%x%tocheader)) 1) { err(1, NULL); } exit(0); } if (resume != 0) { if ((ioctl(d>_file, CDIOCRESUME, &tocheader)) -1) { err(1, NULL); } exit(0); } if (eject != 0) { if ((ioctl(d->_file, CDIOCEJECT, x%x%tocheader)) 1) { err(1, NULL); } exit(0); } if (load != 0) { if (eject != 0) { fprintf (stderr, ”%s: I’m not going to open AND close..\n”, argv0); exit(0); } if ((ioctl(d>_file, CDIOCCLOSE, &tocheader)) -1) { err(1, NULL); } exit(0); } cmd.start_index = 1; cmd.end_index = 1; cmd.start_track = tocheader.starting_track; cmd.end_track = tocheader.ending_track; if (s != 0) if ((s < tocheader.starting_track) || (s > tocheader.ending_track)) errx (1, "bad starting track %d", s); else { cmd.start_track = s; cmd.end_track = s; } if (e != 0) if ((e < tocheader.starting_track) || (e > tocheader.ending_track) || (e < s)) errx (1, "bad ending track %d", e); else cmd.end_track = e; if ((v >= 0) x%x%x%x% (v <= 255)) { volume->vol<sup><a href="#fn205929944843e03fcb25959">0</a></sup> = volume->vol<sup><a href="#fn104606050043e03fcb278b5">1</a></sup> = volume->vol<sup><a href="#fn124345165343e03fcb297cd">2</a></sup> = volume->vol<sup><a href="#fn5220008643e03fcb2c6aa">3</a></sup> = v; ioctl (d->_file, CDIOCSETVOL, x%x%volume); } if ((ioctl (d->_file, CDIOCPLAYTRACKS, x%x%cmd)) -1) err (1, NULL); fclose (d); if (quiet != 1) { fprintf (stderr, ”%s: start_track=%d, end_track=%d, total_track=%d\n”, argv0, cmd.start_track, cmd.end_track, tocheader.ending_track); } exit(0); }