#include <string.h>
#include <stdlib.h>
#include <libintl.h>
#include "bstream.h"
#include "trackio.h"
#include "misc_scsi.h"
#include "util.h"
#include "msgs.h"
#include "main.h"
#include "trackio.h"
#include "mmc.h"
static bstreamhandle
open_audio(char *fname)
{
int at;
char *ext;
if (audio_type == AUDIO_TYPE_NONE) {
ext = (char *)(strrchr(fname, '.'));
if (ext) {
ext++;
}
if ((ext == NULL) || ((at = get_audio_type(ext)) == -1)) {
err_msg(gettext(
"Cannot understand file extension for %s\n"),
fname);
exit(1);
}
} else {
at = audio_type;
}
if (at == AUDIO_TYPE_SUN)
return (open_au_read_stream(fname));
if (at == AUDIO_TYPE_WAV)
return (open_wav_read_stream(fname));
if (at == AUDIO_TYPE_CDA)
return (open_file_read_stream(fname));
if (at == AUDIO_TYPE_AUR)
return (open_aur_read_stream(fname));
return (NULL);
}
void
write_audio(char **argv, int start_argc, int argc)
{
bstreamhandle *h_ptr;
int i, nfiles;
struct track_info *ti;
uint32_t blks_req, blks_avail;
off_t fsize;
nfiles = argc - start_argc;
h_ptr = (bstreamhandle *)my_zalloc(nfiles * sizeof (bstreamhandle));
blks_req = 0;
for (i = 0; i < nfiles; i++) {
h_ptr[i] = open_audio(argv[start_argc + i]);
if (h_ptr[i] == NULL) {
err_msg(gettext("Cannot open %s: %s\n"),
argv[start_argc + i], get_err_str());
exit(1);
}
(void) (h_ptr[i])->bstr_size(h_ptr[i], &fsize);
blks_req += 150 + fsize/2352;
if (fsize % 2352)
blks_req++;
}
(void) check_device(target, CHECK_DEVICE_NOT_READY |
CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
EXIT_IF_CHECK_FAILED);
write_init(TRACK_MODE_AUDIO);
ti = (struct track_info *)my_zalloc(sizeof (*ti));
if ((build_track_info(target, -1, ti) == 0) ||
((ti->ti_flags & TI_NWA_VALID) == 0)) {
err_msg(gettext(
"Cannot get writable address for the media.\n"));
exit(1);
}
if ((blks_avail = get_last_possible_lba(target)) == 0) {
err_msg(gettext("Unable to determine media capacity. "
"Defaulting to 650 MB (74 minute) disc.\n"));
blks_avail = MAX_CD_BLKS;
} else {
blks_avail++;
}
blks_avail -= ti->ti_nwa;
if (blks_avail < blks_req) {
err_msg(gettext("Insufficient space on the media.\n"));
exit(1);
}
for (i = 0; i < nfiles; i++) {
write_next_track(TRACK_MODE_AUDIO, h_ptr[i]);
if (simulation && (nfiles != 1)) {
(void) printf(gettext(
"Simulation mode : skipping remaining tracks\n"));
break;
}
}
for (i = 0; i < nfiles; i++)
(h_ptr[i])->bstr_close(h_ptr[i]);
free(ti);
free(h_ptr);
write_fini();
fini_device(target);
exit(0);
}